Share via

Entering the Message Loop

Once the main window is created and displayed, the WinMain function can begin 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 applications. 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. Note that bRet is a variable of type BOOL.

   while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0 ) 
      if (bRet == -1)
         // handle the error and possibly exit
         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.

See Also

Source Code



Build date: 3/25/2010