Receiving and Dispatching Messages

A message loop that receives and dispatches messages is the heart of every Windows CE-based application. Every thread that creates a window is continuously receiving messages and dispatching messages to window procedures.

You can use the GetMessage function to receive messages. When a thread calls GetMessage, Windows CE examines the thread's message queue for incoming messages. Windows CE processes messages in the following order:

  1. Windows CE checks for messages that were placed on the queue by the SendMessage function. After the system removes the message from the queue, it dispatches the message to the appropriate window procedure from within the GetMessage function. This is done to guarantee that the sender and receiver message queue remain synchronized. The receiver must call GetMessage for the sent messages to be processed.
  2. If no sent message is found, Windows CE checks the queue for messages that were placed on the queue by a call to PostMessage.
  3. If no posted message is found, Windows CE checks the queue for messages that were posted by the user input system.

    By processing user input messages at a lower priority, the system guarantees that each input message and any posted messages that result from it are processed completely before moving on to the next input message.

  4. If no posted input messages are found, Windows CE checks the queue for WM_QUIT messages that were placed on the queue by a call to PostQuitMessage.
  5. If no posted quit messages are found, Windows CE checks the queue for WM_PAINT messages that were placed on the queue by the windowing system.
  6. If no paint messages are found, Windows CE checks the queue for WM_TIMER messages that were placed on the queue by the timer system.

When GetMessage receives any of the previous messages, it returns the message content. It is then the responsibility of the thread to call DispatchMessage to dispatch the message to the correct window procedure. If the message is a WM_QUIT message, the return value of GetMessage is zero, which causes the thread to end its message loop.

The system dispatches messages in the GetMessage call of the message loop, and the application dispatches messages by using the DispatchMessage function in the message loop. Windows CE handles the details of finding the window procedure of the receiver window.