问题
Using windows MFC C++. I have a third party app that calls a user-defined method in my CWinApp derived class. This method is called after InitInstance(). If there is an error in this method, such that an exception is thrown and caught in a try/catch block, I would like to exit the application from the catch block. What is the canonical and correct way to quit?
UPDATE:
Serge I believe is right that in InitInstance() returning false is the correct way to quit the application. However, now suppose I want to quit from a CDialog derived class's OnInitDialog() handler, what's the correct way to do that.
UPDATE 2
For me, I found calling PostMessage(WM_CLOSE) to be the best way from my non-modal CDialog derived class. All other methods of quitting I tried would raise some exception or other in some circumstances.
Here's an example of how I use it:
BOOL SomeDialog::OnInitDialog()
{
CDialog::OnInitDialog();
::OleInitialize(nullptr);
try
{
// ...load settings file here
}
catch(...)
{
PostMessage(WM_CLOSE);
return TRUE;
}
// return TRUE unless you set the focus to a control
return TRUE;
}
回答1:
In InitInstance()
Exiting the app while you are still in InitInstance()
: Simply return FALSE
from InitInstance()
.
In the main message loop
It's another story though if you are already in the message loop: The standard way to close an app is to exit the message loop:
PostQuitMessage(0)
, as its name implies, posts a WM_QUIT
message. The message loop reacts by exiting the loop and closing the program.
But you shouldn't simply do that: You should close the opened windows in your app. Assuming you have only your main window, you should destroy it by calling
m_pMainWindow->DestroyWindow();
MFC will react by PostQuitMessage()
for you, hence exit the main message loop and close your app.
Better yet, you should post a WM_CLOSE
to let your main window close gracefully. It may for example decide to save the current document. Beware though: the standard OnClose()
handler may prompt user to save dirty documents. User can even cancel the close action using this prompt (Save document? Yes, No, Cancel).
Destroying the main window will post a WM_DESTROY
message to it. MFC reacts by calling PostQuitMessage(0)
to exit the message pump. (Actually, MFC does the call in OnNcDestroy()
since WM_NCDESTROY
which is the absolute last mesage received by a window)
Dialog-based app
Call EndDialog(-1);
// Or replace -1 by IDCANCEL
, whatever
This call, as you probably know, will close the dialog.
Note that the main dialog of dialog-based app executes in InitInstance(). Closing the dialog will simply exit InitInstance()
, which always returns FALSE
in such projects.
回答2:
Simply use:
PostQuitMessage(0);
Keep in mind your program won't quit instantly from this call, the window/program will receive a WM_QUIT message and then your program will quit.
回答3:
Serge - your answer is unfortunately not the best way to do it. PostQuitMessage(0) is the way to go and MFC will destroy the windows for you. You should avoid calling m_pMainWindow->DestroyWindow() directly.
来源:https://stackoverflow.com/questions/7562335/what-is-the-correct-way-to-programmatically-quit-an-mfc-application