Sunday, February 1, 2009

[VC++] Preventing your dialog disappear on ESC/Enter key press

If you are new to MFC dialog applications one thing you find irritating will be your application getting close on pressing ESC key or Enter key ! For preventing this what you need to do is over ride virtual OnOK() and OnCancel() in your dialog class.

Add below code to your dialog class(say CMyTestDlg) header file for overriding OnOK() and OnCancel(),
virtual void OnOK();
virtual void OnCancel();


Add empty implementation for the declared functions,
void CMyTestDlg::OnOK()
{
}
void CMyTestDlg::OnCancel()

{
}

Then its done ! You don't need to worry any more about your dialog application disappearing on pressing ESC or Enter key.

No comments: