Preventing ESC to kill a dialog box
How would one prevent a user from hitting the Escape key to kill the dialog box. In my code, I have a timer function and while its running I want to prevent the dialog box from suddenly closing via the escape key...I want to disable this function. When the escape key is hit...does it call WM_CLOSE? I have a boolean value associated with the Timer caled m_bTimerOn (indicates if the timer is on) and I tried inserting a return statement if m_bTimerOn == TRUE...and this didn't work.
can anyone help,
thanks,
-raizada
[539 byte] By [
Raizada] at [2007-11-17 8:55:25]

# 1 Re: Preventing ESC to kill a dialog box
If I remember correctly ESC bypasses the OnClose()
and goes to DestroyWindow from which you can return false to prevent it, or place cleaning up code.
# 2 Re: Preventing ESC to kill a dialog box
Overrid OnClose() and leave it empty.
void CMyDialog::OnClose()
{
}
Gabriel.
_
Forever trusting who we are
And nothing else matters - Metallica
_
# 3 Re: Preventing ESC to kill a dialog box
First of all, thanks for the quick reply and now the problem.
Problem: it seems that when OnClose() is called, it also calls DestroyWindow in the process. Because whenever I close the dialog, an "exception breakpoint" error appears. This error also appears when I hit escape. But you are on the right track, also how can I prevent ALT-F4 from closing the dialog box.
thanks so much,
-raizada
# 4 Re: Preventing ESC to kill a dialog box
Alt+F4 also goes to DestroyWindow.
You could also use PreTranlateWindow in the dialog class to detect these messages and just 'swallow' them there. If they are not dispatched for further processing nothing will happen. That should also prevent the exceptios.
# 5 Re: Preventing ESC to kill a dialog box
I tried looking up information on PreTranlateWindow and can not find anything...Do you mean PreTranslateMessage.
If PreTranslateMessage is what you mean, I don't see how to use it in my code. What message is going to be passed as pMsg...Or could you possibly direct me to a good website that can explain everything for me. I am recently new to MFC so I do not know all the tricks yet,
I really appreciate your help,
-raizada
# 6 Re: Preventing ESC to kill a dialog box
1. Overriding 'PreTranslateMessage()'
BOOL CYourDialog::PreTranslateMessage(MSG* pMsg)
{
// ENTER key
if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
return TRUE;
// ESC key
if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_ESCAPE))
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
2. Overriding 'OnOK()' or 'OnCancel()'
// ENTER key
void CYourDialog::OnOK()
{
// CDialog::OnOK(); <-- Disable this line
}
// ESC key
void CYourDialog::OnCancel()
{
// CDialog::OnCancel(); <-- Disable this line
}
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
# 7 Re: Preventing ESC to kill a dialog box
Ooops yes I meant PreTranslateMessage.
All messages get passed through there.
# 8 Re: Preventing ESC to kill a dialog box
Thanks for your advice, but it doesn't go with my code. My CMyDlg::OnOk has a function to SetTimer() and to assign value to variables and CMyDlg::OnClose has a function to close the dlg only if m_bTimerOn is FALSE, otherwise button is diabled. I want to prevent the user from closing the dlgbox through "Esc" or "ALT+F4". I belive that the code needs to be defined in the CMyDlg::DestroyWindow() handler. I attached a snippet of my code:
BOOL CTimedUpdates::DestroyWindow()
{
if(m_bTimerOn)
return FALSE;
else
return CDialog::DestroyWindow();
}
This, however, gives me an application error (exception breakpoint) when the dlg is abruptly closed(via escape or alt F4) when the m_bTimerOn=TRUE.
Thanks for any help,
-raizada