VC++

How to prevent CDialog closing on pressing Escape key

Posted by amit on November 19, 2008 at 12:11 am

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 [...]