Creating the Message Loop

Once you have created and displayed a window, the WinMain function can begin its primary duty: to read messages from the application queue and dispatch them to the appropriate window. WinMain does this by using a message loop. A “message loop” is a program loop, typically created by using a while statement, in which WinMain retrieves messages and dispatches them.

Summary: Windows applications receive input through a message queue.

Windows does not send input directly to an application. Instead, it places all mouse and keyboard input into an application queue (along with messages posted by Windows and other applications). The application must read the application queue, retrieve the messages, and dispatch them so that the appropriate window function can process them.

The simplest possible message loop consists of the GetMessage and DispatchMessage functions. This loop has the following form:

MSG msg;

.

.

.

while(GetMessage(&msg, NULL, NULL, NULL)) {

DispatchMessage(&msg);

}

In this example, the GetMessage function retrieves a message from the application queue and copies it into the message structure named msg. The NULL arguments indicate that all messages should be processed. The DispatchMessage function directs Windows to send each message to the appropriate window function. Every message an application receives, except the WM_QUIT message, belongs to one of the windows created by the application. Since an application must not call a window function directly, it instead uses the DispatchMessage function to pass each message to the appropriate function.

Depending on what your application does, you may need a more complicated message loop. In particular, to process character input from the keyboard, you must translate each message you receive by using the TranslateMessage function. Your message loop should then look like this:

while (GetMessage(&msg, NULL, NULL, NULL)) {

TranslateMessage(&msg);

DispatchMessage(&msg);

}

The TranslateMessage function looks for matching WM_KEYDOWN and WM_KEYUP messages and generates a corresponding WM_CHAR message for the window that contains the ANSI character code for the given key.

A message loop may also contain functions to process menu accelerators and keystrokes within dialog boxes. Again, this depends on what your application actually does.

Windows places input messages in an application queue when the user moves the cursor in the window, presses or releases a mouse button when the cursor is in the window, or presses or releases a keyboard key when the window has the input focus. The window manager first collects all keyboard and mouse input in a system queue, then copies the corresponding messages to the appropriate application queue.

The message loop continues until GetMessage returns NULL, which it does only if it retrieves the WM_QUIT message. This message is a signal to terminate the application, and is usually posted (placed in the application queue) by the window function of the application's main window.