C++ 以编程方式退出 MFC 应用程序的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7562335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to programmatically quit an MFC application?
提问by User
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?
使用 Windows MFC C++。我有一个第三方应用程序,它在我的 CWinApp 派生类中调用用户定义的方法。该方法在 InitInstance() 之后调用。如果此方法中存在错误,从而引发异常并在 try/catch 块中捕获,我想从 catch 块中退出应用程序。什么是规范和正确的戒烟方式?
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.
Serge 我认为在 InitInstance() 中返回 false 是退出应用程序的正确方法是正确的。但是,现在假设我想退出 CDialog 派生类的 OnInitDialog() 处理程序,那么正确的方法是什么。
UPDATE 2
更新 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.
对我来说,我发现调用 PostMessage(WM_CLOSE) 是我的非模态 CDialog 派生类的最佳方法。在某些情况下,我尝试过的所有其他退出方法都会引发一些异常或其他异常。
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;
}
回答by Serge Wautier
In InitInstance()
在 InitInstance()
Exiting the app while you are still in InitInstance()
: Simply return FALSE
from InitInstance()
.
在您仍在时退出应用程序InitInstance()
:只需FALSE
从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.
PostQuitMessage(0)
,顾名思义,发布WM_QUIT
消息。消息循环的反应是退出循环并关闭程序。
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.
MFC 将为PostQuitMessage()
您做出反应,因此退出主消息循环并关闭您的应用程序。
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).
更好的是,你应该发布一个WM_CLOSE
让你的主窗口优雅地关闭。例如,它可以决定保存当前文档。但要注意:标准OnClose()
处理程序可能会提示用户保存脏文档。用户甚至可以使用此提示取消关闭操作(保存文档?是、否、取消)。
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)
销毁主窗口将WM_DESTROY
向其发布一条消息。MFC 通过调用PostQuitMessage(0)
退出消息泵来做出反应。(实际上,MFC 会调用 inOnNcDestroy()
因为WM_NCDESTROY
这是窗口收到的绝对最后一条消息)
Dialog-based app
基于对话框的应用程序
Call EndDialog(-1);
// Or replace -1 by IDCANCEL
, whatever
调用EndDialog(-1);
// 或者用 替换 -1 IDCANCEL
,随便
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.
请注意,基于对话框的应用程序的主对话框在 InitInstance() 中执行。关闭对话框将简单地 exit InitInstance()
,它总是FALSE
在此类项目中返回。
回答by leetNightshade
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.
请记住,您的程序不会从此调用立即退出,窗口/程序将收到 WM_QUIT 消息,然后您的程序将退出。
回答by zar
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.
Serge - 不幸的是,您的回答不是最好的方法。PostQuitMessage(0) 是要走的路,MFC 会为您销毁窗口。您应该避免直接调用 m_pMainWindow->DestroyWindow()。