简体   繁体   中英

How to close the main window in MFC dialog-based application

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.

Try this:

ASSERT( AfxGetMainWnd()!=NULL );
AfxGetMainWnd()->SendMessage(WM_CLOSE);

You can send WM_CLOSE to your dialog hwnd. PostQuitMessage should also allow to end application execution.

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() ); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM