Adding the CircleOffset Property

The first step in implementing CircleOffset functionality is to add the CircleOffset property to the control.

To add the CircleOffset property

  1. On the View menu, click ClassWizard.

  2. Click the Automation tab.

  3. In the Class name drop-down list box, select CCircCtrl.

  4. Click Add Property.

    The Add Property dialog box appears.

  5. In the edit control of the External name drop-down combo box, type CircleOffset.

  6. Under Implementation, select Get/Set methods.

    The Get function and Set function edit controls appear, replacing the Variable name and Notification function edit controls.

  7. From the Type list box, select short.

  8. Click OK to close the Add Property dialog box.

    This returns you to the Automation tab. Notice that the implementation of the CircleOffset property appears as:

    Implementation:
    short GetCircleOffset();
    void SetCircleOffset(short nNewValue);
    
  9. Click OK to confirm your choices and close ClassWizard.

ClassWizard creates the appropriate code to add the CircleOffset property to CCircCtrl and to CIRC.ODL. Because CircleOffset is a Get/Set property type, ClassWizard modifies the CCircCtrl class's dispatch map to include a DISP_PROPERTY_EX macro entry:

BEGIN_DISPATCH_MAP(CCircCtrl, COleControl)
    //{{AFX_DISPATCH_MAP(CCircCtrl)
    DISP_PROPERTY_NOTIFY(CCircCtrl, "CircleShape",
        m_circleShape, OnCircleShapeChanged, VT_BOOL)
    DISP_PROPERTY_EX(CCircCtrl, "CircleOffset",
        GetCircleOffset, SetCircleOffset, VT_I2)
    DISP_STOCKPROP_BACKCOLOR()
    //}}AFX_DISPATCH_MAP
    DISP_FUNCTION_ID(CCircCtrl, "AboutBox",
        DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()

The DISP_PROPERTY_EX macro associates the CircleOffset property name with its corresponding CCircCtrl class's Get and Set methods, GetCircleOffset and SetCircleOffset. The type of the property value is specified as VT_I2, which corresponds to short.

ClassWizard also adds a declaration for the GetCircleOffset and SetCircleOffset functions in CIRCCTL.H and also adds their function templates in CIRCCTL.CPP:

short CCircCtrl::GetCircleOffset()
{
    // TODO: Add your property handler here

    return 0;
}

void CCircCtrl::SetCircleOffset(short nNewValue)
{
    // TODO: Add your property handler here

    SetModifiedFlag();
}

You will modify the SetCircleOffset function to perform offset validation later in this lesson.

Because ClassWizard only creates the Get and Set functions, you must add a member variable to the CCircCtrl class to keep track of the actual value of the CircleOffset property. The Get and Set methods will query and modify this variable. You can add this variable by modifying the declaration of the CCircCtrl class in CIRCCTL.H. Add the member variable in the protected section after the destructor.

Add the following line of code in CIRCCTL.H:

    short m_circleOffset;

as shown in the following code example:

class CCircCtrl : public COleControl
{
...
protected:
    ~CCircCtrl();
    void GetDrawRect(CRect* rc);
    short m_circleOffset;
...
};

Modify the Get method created by ClassWizard, GetCircleOffset, in CIRCCTL.CPP to return the value of this new variable (the //TODO comment line can be removed):

short CCircCtrl::GetCircleOffset()
{
    return m_circleOffset;
}