共用方式為


Defining a Message Handler for a Reflected Message

OverviewHow Do I

Once you have created a new MFC control class, you can use either ClassWizard or WizardBar to define message handlers for it. Reflected message handlers allow your control class to handle its own messages before the message is received by the parent. You can use the MFC function to send messages from your control to a parent window.

With this functionality you could, for example, create a list box that will redraw itself rather than relying on the parent window to do so (owner drawn). For more information on reflected messages, see Handling Reflected Messages. To create an ActiveX control with the same functionality, you would have to create a project for the ActiveX control.

To define a message handler for a reflected message with ClassWizard

  1. in ClassWizard, click the Message Maps tab.

  2. From the Class name drop-down list, select the name of your control class.

  3. In the Object IDs box, select the name of your control class.

  4. In the Messages box, select the message for which you want to define a handler. Your class's reflected messages are marked with an equal sign (=).

    A message with a handler already defined is displayed in bold.

    Tip   Selecting a message displays a brief description of it at the bottom of the MFC ClassWizard dialog box.

  5. Click Add Function (or double-click the message name).

    For messages that do not already have a predefined name for the handler function, the Add Member Function dialog box appears.

  6. Click OK to accept the proposed name.

    ClassWizard reappears. The message name is displayed in bold to show that a message handler is defined. The name of the new message hander appears in the Member functions box.

  7. At this point you have several options. You can:

    • Click Cancel to avoid updating your source code with the selected member functions.

    • Add more message handlers.

    • Click OK to automatically update your source code with selected member functions and close ClassWizard. You can return to ClassWizard any time during the development process.

    • Click Edit Code to jump to the empty function body just created by ClassWizard and begin defining the function's behavior.

    When you click OK or Edit Code, ClassWizard updates your source code as follows:

    • A function declaration is inserted into the header file.

    • A complete, correct function definition with a skeletal implementation is inserted into the implementation file.

    • The class's message map is updated to include the new message-handling function.

To define a message handler for a reflected message with WizardBar

  1. Using WizardBar, select the control class from the Class drop-down list, and click the Action button to navigate to the class's implementation (.cpp) file.

  2. From the Action menu, select Add Windows Message Handler.

    The New Windows Message Handler dialog appears.

  3. In Class or object to handle, the name of the control class should be selected.

  4. In the New windows messages box, select the message for which you want to define a handler. A class's reflected messages are marked with an equal sign (=).

    Click Add Handler or Add and Edit. WizardBar updates your source code as follows:

    • A function declaration is inserted into the header file.

    • A complete, correct function definition with a skeletal implementation is inserted into the implementation file.

    • The class's message map is updated to include the new message-handling function.

    WizardBar then moves the text-editor's insertion point to the body of the function.