龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

VC程序中定制对话框中的回车键(6)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
BEGIN_MESSAGE_MAP(CMyDlg, CDlgWithAccelerators) ON_COMMAND(ID_MY_ENTER, OnMyEnter) // The following is NOT needed since I am using accelerators to map // ENTER to ID_MY_ENTER. But if all you want to d

BEGIN_MESSAGE_MAP(CMyDlg, CDlgWithAccelerators)
ON_COMMAND(ID_MY_ENTER, OnMyEnter)

// The following is NOT needed since I am using accelerators to map
// ENTER to ID_MY_ENTER. But if all you want to do is ignore the Enter key,
// you can handle DM_GETDEFID as below.
// ON_MESSAGE(DM_GETDEFID, OnGetDefID) // not used
END_MESSAGE_MAP()

CMyDlg::CMyDlg(CWnd* pParent) : CDlgWithAccelerators(IDD_MYDIALOG, pParent)
{}

//////////////////// Initialize dialog:
BOOL CMyDlg::OnInitDialog()
{
 CDlgWithAccelerators::OnInitDialog();
 // Set the icon for this dialog. The framework does this automatically
 // when the application's main window is not a dialog
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 ASSERT(m_hIcon);
 SetIcon(m_hIcon, TRUE); // Set big icon
 SetIcon(m_hIcon, FALSE); // Set small icon
 // use same resource name as dialog to load dialog's accelerators
 m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(),m_lpszTemplateName);
 ASSERT(m_hAccel);
 return TRUE; // return TRUE unless you set the focus to a control
}

//////////////////// This is called to handle ID_MY_ENTER--ie, Enter key.
void CMyDlg::OnMyEnter()
{
 TRACE(_T("CMyDlg::OnMyEnter "));
 NextInTabOrder(); // move to next control
}

//////////////////// Helper function to move focus to the next control.
void CMyDlg::NextInTabOrder()
{
 CWnd* pWndNext = GetNextDlgTabItem(GetFocus());
 if (pWndNext) {
  pWndNext->SetFocus();
 }
}

//////////////////
// This function is not used, since its message map entry is commented out.
// If all you want to do is ignore the Enter key (not map it to a command),
// then all you have to do is return zero here. Note that you MUST return
// the special code DC_HASDEFID in the high-order word!!
LRESULT CMyDlg::OnGetDefID(WPARAM wp, LPARAM lp)
{
 TRACE(_T("CMyDlg::OnGetDefID "));
 return MAKELONG(0,DC_HASDEFID);
}

  四、小结

  综上所述,MFC中为对话框添加加速键功能的方法就是:加载加速键和重载PreTranslateMessage()函数。也就是说,如果开发人员决定在对话框中使用加速键,不用去操心OnGetDefID()函数的处理,而是要重点实现加速键对应的WM_COMMAND消息响应处理函数。

收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!
按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接