1.1.1 Generating and Processing Messages

Windows generates a message at each input event, such as when the user moves the mouse or presses a keyboard key. Windows collects these input messages in a system-wide queue and then places these messages, as well as timer and paint messages, in an application's queue. The application queues are first-in/first-out queues that belong to individual applications; however, timer and paint messages are held in the queue until the application has processed all other messages. Windows places messages that belong to a specific application in that application's queue. The application then reads the messages by using the GetMessage function and dispatches them to the appropriate window function by using the DispatchMessage function.

Windows sends some messages directly to an application's window function, without placing them in the application queue. Such messages are called unqueued messages. In general, an unqueued message is any message that affects the window only. The SendMessage function sends messages directly to a window.

For example, the CreateWindow function directs Windows to send a WM_CREATE message to the window function of the application and to wait until the message has been processed by the window function. Windows sends this message directly to the function and does not place it in the application queue.

Although most messages are generated by Windows, applications can create their own messages and place them in the application queues of other applications.

An application can pull messages from its queue by using the GetMessage function. This function searches the application queue for messages and, if a message exists, returns the top message in the application queue. If the application 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 a main function has a message from a queue, it can dispatch the message to a window function by using the DispatchMessage function. This function directs Windows to call the window function of the window associated with the message, and then passes the content of the message as function arguments. The window function can then process the message and carry out any requested changes to the window. When the window function returns, Windows returns control to the main function. The main function can then pull 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 virtual-key message each time the user presses a keyboard key. The virtual-key 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 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 queue. The message can then be dispatched to a window function.