Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Message Handling with ClassWizard

For most all of your message handling tasks, you will find that ClassWizard can be a great help. ClassWizard allows you to map messages to handlers for all your classes that are derived from CCmdTarget, by using the Message Maps page of ClassWizard. Remember that you can always start ClassWizard from the View menu or by pressing Ctrl+W. Selecting the Message Maps tab will present a dialog box that looks like the one in Figure 3.1.

03fig01.gif

Figure 3.1 The ClassWizard Message Maps page.

If you have multiple projects in your workspace, you will first need to select the appropriate entry in the Project list. You can then select any of your classes derived from CCmdTarget in the Class name list. This will present you with a list of Object IDs, the Messages those objects may receive, and a list of the member functions already implemented in the Member functions list.

The objects listed in the Object ID's list can be of three different types: the class listed in Class name, menu item objects, and other controls that may send messages to the class you have selected in Class name.

When you select your class name from the top of the Object IDs list, you will see a list of all of the Windows messages that your class is likely to receive, including several functions, such as InitInstance or Create, that aren't really message handlers at all. Nevertheless, ClassWizard will allow you to manage these functions here as well. ClassWizard will display only a certain subset of all available Windows messages, based on the Message filter specified in the Class Info page. This allows you to filter out messages that are not normally handled by certain classes. If you want to handle a message that you don't see listed, try changing the message filter.

When you select a menu command, such as ID_FILE_OPEN, from the Object IDs list, you will see two entries in the Messages list. To do something when the user selects this command from a menu, select the COMMAND message. The UPDATE_COMMAND_UI message is used to help update the status (checked, grayed, and so forth) of the menu item.

The third type of objects listed in the Object IDs list are controls that may send messages to your class. For simple controls such as buttons, you will receive only a limited set of messages, such as BN_CLICKED and BN_DOUBLECLICKED. For more complicated objects, such as edit controls, you will be able to handle many different control notifications.

Once you have selected the message that you want to handle, simply click on the Add Function button to create a handler function. ClassWizard will add a declaration for your handler to the declaration of your class and add a message map entry and skeleton handler function to your class implementation. Alternatively, you can simply double-click the message name to add the message map entry.

All you have to do now is add the code to your handler function. You can go straight to your new handler function implementation by double-clicking in the Member functions list or by clicking the Edit Code button.

Share ThisShare This

Informit Network