C++ windows程序中的WM_QUIT、WM_CLOSE、WM_DESTROY有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3155782/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:13:08  来源:igfitidea点击:

What is the difference between WM_QUIT, WM_CLOSE, and WM_DESTROY in a windows program?

c++windowsmessages

提问by adf88

I was wondering what the difference between the WM_QUIT, WM_CLOSE, and WM_DESTROY messages in a windows program, essentially: when are they sent, and do they have any automatic effects besides what's defined by the program?

我想知道 Windows 程序中的 WM_QUIT、WM_CLOSE 和 WM_DESTROY 消息之间有什么区别,本质上是:它们何时发送,除了程序定义的内容之外,它们是否有任何自动效果?

回答by adf88

They are totally different.

他们是完全不同的。

WM_CLOSEis sent to the window when "X" is pressed or "Close" is chosen from window menu. If you catch this message this is your call how to treat it - ignore it or really close the window. By default, WM_CLOSEpassed to DefWindowProccauses window to be destroyed. When the window is being destroyed WM_DESTROYmessage is sent. In this stage, in opposition toWM_CLOSE, you cannot stop the process, you can only make a necessary cleanup. But remember that when you catch WM_DESTROYjust before all child windows are already destroyed. WM_NCDESTROYis send just after all child windows have been destroyed.

WM_CLOSE当按下“X”或从窗口菜单中选择“关闭”时发送到窗口。如果您收到此消息,这就是您如何处理它的电话 - 忽略它或真正关闭窗口。默认情况下,WM_CLOSE传递给DefWindowProc导致窗口被销毁。当窗口被销毁时WM_DESTROY发送消息。在这个阶段,与 相反WM_CLOSE,您不能停止该过程,您只能进行必要的清理。但是请记住,当您WM_DESTROY在所有子窗口已经被销毁之前捕获时。WM_NCDESTROY在所有子窗口都被销毁后发送。

WM_QUITmessage is not related to any window (the hwndgot from GetMessageis NULL and no window procedure is called). This message indicates that the message loop should be stopped and application should be closed. When GetMessagereads WM_QUITit returns 0 to indicate that. Take a look at typical message loop snippet- the loop is continued while GetMessagereturns non-zero. WM_QUITcan be sent by PostQuitMessagefunction. This function is usually called when main window receives WM_DESTROY(see typical window procedure snippet).

WM_QUIT消息与任何窗口无关(hwndGetMessageNULL 中得到,并且没有调用任何窗口过程)。此消息表示应停止消息循环并关闭应用程序。当GetMessage读取WM_QUIT返回0来表示。看看典型的消息循环片段- 循环继续,同时GetMessage返回非零值。WM_QUIT可以通过PostQuitMessage函数发送。这个函数通常在主窗口接收时调用WM_DESTROY(参见典型的窗口过程片段)。

回答by user353297

First of all, the WM_CLOSEand WM_DESTROYmessages are associated with particular windows whereas the WM_QUITmessage is applicable to the whole application (well thread) and the message is never received through a window procedure (WndProcroutine), but only through the GetMessageor PeekMessagefunctions.

首先,WM_CLOSEWM_DESTROY消息与特定窗口相关联,而WM_QUIT消息适用于整个应用程序(井线程)并且该消息永远不会通过窗口过程(WndProc例程)接收,而只能通过GetMessagePeekMessage函数接收。

In your WndProcroutine the DefWindowProcfunction takes care of the default behavtheitroad of these messages. The WM_CLOSEmessages requests that the application should close and the default behavtheitroad for this is to call the DestroyWindowfunction. Its when this DestroyWindowfunction is called that the WM_DESTROYmessage is sent. Notice that the WM_CLOSEis only a message requesting that you close (like WM_QUIT) - you don't actually have to exit/quit. But the WM_DESTROYmessage tells you that your window ISbeing closed and destroyed so you must cleanup any resources, handles etc.

在您的WndProc例程中,该DefWindowProc函数负责处理这些消息的默认行为。该WM_CLOSE消息请求,应用程序应该关闭,为此默认behavtheitroad是调用DestroyWindow函数。当这个DestroyWindow函数被调用时,WM_DESTROY消息被发送。请注意,WM_CLOSE只是一条要求您关闭的消息(如WM_QUIT)-您实际上不必退出/退出。但是WM_DESTROY消息告诉你,你的窗口IS关闭和破坏,所以你必须清除任何资源,把手等。

回答by omatai

Just so it doesn't get lost in the comments... don't forget about WM_CANCEL. When you click the close (x) button on an MFC dialog, it will certainly send WM_CLOSE. The default OnClose()function will then call the default (base class) OnCancel()function.

只是为了不会在评论中迷失...不要忘记WM_CANCEL. 当您单击 MFC 对话框上的关闭 (x) 按钮时,它肯定会发送WM_CLOSE. 然后默认OnClose()函数将调用默认(基类)OnCancel()函数。

However, if you simply type the ESCkey, this will lead to the closure of the dialog, but (as far as I can tell) without generating the WM_CLOSEevent - it goes directly to the WM_CANCEL/OnCancel()mechanism.

但是,如果您只是键入ESC键,这将导致对话框关闭,但是(据我所知)不会生成WM_CLOSE事件 - 它直接进入WM_CANCEL/OnCancel()机制。

I hereby invite the community to elaborate on this... or edit that elaboration into the accepted answer.

我特此邀请社区对此进行详细说明……或将该详细说明编辑为已接受的答案。

回答by Dewfy

At first let's discuss WM_QUIT - the difference from another messages that this is not associated with window. It is used by application. For example this can be handled by non-visible standalone OLE server (.exe, but not in-proc as .dll)

首先让我们讨论 WM_QUIT - 与其他消息的区别,这与窗口无关。它由应用程序使用。例如,这可以由不可见的独立 OLE 服务器处理(.exe,但不能作为 .dll 在进程内处理)

WM_CLOSE - per msdn: "An application can prompt the user for confirmation, prior to destroying a window" - it is used as notification about intention to close (you can reject this intention).

WM_CLOSE - 根据 msdn:“应用程序可以在销毁窗口之前提示用户进行确认” - 它用作有关关闭意图的通知(您可以拒绝此意图)。

WM_DESTROY - is a fact that window is closing and all resources must(!) be deallocated.

WM_DESTROY - 窗口正在关闭并且必须(!)释放所有资源的事实。