Reputation: 612
I know one method of preventing an MFC dialog from closing when the Enter or Esc keys are pressed, but I'd like to know more details of the process and all the common alternative methods for doing so.
Thanks in advance for any help.
Upvotes: 30
Views: 35030
Reputation: 98358
When the user presses Enter key in a dialog two things can happen:
CDialog::SetDefID()
). Then a WM_COMMAND
with the ID of this control is sent to the dialog.WM_COMMAND
with ID = IDOK is sent to the dialog.With the first option, it may happen that the default control has a ID equal to IDOK. Then the results will be the same that in the second option.
By default, class CDialog
has a handler for the WM_COMMAND(IDOK)
that is to call to CDialog::OnOk()
, that is a virtual function, and by default it calls EndDialog(IDOK)
that closes the dialog.
So, if you want to avoid the dialog being closed, do one of the following.
IDOK
.WM_COMMAND(IDOK)
that does not call EndDialog()
.CDialog::OnOk()
and do not call the base implementation.About IDCANCEL, it is similar but there is not equivalent SetDefID()
and the ESC key is hardcoded. So to avoid the dialog being closed:
WM_COMMAND(IDCANCEL)
that does not call EndDialog()
.CDialog::OnCancel()
and do not call the base implementation.Upvotes: 43
Reputation: 51
When dealing with Dialog style MFC applications, the framework automatically hard codes a few items that must be overridden to prevent the application from quitting when the Esc or Enter keys are pressed. But there is a very simple way that doesn't require anything special such as implementing PreTranslateMessage() which is very much not recommend.
There are three functions that need to be in place:
In the header, add the three function prototypes. You can use the Class Wizard if you like to add the WM_CLOSE event handler but it's super simple to just type it in.
// DefaultDialogAppDlg.h
//
class CDefaultDialogAppDlg : public CDialogEx
{
// ... other code
protected:
virtual void OnCancel(){} // inline empty function
virtual void OnOK(){} // inline empty function
public:
afx_msg void OnClose(); // message handler for WM_CLOSE
// ...other code
};
In the .cpp file, add the ON_WM_CLOSE() entry to the message map and the definitions for the three functions. Since OnCancel() and OnOK() are generally going to be empty, you could just inline them in the header if you want (see what I did in Step 1?).
The .cpp file will have something like this:
// DefaultDialogAppDlg.cpp
// ... other code
BEGIN_MESSAGE_MAP(CDefaultDialogAppDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE() // WM_CLOSE messages are handled here.
END_MESSAGE_MAP()
// ... other code
void CDefaultDialogAppDlg::OnClose()
{
// TODO: Add exit handling code here
// NOTE: to actually allow the program to end, call the base class
// version of either the OnOK() or OnCancel() function.
//CDialogEx::OnOK(); // returns 1 to theApp object
CDialogEx::OnCancel(); // returns 2 to theApp object
}
Upvotes: 5
Reputation: 126
The answer of @the-forest-and-the-trees is quite good. Except one situation which was addressed by @oneworld. You need to filter messages which are not for dialog window:
BOOL CDialogDemoDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->hwnd == this->m_hWnd && pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
return TRUE; // Do not process further
}
}
return CWnd::PreTranslateMessage(pMsg);
}
Remember to add virtual
in the header file.
Upvotes: 5
Reputation: 33
Make sure you don't #define CUSTOM_ID 2
because 2
is already defined for escape and I think 1
is defined for enter? Correct me if i'm wrong.
Upvotes: 0
Reputation:
I simply override the OnOk event and instead of passing the message to the parent dialog, do nothing.
So it's basically simple as doing so:
void OnOk() override { /*CDialog::OnOK();*/ }
This should prevent the dialog from closing when pressing the return/enter key.
Upvotes: 2
Reputation: 1856
There is an alternative to the previous answer, which is useful if you wish to still have an OK / Close button. If you override the PreTranslateMessage function, you can catch the use of VK_ESCAPE / VK_RETURN like so:
BOOL MyCtrl::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message == WM_KEYDOWN )
{
if(pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
return TRUE; // Do not process further
}
}
return CWnd::PreTranslateMessage(pMsg);
}
Upvotes: 37