How to prevent CDialog closing on pressing Escape key

MFC CDialog classes have a default behaviour of closing if the user presses Esc key anytime while the dialog has focus. To overcome this behaviour, the Esc key has to be trapped and disabled in the PreTranslateMessage event handler:


BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
// disable closing by escape key
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*