Win32: Messages


In this video, we explain the elements of the structure MSG, which is used to send messages to an application. The Win32 programs are event-based and the events that occur are signaled by messages that are sent to an application. In this way, GUI applications, like those programmed with the Win32 API, are able to process mouse clicks, key presses and any other user input.

The type definition of the MSG structure is shown below. The first member, hwnd, is a handle to a window. This handle allows for access to the window that received the message; we will demonstrate the use of handles later. The second member, message, is an unsigned integer that defines which enumerated message was sent. You can find a long list of the defined message values in the file "WinUser.h" beginning at line 1722. The members, wParam and lParam, carry additional information about the message that was sent—the format is dependent upon the type of the message.

The last two members of MSG, time and pt, hold the time the message was generated and position in screen coordinates of the cursor when the message was generated. Only the first four members, however, are passed on to our callback function below.

Notice that the switch statement above handles the message parameter for the message types: WM_COMMAND, WM_PAINT and WM_DESTROY. The WM_COMMAND message is used for menu selections and the argument wParam gives additional information about which menu selection was made: About or Exit. The WM_PAINT message tells the window client area to repaint itself and is sent in response to events like window resizing. Notice that the WM_PAINT handler makes use of the handle HWND. Lastly, WM_DESTROY simply posts a message to close the application.