使用类向导为对话框添加PreTranslateMessage(MSG* pMsg)虚函数,并在在函数内添加一下代码:
//屏蔽回车键和ESC键消息
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
// 屏蔽ESC和回车键退出对话框的消息
if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
{
return TRUE;
}
if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_ESCAPE))
{
return TRUE;
}
return CDialogEx::PreTranslateMessage(pMsg);
}
即对于回车键和ESC键的消息进行直接返回,不进行下一步的处理,默认是退出对话框。