Wm_close, Wm_destroy, wm_quit and various message delivery functions in detail _c language

Source: Internet
Author: User
Tags getmessage message queue

This paper analyzes the functions and differences of WM_CLOSE, Wm_destroy, wm_quit and various message delivery functions, which helps readers to understand the message delivery function better. Details are as follows:

First, Wm_close, Wm_destroy, wm_quit difference

Wm_close: Close the application window

Wm_destroy: Shutting down applications

Wm_quit: Turn off message loops

The application's process actually exits (disappears in the Task Manager) only if the message loop is closed.

Win32 application's complete exit process: Click the Close button in the upper-right corner of the window to send a WM_CLOSE message. The DestroyWindow function is invoked in this message processing to send a WM_DESTROY message. The PostQuitMessage (0) function is invoked in this message processing to send wm_quit messages to the message queue. GetMessage captures the Wm_quit, returns 0, exits the loop (the application actually exits).

Note: according to the above normal process, wm_quit will not reach the window process. (because after the getmessage intercepted the Wm_quit message, the program has completely exited!) )

Full exit procedure for MFC applications: Click the Close button in the upper-right corner of the window, or select "File/close" to send a WM_CLOSE message. Cmyframewnd does not set the Wm_close handler, so it is given to the preset handler. The default function for Wm_close is to call::D Estroywindow, and thus emit Wm_destroy. The default Wm_destroy processing is the call::P ostquitmessage, so the Wm_quit is issued. CWinApp::Run receives wm_quit and ends its internal message loop, then calls ExitInstance, which is a virtual function of CWinApp. If your application class CMyWinApp rewrite ExitInstance, then CWinApp::Run call is cmywinapp::exitinstance, otherwise it is cwinapp::exitinstance. Finally go back to Afxwinmain, execute afxwinterm, and end the program.

Attach: When the DestroyWindow function is called, the operating system makes a series of deletions, sends the WM_DESTROY message first, and then sends the WM_NCDESTROY message. If the window also has a child window or the owner of another window, you need to send a delete message to all child windows.
Wm_quit is the only message that allows GetMessage (&msg,null,0,0) to return a false value.
Related Code Analysis:

The code fragment that enters the message loop in the main function while
(GetMessage (&msg,null,0,0))
{
translatemessage (&msg);//To process the message
DispatchMessage (&msg);//Then pass the message variable MSG to Windows so that Windows can invoke the message handler function
}

If you change GetMessage (&msg,null,0,0) to GetMessage (&msg,hwnd,0,0), you find that the program is still in the Task manager's process and consumes a lot of memory after the application is closed, why?

The reason for this in MSDN is that for GetMessage (&msg,hwnd,0,0), when the second argument is invalid, this function returns a value of-1. For the above loop, this while condition is true and therefore enters the dead loop, and the process cannot exit.

II. various Message delivery functions

1, SendMessage: Sends a message to the specified window procedure, and returns until the window process has processed the message.

2, PostMessage: Place messages in Message Queuing (related to threads created by the specified window); Do not wait for message processing and return immediately.
   cannot send a wm_quit message, which can only be sent by the PostQuitMessage function.

3, PostThreadMessage: Sends a message to the specified thread's message queue, and returns immediately without waiting for the thread to process the message.
   The message sent by this function has nothing to do with the window. We just need to specify the thread ID to OK, but we will fail to ensure that the thread is created.

4, GetMessage: Messages are fetched from the message queue of the calling thread.
   When the second argument is NULL, it retrieves the following two messages:
     A, a message that belongs to any window of the calling thread;
      B, a message posted by POSTTHREADMESSAG to the calling thread.

5, PeekMessage: Features with GetMessage. The difference is:
   GetMessage: Until a message matches the filter condition, it is placed in the message queue before returning.
   PeekMessage: Returns immediately, regardless of whether the message is queued.

6, DispatchMessage: Dispatch a message to the appropriate window procedure.

7, TranslateMessage: Converts virtual key information to character messages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.