Creating a New CCmdTarget Class with a Dispatch Interface

Using ClassWizard is a simple way to derive a new class from CCmdTarget that implements a new dispatch interface.

To create a new CCmdTarget class with a dispatch interface

  1. From the View menu, click ClassWizard.

  2. Click Add Class and from the menu click New.

    The New Class dialog box appears.

  3. Under Class information, in the Name box, type CAutoClickPoint.

  4. From the Base class drop-down list box, select CCmdTarget.

    Notice that all Automation options are now available because these options pertain only to CCmdTarget-derived classes.

  5. Select the Automation option.

  6. Click OK.

    You are returned to the ClassWizard dialog box.

  7. Click the Automation tab and ensure that CAutoClickPoint is selected in the Class name box.

  8. Click the Add Property button and fill in the Add Property dialog box:

    • In the External name box, Type x.

    • In the Type box, select short.

    • Accept m_x as the Variable name.

    • Remove OnXChanged as the Notification function.

      As you will see later, the members of the CAutoClickPoint dispatch interface class do not need notification functions.

    • Use the default Implementation type, Member variable.

  9. Click OK.

  10. Repeat steps 8 and 9 for the y property.

  11. Click OK.

Study the CAutoClickPoint class created by ClassWizard in AutoClickPoint.h and AutoClickPoint.cpp.

Toward the end of AutoClickPoint.h, you’ll find the declaration for the dispatch map:

DECLARE_DISPATCH_MAP()

The AutoClickPoint.cpp file implements the dispatch map, reflecting the two properties you added in ClassWizard, x and y.

Tip   To jump to the implementation file, from ClassView, right-click any of the members of CAutoClickPoint, and click Go to Definition.

BEGIN_DISPATCH_MAP(CAutoClickPoint, CCmdTarget)
   //{{AFX_DISPATCH_MAP(CAutoClickPoint)
   DISP_PROPERTY(CAutoClickPoint, "x", m_x, VT_I2)
   DISP_PROPERTY(CAutoClickPoint, "y", m_y, VT_I2)
   //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()