Platform SDK: Win32 API

Entering the Message Loop

Once the main window is created and displayed, the WinMain function can being its primary task, which is to read messages from the application queue and dispatch them to the appropriate window.

The system does not send input directly to an application. Instead, it places all mouse and keyboard input from the user into a message queue, along with messages posted by the system and other application. The application must read the message queue, retrieve the messages, and dispatch them so that the window procedure can process them.

The GENERIC application uses the following message loop:

while( GetMessage( &msg, NULL, 0, 0 ) ) {
   TranslateMessage( &msg );
   DispatchMessage( &msg );
}

The GetMessage function retrieves a message from the queue. The DispatchMessage function sends each message to the appropriate window procedure. The TranslateMessage function translates virtual-key message into character messages. In GENERIC, this is necessary to implement menu access keys.