First of all, let me say that I love Asian cuisine. I've eaten enough General Tso's Chicken to choke a horse, and Egg Fu Yung and Hunan Shrimp is not far behind. But, I'm also a computer geek, and Asian food has nothing to do with this writeup.

MSG is a C structure in the Windows API which describes message information from a thread's message queue.

This struct is very important to the Windows Programmer because it is with the MSG struct that the programmer can handle user events. Most GUI's are event driven - and you must handle these events so that your software behaves in an expected manner. For instance, when you click on a button in a dialog box, the user probably has a pretty good idea what is supposed to happen next. It is the job of the programmer to capture that button click event, and then fulfill the user's expectations. In order to capture the events as they come in, the programmer must pass a pointer to the MSG struct to Windows, which will subsequently allow you to handle the event (or call a handler function) from your Window Procedure function. A Window Procedure function, or WndProc, is a callback function which often contains an enormous switch block that can determine which event (button click, scrollbar adjustment, keydown, timer event, etc) needs to be handled. If the event doesn't have custom behavior, it is ignored, and the DefWindowProc handler will be called.

The struct is defined thus:

typedef struct tagMSG
{
    HWND hWnd;
    UINT message;
    WPARAM wParam;
    LPARAM lParam;
    DWORD dwTime;
    POINT pt;
} MSG;

The HWND in the first field contains the handle to the window that is to receive the message. This is important, because the application needs to know which window's WndProc function is being called. Usually, you want an event to affect your window in some way, so having a handle to the window is not really a bad idea.

The second field is a UINT (an unsigned integer) which refers to the actual code value of the message. This message field is what is checked by the switch block mentioned above. The Windows API has a whole bunch of constants defined for nearly every conceivable message. You can also register your own custom messages by calling the RegisterWindowMessage function. These constants are typically assigned with a WM_ prefix followed by a descriptive title. WM_INITDIALOG for instance, is used when a Dialog Box is ready to be initialized. WM_LBUTTONDOWN is used when the user presses down on the Left Mouse Button.

The wParam and lParam parameters are extra information. Most controls are more sophisticated than a simple button click, and require a little more information from the operation system. If the user enters data into a textbox, for instance, you may need some way of knowing what that data was. Or if the user selects an item from a dropdown menu or a listbox, you need someway of indexing the user's selection. The exact meaning of the wParam and lParam parameters varies from message to message.

the dwTime and pt parameters are a simple timestamp, and cursor location respectively. I've never had to use this information while writing a program, but I'm sure there's at least one programmer out there who's grateful for it.

Headaches and asthma attacks notwithstanding, I need MSG to survive.

Log in or register to write something here or to contact authors.