VC程序中定制对话框中的回车键(6)
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消息响应处理函数。
- 上一篇:VC通过HTTP方式获取网页
- 下一篇:VC++实现Windows回收站的文件存取