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

I was wondering what is the difference between the WM_QUIT, WM_CLOSE and WM_DESTROY messages in a Windows program, essentially: when they are sent, and do they have any automatic effects other than what the program defines?

+48
c ++ windows messages
Jul 01 '10 at 7:24
source share
4 answers

They are completely different.

WM_CLOSE sent to the window when you click "X" or "Close" is selected from the window menu. If you catch this message, it is your call how to handle it - ignore it or really close the window. By default, WM_CLOSE passed to DefWindowProc causes the window to WM_CLOSE . When the window is destroyed, a WM_DESTROY message is sent. At this stage, unlike WM_CLOSE , you cannot stop the process, you can only do the necessary cleanup. But remember that when you catch WM_DESTROY just before all child windows are already destroyed. WM_NCDESTROY sent immediately after the destruction of all child windows.

WM_QUIT message is not associated with any windows (the hwnd value obtained from GetMessage is NULL, and the window procedure is not called). This message indicates that the message loop should be stopped and the application should be closed. When GetMessage reads WM_QUIT , it returns 0 to indicate this. Look at a typical fragment of a message loop - the loop continues, and GetMessage returns a nonzero value. WM_QUIT can be sent by the PostQuitMessage function. This function is usually called when the main window receives WM_DESTROY (see a typical fragment of a window procedure ).

+65
Jul 01 '10 at 7:40
source share
β€” -

First of all, WM_CLOSE and WM_DESTROY messages are associated with specific windows, while the WM_QUIT message is applicable to the entire application (well flow), and the message is never received through the procedure window ( WndProc ), but only through the GetMessage or PeekMessage .

In your WndProc routine, the DefWindowProc function defaults to the behavior of these messages. WM_CLOSE reports that the application should close, and the default behavior for this is to call the DestroyWindow function. Its when this DestroyWindow function DestroyWindow called that the WM_DESTROY message is sent. Please note that WM_CLOSE is only a message asking to close (for example WM_QUIT ) - you really do not need to exit / exit. But the WM_DESTROY message says that your IS window is being closed and destroyed, so you must clear any resources, process, etc.

+9
Jul 01 '10 at 7:41
source share

First we’ll discuss WM_QUIT β€” unlike other posts that aren’t connected to the window. It is used by the application. For example, this can be handled by an invisible stand-alone OLE server (.exe, but not in-proc like .dll)

WM_CLOSE - for msdn: "The application can ask the user for confirmation before the window is destroyed" - it is used as a notification of the intention to close (you can reject this intention).

WM_DESTROY - the fact that the window is closing and all resources must (!) Be freed.

+3
Jul 01 2018-10-10T00:
source share

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

However, if you just type the ESC key, this will close the dialog box, but (as far as I can tell) without generating the WM_CLOSE event - it goes directly to the WM_CANCEL/OnCancel() mechanism.

I hereby invite the community to sort this out ... or edit this development in the accepted answer.

+3
Jun 10 '15 at 23:21
source share



All Articles