When Dialog boxes in MFC are created using the resource editor, they will have two default Command Buttons. One with caption as OK and other one with CANCEL. When a class is created, both these buttons will be created with default handlers. When any one of them is clicked, they will close the dialog. Just removing/renaming these buttons will be enough to change their behavior.
There are two key press events which produce a similar result. By default when a dialog is created, the ENTER and ESCAPE keys are considered as shortcuts for the OK and CANCEL buttons. When any of these keys are pressed, the dialog box will be closed.
To avoid this, the default behavior of the dialog should be modified. The incoming ENTER and ESCAPE key down messages are to be nullified, inside the PreTranslateMessage function. The following is the sample code required for this purpose.
BOOL DialogName::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message==WM_KEYDOWN) { if(pMsg->wParam==VK_RETURN || pMsg->wParam==VK_ESCAPE) pMsg->wParam=NULL ; } return CDialog::PreTranslateMessage(pMsg); }
If this code is used in PreTranslateMessage, all the ENTER and ESCAPE messages will be nullified and the dialog will not be closed when these keys are pressed.