Chapter 2 Messages

The Microsoft Windows operating system communicates with applications through formatted window messages. These messages are sent to an application's window procedure for processing.

Some messages return values that contain information about the success of the message or contain other data needed by an application. To obtain the return value, the application must call the SendMessage function to send the message to a window. This function does not return until the message has been processed.

If the application does not require the return value of the message, it can call the PostMessage function to send the message. This function places a message in a window's application queue and then returns immediately. If a message does not have a return value, the application can use either function to send the message, unless the message description indicates otherwise.

A message consists of three parts: a message number, a word parameter, and a long parameter. Message numbers are identified by predefined message names. Each message name begins with letters that suggest the meaning or origin of the message. The word parameter and long parameter, named wParam and lParam respectively, contain values that depend on the message number.

The lParam parameter often contains more than one type of information. For example, the high-order word may contain a handle to a window and the low-order word may contain an integer value. The HIWORD and LOWORD utility macros can be used to extract the high- and low-order words of the lParam parameter. The HIBYTE and LOBYTE utility macros can be used with HIWORD and LOWORD to access any of the bytes. Casting can also be used.

Following are the four ranges of message numbers:

Range Meaning

0 through WM_USER – 1 Messages reserved for use by Windows.
WM_USER through 0x7FFF Integer messages for use by applications.
0x8000 through 0xBFFF Messages reserved for use by Windows.
0xC000 through 0xFFFF String messages for use by applications.

Message numbers in the first range (0 through WM_USER – 1) are defined by Windows. Values in this range that are not explicitly defined are reserved for future use by Windows. This chapter describes messages in this range.

Message numbers in the second range (WM_USER through 0x7FFF) can be defined and used by an application to send messages within a private window class. Such predefined control classes as BUTTON, EDIT, LISTBOX, and COMBOBOX may use values in this range. Messages in this range should not be sent to other applications unless the applications have been designed to exchange messages and to attach the same meaning to the message numbers.

Message numbers in the third range (0x8000 through 0xBFFF) are reserved for future use by Windows.

Message numbers in the fourth range (0xC000 through 0xFFFF) are defined at run time when an application calls the RegisterWindowMessage function to obtain a message number for a string. All applications that register the identical string can use the associated message number for exchanging messages with each other. The actual message number, however, is not a constant and cannot be assumed to be the same in different Windows sessions.