Enabling the BackColor Property

The first step in implementing the new painting behavior is to add the background color property (BackColor) to the control. BackColor, among others, is a stock property.

To add the BackColor property

  1. On the View menu, click ClassWizard.

  2. Click the Automation tab.

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

  4. Click Add Property.

    The Add Property dialog box appears.

  5. From the External name drop-down combo box, select BackColor.

  6. Under Implementation, select Stock.

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

    This returns you to the Automation tab. Notice that the implementation of the BackColor property is listed as:

    Implementation:
    Stock Property
    
  8. Click OK again to confirm your choices and close ClassWizard.

ClassWizard creates the code to add the BackColor property, modifying both the CCircCtrl class and the type library file CIRC.ODL.

ClassWizard adds the DISP_STOCKPROP_BACKCOLOR macro in CCircCtrl class's dispatch map in CIRCCTL.CPP as shown below:

BEGIN_DISPATCH_MAP(CCircCtrl, COleControl)
    //{{AFX_DISPATCH_MAP(CCircCtrl)
    DISP_STOCKPROP_BACKCOLOR()
    //}}AFX_DISPATCH_MAP
    DISP_FUNCTION_ID(CCircCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()

The control's type library file (CIRC.ODL) is modified by ClassWizard to add BackColor to its property section, as shown in the following code sample:

dispinterface _DCirc
{
    properties:
        // NOTE - ClassWizard will maintain property information here.
        //    Use extreme caution when editing this section.
        //{{AFX_ODL_PROP(CCircCtrl)
        [id(DISPID_BACKCOLOR), bindable, requestedit] OLE_COLOR BackColor;
        //}}AFX_ODL_PROP
    methods:
        // NOTE - ClassWizard will maintain method information here.
        //    Use extreme caution when editing this section.
        //{{AFX_ODL_METHOD(CCircCtrl)
        //}}AFX_ODL_METHOD

        [id(DISPID_ABOUTBOX)] void AboutBox();
};

Because the BackColor property is of type DISP_STOCKPROP_BACKCOLOR, its value can be modified only through its Get and Set methods.

The value of the BackColor property is maintained by class COleControl. The SetBackColor member function of COleControl automatically calls the OnBackColorChanged member function after setting the BackColor value, invalidating the control. Invalidating the control causes the OnDraw function to be called, and the control is redrawn using the new background color.