什么是以编程方式退出MFC应用程序的正确方法? [英] What is the correct way to programmatically quit an MFC application?

查看:268
本文介绍了什么是以编程方式退出MFC应用程序的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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.

UPDATE 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 )

In InitInstance()

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

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

更棒的是,您应该发布一个 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)

基于对话框的应用程序

调用 EndDialog -1); //或用 IDCANCEL 替换-1,无论

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

请注意,基于对话框的应用程序的主对话框在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.

这篇关于什么是以编程方式退出MFC应用程序的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆