Windows generates an input message for each input event, such as when the user moves the mouse or presses a key. Windows collects input messages in a systemwide message queue and then places the messages, as well as timer and paint messages, in an application message queue. An application message queue is a first in, first out queue. Timer and paint messages are exceptions to the first in, first out rule; these messages are held in an application's message queue until the application has processed all other messages. Windows places messages that belong to a specific application in that application's message queue. The application then reads the messages by using the GetMessage function and dispatches them to the appropriate window procedure by using the DispatchMessage function.
Windows sends some messages directly to the window procedure in the appropriate application instead of placing the messages in the application's message queue. Such messages are called unqueued messages. Typically, an unqueued message is any message that affects the window only. The SendMessage function sends messages directly to a window procedure. For more information about window procedures, see Section 1.2.13, “Window Procedures.”
For example, the CreateWindow function directs Windows to send a WM_CREATE message to a window procedure of an application and to wait until the window procedure has processed the message. Windows sends this message directly to the window procedure and does not place it in the application's message queue.
Although Windows generates most messages, an application can create its own messages and place them in its own message queue or that of another application.
An application typically uses the GetMessage function in a loop within its WinMain function to remove messages from the application's message queue. This loop is called the main message loop. The GetMessage function searches an application's message queue and, if any messages exist, returns the top message in the queue. If the message queue is empty, GetMessage waits for a message to be placed in the queue. While waiting, GetMessage relinquishes control to Windows, allowing other applications to take control and process their own messages.
Once an application's WinMain function has retrieved a message from the application's message queue, it can dispatch the message to a window procedure by using the DispatchMessage function. This function directs Windows to call the window procedure of the window associated with the message, and then passes the content of the message as function arguments. The window procedure can then process the message and carry out any requested changes to the window. When the window procedure returns, Windows returns control to the main message loop in the WinMain function. The main message loop can then retrieve the next message from the queue.
Note:
Unless noted otherwise, Windows can send messages in any sequence. An application should not rely on receiving messages in a particular order.
Windows generates a message each time the user presses a key. The message contains a virtual-key code that defines which key was pressed, but does not define the character value of that key. To retrieve the character value, the main message loop in the WinMain function must translate the virtual-key message by using the TranslateMessage function. This function puts another message with an appropriate character value in the application's message queue. The message can then be dispatched to a window procedure.