Window Message

From ReliCodia

User input and system notifications are sent by Windows to your application. You get a first look at each message inside the message loop. You can also create messages and send them back to your application, or post them in the Windows message queue.

Message Loop

Every interactive Windows application must have a message loop, usually in its main procedure. The simplest message loop will just retrieve a message from the internal Windows message queue, and dispatch it to your application, where it is picked by your Window Procedure.

MSG  msg;
int status;
while ((status = ::GetMessage (&msg, 0, 0, 0 )) != 0)
{
    if (status == -1)
        throw Win::Exception ("Error in the Windows message loop");
    ::DispatchMessage (&msg);
}				

The MSG data structure has the following data members:

  • hwnd -- handle to the window that will process this message
  • message -- low word: message ID (for instance WM_PAINT), high word: reserved
  • wParam -- message-specific data
  • lParam -- message-specific data
  • time -- time at which message was posted
  • pt -- cursor position in screen coordinates, when message was posted.

The types of these parameters are as follows:

  • HWND hwnd;
  • UINT message;
  • WPARAM wParam;
  • LPARAM lParam;
  • DWORD time;
  • POINT pt;

User Message

In your program you can send or post Windows messages. Conceptually, when you send a message, you are calling your own Window Procedure. After you have returned from SendMessage, the message have been processed and you can see its result. When you post a message, you append it to the Windows message queue. The message will be executed later, when its turn comes. PostMessage returns as soon as the message is added to the queue.

You can also define your own messages. Message IDs between WM_USER and 0x7FFF are available. Or you can call RegisterMessage to obtain a message ID (between 0xC000 and 0xFFFF) that can be shared between applications. It's up to you to decide what to pass in wParam and lParam, and what to return as a result of message processing.

Window-Specific Messages