有时候,在用MFC编程时,要把OK按钮和Cancel按钮去掉。但删除后还能响应Enter键和ESC键动作使窗口关闭从而退出程序。
那么怎么屏蔽ESC和ENTER键退出呢?
下面是拷贝的部分代码,我也不知是不是要改动这部分,望赐教,谢谢!!
一种方法:
重载对话框的PreTranslateMessage函数屏蔽调消息
CxxDlg::PreTranslateMessage(MSG* pMsg)
{
//屏蔽esc键和enter键
if((pMsg->message == WM_KEYDOWN &&
pMsg->wParam == VK_ESCAPE))
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
或BOOL C..Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN){
switch(pMsg->wParam){
case VK_RETURN://截获回车
return TRUE;
case VK_ESCAPE://截获ESC
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
或BOOL CxxxDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE) return TRUE;
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN) return TRUE;
else
return CDialog::PreTranslateMessage(pMsg);
}
另一种方法:
重载OnOk或者OnCancel函数,在其中直接返回