Can anybody answer how to close the main window in MFC dialog-based application when I want to close the application itself? The application was created in MS VS 2010. Thank you very much in advance.
Eugene.
It depends on what processing you want to happen as part of the shutdown. If all you want is to terminate the message loop without any further processing you can call PostQuitMessage( exitCode )
. The exitCode
will be stored as the process' exit code.
If you want to invoke the OK/Cancel handlers you have to call OnOK()
or OnCancel()
respectively. The default implementation for OnOK()
will try to save and validate the dialog data and upon successful execution call EndDialog( IDOK )
to terminate the message loop. The default implementation for OnCancel()
simply calls EndDialog( IDCANCEL )
. Neither one lets you specify an exit code for your process. If you have overridden either of those member functions you need to call the base class implementation after performing your specialized code, unless you wish to prevent shutdown.
Keep in mind that OnOK()
and OnCancel()
are protected members of CDialog[Ex]
and cannot be directly accessed from outside. If you need to call either one from outside your dialog class you have to override them publicly. You can get a pointer to the main dialog using CMyDialog* pDlg = dynamic_cast< CMyDialog* >( AfxGetMainWnd() );
.