ClassView, WizardBar, or ClassWizard enable you to bind member functions of MFC classes (user-interface classes derived from the MFC library) to the messages generated by your application's resources. They use MFC message maps to create the binding.
Note ClassWizard is not for use when mapping messages to ATL objects. Instead use ClassView or the WizardBar only.
When you use ClassView, WizardBar, or ClassWizard to create a new class derived from one of the framework classes, it automatically places a complete and functional class in the header (.h) and implementation (.cpp) files you specify. ClassWizard keeps track of the class's message-handling and data-exchange members, so that you can update the class at a later time.
Note ClassWizard is only for use with MFC user-interface classes derived from CCmdTarget that handle messages or manage dialog box controls. To add a new class that does not handle messages, create the class directly in the text editor. (The exception to this rule is class CRecordset, for database support, which can be created with ClassWizard.)
To define a message handler with ClassView or WizardBar
-or-
From WizardBar's Class drop-down list, select a class, then click Add Windows Message Handler on the Action menu.
The New Windows Message and Event Handler dialog box appears.
The New Windows messages/events box displays a list of messages that you can add to the selected class or object. The Existing Windows message/event handlers box displays a list of messages already associated with the selected class or object. (Note that the Filter for messages available to class drop-down list allows you to select different sets of messages/events.)
Note If the selected class is a dialog class, form view, or record view, then the New Windows Messages and Event Handlers box includes window messages but not menu commands. To map menu IDs to messages in a class, you need to determine which class should handle the message (for example, CView or CMainFrame). Choose that class in the WizardBar Class drop-down list, select the menu ID from the Filter drop-down list, then from the Action menu select Add Windows Message Handler.
For information on handling custom messages, see MFC Technical Note 6.
WizardBar then adds a stub function to the class's implementation file. For example:
// CChildFrame message handlers
void CChildFrame::OnLButtonDblClk(UINT nFlags,
CPoint point)
{
// TODO: Add your message handler code here
// and/or call default
CMDIChildWnd::OnLButtonDblClk(nFlags, point);
}
Add and Edit performs the same function as Add Handler, except that after adding the message handler, Add and Edit navigates you to the file in which the message handler is implemented.
If you want to edit an existing message handler, or one you just added, select an item from Existing message/event handlers, and click Edit Existing to navigate to the file in which the message handler is implemented.
For dialog boxes in your project that are already associated with a class, you can take advantage of some shortcuts when creating message handlers. You can quickly create a handler for either the default control event, or for any applicable Windows message.
Note ClassWizard and the WizardBar can not be used for adding message handlers to ATL objects. Add a new message handler using the ClassView method.
To quickly add the default message handler for a control
If the control already has a handler defined for it, double-clicking jumps you to the handler code in the Text editor.
Note You can disable this functionality by clearing the Double-click in dialog editor edits code checkbox on the Compatibility tab of the Options dialog (available from the Tools menu). In this case, double-clicking on a control opens the control's Properties page.
To quickly add a message handler for a control
The Add Member Function dialog appears, with a proposed name for that message handler (for example, OnKeydownTab1 for the TCN_KEYDOWN message on a tabbed dialog with an object ID of IDC_TAB1).
Note Selecting a bolded, or implemented message from the Messages list jumps you to that handler function in the Text editor.
afx_msg void OnKeydownTab1(NMHDR* pNMHDR, LRESULT* pResult);
……
void CAboutDlg::OnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
Control name | Default event |
Animate | NM_OUTOFMEMORY |
Check Box | BN_CLICKED |
Combo Box | CBN_EDITCHANGE |
Custom | (Not applicable) |
Date Time Picker | DTN_CLOSEUP |
Edit Box | EN_CHANGE |
Hot Key | NM_OUTOFMEMORY |
IP Address | IPN_FIELDCHANGED |
List | NM_CLICK |
List Box | LBN_SELCHANGE |
Month Calendar | MCN_GETDAYSTATE |
Progress | NM_OUTOFMEMORY |
Push Button | BN_CLICKED |
Radio Button | BN_CLICKED |
Rich Edit | NM_CLICK |
Scrollbar | (Not applicable) |
Slider | NM_OUTOFMEMORY |
Spin | NM_OUTOFMEMORY |
Tab | TCN_SELCHANGE |
Tree | NM_CLICK |
To define a message handler with ClassWizard
Two shortcuts are available for:
ClassWizard appears and displays information about the currently selected class or the class you last edited with ClassWizard.
ClassWizard displays information about the user-interface object that is currently selected.
Messages with handlers already defined are displayed in bold.
Note The messages you see in the Messages box are those most appropriate to your class. If your class is not associated with the resource that contains the command that you want to handle, set the focus on the resource (for example, a dialog), open ClassWizard, and then use the Class name drop-down list to switch to the class from which you want the message handled.
In addition, you can change the set of messages you handle by selecting the Class Info tab and selecting a new set of messages in the Message filter box. For information on handling custom messages, see MFC Technical Note 6.
Tip Selecting a message displays a brief description of it at the bottom of the MFC ClassWizard dialog box.
For messages that do not already have a predefined name for the handler function, the Add Member Function dialog box appears.
-or-
From the Add Member Function dialog box, click OK to accept the default name.
Either action returns you to the ClassWizard Message Maps tab.
The message name is now displayed in bold to show that a message handler has been defined. The name of the new message hander appears in the Member functions box.
When you choose OK or Edit Code, ClassWizard updates your source code by:
For tips on adding message handlers to a class using ClassWizard, see Adding Functions with ClassWizard.