MFC中默认回车或ESC会关闭对话框,解决办法:
对于有对话框的类,点击右键,virtual function,添加PreTranslateMessage,然后Edit,写入如下代码:
BOOL CForm::PreTranslateMessage(MSG* pMsg)
{
{
//屏蔽 回车和ESC键
//屏蔽ESC键退出
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == 0x1b)
return TRUE;
//回车
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == 0x0d )
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
或
BOOL CPictureDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_RETURN)
return TRUE;
if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==VK_ESCAPE)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}

本文介绍如何在MFC中通过重写PreTranslateMessage函数来阻止默认的回车键和ESC键关闭对话框的行为。提供了具体的代码实现,帮助开发者在MFC应用程序中自定义对话框的按键响应。
3058

被折叠的 条评论
为什么被折叠?



