Taking a default MFC based single dialog appplication, I’m trying to prevent the default behaviour where the Escape or Enter key will cause the dialog to close.
However whilst I’m able to acheive this, I’ve discovered that the application exit code is now always returns 20, regardless of the value I pass to EndDialog.
In the below test I’ve prevented the Escape key:
BEGIN_MESSAGE_MAP(CMFCApplication10Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDCANCEL, &CMFCApplication10Dlg::OnBnClickedCancel) // Triggers when Escape key pressed
ON_WM_CLOSE() // Added WM_CLOSE to handle the close
END_MESSAGE_MAP()
OnBnClickedCancel occurs when pressing Escape, so to prevent the application closing I do not call OnCancel
void CMFCApplication10Dlg::OnBnClickedCancel()
{
//CDialogEx::OnCancel(); // Not calling OnCancel
}
I’ve added the OnClose to allow the dialog to still be closed using the top right Close button
void CMFCApplication10Dlg::OnClose()
{
CDialogEx::EndDialog(0); // Call the EndDialog with 0
}
How can I disable the escape key and also return an exit code of 0
Expecting: An exit code of 0
Actual: Getting an exit code of 20