MFC OnClose() behaviour
-
Hello guys, I'm wondering if anyone could help me to better undestand the exact
OnClose()
behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()
) to a parent dialog's function, which leadsOnClose()
, the function looks like:LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam) { OnClose(); return 0; }
I need to do some clean-up before the application is closed, so I override the OnClose() function:
void ParentDlg::OnClose() { DoSomeCleanUp(); CDialog::OnClose(); // call base class handler }
MSDN says
OnClose()
that its default implementation calls DestroyWindow. So I call the base class handlerCDialog::OnClose()
at the end of overridenOnClose()
and assumes that should callDestroyWindow()
, which leads toParentDlg::OnDestroy()
I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough,OnClose()
is called and it goes toOnDestroy()
and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes throughOnExitApp()
,OnClose()
, thenOnDestroy()
never gets called after and the program will not exit. Why is the behaviour different when both callCDialog::OnClose()
eventually? I later simply insert the codeDestroyWindow();
to theOnClose()
function and that made both callOnDestroy()
and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ Thanks -
Hello guys, I'm wondering if anyone could help me to better undestand the exact
OnClose()
behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()
) to a parent dialog's function, which leadsOnClose()
, the function looks like:LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam) { OnClose(); return 0; }
I need to do some clean-up before the application is closed, so I override the OnClose() function:
void ParentDlg::OnClose() { DoSomeCleanUp(); CDialog::OnClose(); // call base class handler }
MSDN says
OnClose()
that its default implementation calls DestroyWindow. So I call the base class handlerCDialog::OnClose()
at the end of overridenOnClose()
and assumes that should callDestroyWindow()
, which leads toParentDlg::OnDestroy()
I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough,OnClose()
is called and it goes toOnDestroy()
and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes throughOnExitApp()
,OnClose()
, thenOnDestroy()
never gets called after and the program will not exit. Why is the behaviour different when both callCDialog::OnClose()
eventually? I later simply insert the codeDestroyWindow();
to theOnClose()
function and that made both callOnDestroy()
and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ ThanksHi, When you push the X button in an app, some messages are send to your app. One of those messages is the WM_DESTROY message, which doesn't get send when you push the Exit button in your dialog. One solution is (only when it is a modal dialog) to use the OnOK (or OnCancel) message handler. This will terminate the dialog and return the DoModal funtion will return a value. Hope this helps.
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Hello guys, I'm wondering if anyone could help me to better undestand the exact
OnClose()
behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()
) to a parent dialog's function, which leadsOnClose()
, the function looks like:LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam) { OnClose(); return 0; }
I need to do some clean-up before the application is closed, so I override the OnClose() function:
void ParentDlg::OnClose() { DoSomeCleanUp(); CDialog::OnClose(); // call base class handler }
MSDN says
OnClose()
that its default implementation calls DestroyWindow. So I call the base class handlerCDialog::OnClose()
at the end of overridenOnClose()
and assumes that should callDestroyWindow()
, which leads toParentDlg::OnDestroy()
I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough,OnClose()
is called and it goes toOnDestroy()
and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes throughOnExitApp()
,OnClose()
, thenOnDestroy()
never gets called after and the program will not exit. Why is the behaviour different when both callCDialog::OnClose()
eventually? I later simply insert the codeDestroyWindow();
to theOnClose()
function and that made both callOnDestroy()
and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ ThanksLRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam){ OnClose(); return 0; } Is why. Change it to LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam){ SendMessage(WM_CLOSE); return 0; } When CDialog::OnClose gets called, it calls MFC's Default() method - which says hay what was the last message? Lets pass that to DefWindowProc for processing. If you just _call_ OnClose, the last message will be your custom message you are using to close the dialog - and DefWindowProc will do nowt. Better yet - do away with your custom message and just send the dialog a WM_CLOSE from the other dialog.
-
Hello guys, I'm wondering if anyone could help me to better undestand the exact
OnClose()
behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()
) to a parent dialog's function, which leadsOnClose()
, the function looks like:LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam) { OnClose(); return 0; }
I need to do some clean-up before the application is closed, so I override the OnClose() function:
void ParentDlg::OnClose() { DoSomeCleanUp(); CDialog::OnClose(); // call base class handler }
MSDN says
OnClose()
that its default implementation calls DestroyWindow. So I call the base class handlerCDialog::OnClose()
at the end of overridenOnClose()
and assumes that should callDestroyWindow()
, which leads toParentDlg::OnDestroy()
I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough,OnClose()
is called and it goes toOnDestroy()
and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes throughOnExitApp()
,OnClose()
, thenOnDestroy()
never gets called after and the program will not exit. Why is the behaviour different when both callCDialog::OnClose()
eventually? I later simply insert the codeDestroyWindow();
to theOnClose()
function and that made both callOnDestroy()
and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ ThanksJ.B. wrote: I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). Not strange at all. This is the expected return value when the "X" is clicked. If this is a modal dialog, look at the
EndDialog()
function. It gets called whenever a modal dialog is dismissed. Alt+F4, Cancel, and the "X" all effectively callEndDialog(2)
. This value, in turn, gets passed back throughDoModal()
. Had you dismissed the dialog using the OK button,EndDialog(1)
would have been called instead.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
J.B. wrote: I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). Not strange at all. This is the expected return value when the "X" is clicked. If this is a modal dialog, look at the
EndDialog()
function. It gets called whenever a modal dialog is dismissed. Alt+F4, Cancel, and the "X" all effectively callEndDialog(2)
. This value, in turn, gets passed back throughDoModal()
. Had you dismissed the dialog using the OK button,EndDialog(1)
would have been called instead.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen