14.3 Handling Window Messages

When you use the C++ derivation mechanism of the Microsoft Foundation Class Library, the derived class inherits all the functionality of the base window class, including its window procedure. The window procedure is where incoming window messages are processed. A traditional window procedure is made up of a large switch statement that examines the window message and its arguments to determine what action to take.

When you use the Microsoft Foundation window classes, you are still free to override the window procedure for the base window class and use switch statements to decode incoming window messages. However, to take full advantage of the Microsoft Foundation Class Library functionality and to make your code more compatible with future enhancements to the library and its associated programming tools, you should use the message-map mechanism to associate specific window messages to message-handler functions that you write.

·To use the message-map mechanism in your derived window classes:

1.Define message-handler member functions in your derived window class.

2.Use the DECLARE_MESSAGE_MAP macro in your derived window class declaration.

3.Use the BEGIN_MESSAGE_MAP, END_MESSAGE_MAP, and message-specific macros in the implementation file (.CPP) for your derived window class.

You can define message-handler functions as member functions for your derived window class. Typically, you define one message-handler function for each window message that you handle. The base window class from which you derive your window will handle all the other window messages that you don't explicitly handle.

There are three main categories of messages that a window receives, as listed here:

WM_COMMAND messages generated by user menu selection or accelerator invocation.

Notification messages from child windows. These are also WM_COMMAND messages, but the window procedure arguments contain the control ID of the child window and an event code, such as BN_CLICKED, to identify why the child window is sending the notification.

Other WM_XXX messages, such as WM_PAINT, generated by the system or user input.