以编程方式退出 MFC 应用程序的正确方法是什么?

2022-01-12 00:00:00 exit c++ mfc quit

使用 windows MFC C++.我有一个第三方应用程序在我的 CWinApp 派生类中调用用户定义的方法.此方法在 InitInstance() 之后调用.如果此方法中存在错误,例如抛出异常并在 try/catch 块中捕获,我想从 catch 块中退出应用程序.什么是规范且正确的戒烟方式?

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?

更新:

Serge 我认为在 InitInstance() 中返回 false 是正确的退出应用程序的方法.但是,现在假设我想从 CDialog 派生类的 OnInitDialog() 处理程序中退出,那么正确的方法是什么.

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.

更新 2

对我来说,我发现调用 PostMessage(WM_CLOSE) 是我的非模态 CDialog 派生类的最佳方法.在某些情况下,我尝试过的所有其他退出方法都会引发一些异常.

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;
}

推荐答案

在 InitInstance() 中

InitInstance()中退出应用程序:只需从InitInstance()返回FALSE.

Exiting the app while you are still in InitInstance(): Simply return FALSE from InitInstance().

在主消息循环中

如果您已经处于消息循环中,那就另当别论了:关闭应用程序的标准方法是退出消息循环:

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),顾名思义,发布 WM_QUIT 消息.消息循环的反应是退出循环并关闭程序.

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 将通过 PostQuitMessage() 为您做出反应,因此退出主消息循环并关闭您的应用程序.

MFC will react by PostQuitMessage() for you, hence exit the main message loop and close your app.

更好的是,您应该发布一个 WM_CLOSE 让您的主窗口优雅地关闭.例如,它可能决定保存当前文档.但请注意:标准的 OnClose() 处理程序可能会提示用户保存脏文档.用户甚至可以使用此提示取消关闭操作(保存文档?是、否、取消).

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_DESTROY 消息.MFC 通过调用 PostQuitMessage(0) 做出反应,退出消息泵.(实际上,MFC 在 OnNcDestroy() 中进行调用,因为 WM_NCDESTROY 是窗口接收到的绝对最后一条消息)

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)

基于对话框的应用

Call EndDialog(-1);//或者用 IDCANCEL 替换 -1,随便什么

Call EndDialog(-1); // Or replace -1 by IDCANCEL, whatever

您可能知道,此调用将关闭对话框.

This call, as you probably know, will close the dialog.

请注意,基于对话框的应用程序的主对话框在 InitInstance() 中执行.关闭对话框将简单地退出 InitInstance(),在此类项目中总是返回 FALSE.

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.

相关文章