Since an application receives input through its application queue, the chief feature of any Windows application is its message loop. The message loop retrieves input messages from the application queue and dispatches them to the appropriate windows.
The following figure shows how Windows and applications collaborate to process keyboard-input messages. Windows receives keyboard input when the user presses and releases a key. Windows then copies the keyboard-input messages from the system queue to the appropriate application queue. The message loop retrieves the keyboard-input messages, translates them into the Windows character message WM_CHAR, and dispatches the WM_CHAR message, as well as the keyboard-input messages, to the appropriate window procedure. The window procedure then uses the TextOut function to display the character in the client area of the window.
Windows can receive and distribute input messages for several applications at the same time. As shown in the following figure, Windows collects input messages in its system queue and then copies each message to the appropriate application queue. Again, the message loop in each application retrieves messages and dispatches them, through Windows, to each application's appropriate window procedure.
Keyboard-input messages must be retrieved by an application from its message queue. In contrast, window-management messages are sent directly by Windows to the appropriate window procedure. The following figure illustrates this process. After Windows carries out a request to destroy a window, it sends a WM_DESTROY message directly to the window procedure, bypassing the application queue. The window procedure must then signal the main function that the window is destroyed and that the application should terminate. It does this by copying a WM_QUIT message into the application queue by using the PostQuitMessage function.
When the message loop retrieves the WM_QUIT message, the loop terminates and the main function exits.