(1) The operating system receives a form message from the application and posts the message to the application's message queue .
(2) The application calls the GetMessage function in the message loop to remove a single message from the message queue.
After the message is removed, the application can pre-process the message, for example, discard the response to some messages, or call translatemessage to generate a new message.
(3) The application calls DispatchMessageand passes the message back to the operating system.
The message is represented by the MSG struct object, which includes a handle to the form that receives the message. Therefore, the DispatchMessage function is always properly passed .
(4) The system uses a pointer to a form procedure function saved by the Lpfnwndproc member of the WNDCLASS struct to invoke the form procedure and process the message ("The system sends a message to the application").
The flowchart is as follows:
Message handling Mechanisms for Windows applications