Notification Messages from Child Windows

Windows programs often use a main frame window containing one or more child windows. These child windows are often predefined control windows such as buttons or edit text fields. These controls communicate with their parent window by sending WM_COMMAND notification messages. For example, a child button control responds to a user mouse click by sending a WM_COMMAND message to its parent window. The arguments to the window message procedure contain the control ID of the button and the the constant BN_CLICKED. Thus, the parent window gets a notification message that tells it the ID of the control and what happened to that control.

The Microsoft Foundation Class Library provides support through message maps for handling notification messages from child windows. A set of macros is provided to support notification messages that are generated by standard control windows. For example, the message-map entry macro for a BN_CLICKED notification message is ON_BN_CLICKED. Macros for other notification messages are formatted in a similar fashion. For a list of available notification message-map entry macros, see AFXMSG.H or the reference manual.

·To handle notification messages from child windows:

Provide for each possible message a control ID number and a function pointer to the member function to be called when that message is received. Add entries to the message map and member functions for your frame window class to handle the possible notification messages for your window.

For example, assume that you have a child button window with an ID of ID_MY_FIRST_BUTTON. The following class declaration and message- map definition calls the member function OnMyFirstButtonClick when the button sends a BN_CLICKED notification message to the frame window:

class CMyWnd : public CFrameWnd

{

public:

afx_msg void OnMyFirstButtonClick();

// other class declaration stuff ...

DECLARE_MESSAGE_MAP();

};

// in .CPP file

BEGIN_MESSAGE_MAP( CMyWnd, CFrameWnd )

ON_BN_CLICKED( ID_MY_FIRST_BUTTON, OnMyFirstButtonClick )

END_MESSAGE_MAP()

·To differentiate between messages sent by several child windows:

Use different control ID arguments to the message-map macro. For example, you can have two different child button windows that send the same BN_CLICKED message. Then the message map described above would have a second entry for another button child window, as follows:

BEGIN_MESSAGE_MAP( CMyWnd,CFrameWnd )

ON_BN_CLICKED( ID_MY_FIRST_BUTTON, OnMyFirstButtonClick )

ON_BN_CLICKED( ID_MY_SECOND_BUTTON, OnMySecondButtonClick )

END_MESSAGE_MAP()

Notice that the second message-map entry refers to a different member function, which you would have to define for your frame window class.

Like the message-handler functions for menu-command messages, all message-handler functions for notification messages are declared with the afx_msg prefix and take no arguments and return no value. They are similar to the ON_COMMAND macro used for message-map entries for menu commands. The message-map macros for the notification messages have two arguments: the ID of the child window and a function pointer to the user-defined message-handler function for that message.

Dialog boxes, which are described in the “Dialogs and Controls Windows” chapter of the cookbook, use this same notification mechanism: within the message-map entries, each child window notification message is matched to a user-defined message-handler function.

The next section shows how to use the message-map mechanism to respond to messages that are not from menu commands or notifications from child windows.