ActiveX Controls: Adding Custom Properties

HomeOverviewHow Do IFAQTutorialSample

Custom properties differ from stock properties in that custom properties are not already implemented by the COleControl class. A custom property is used to expose a certain state or appearance of an ActiveX control to a programmer using the control.

This article describes how to add a custom property to the ActiveX control using ClassWizard and explains the resulting code modifications. Topics include:

Custom properties come in four varieties of implementation: Member Variable, Member Variable with Notification, Get/Set Methods, and Parameterized.

Using ClassWizard to Add a Custom Property

The following procedure demonstrates adding a custom property, CircleOffset, which uses the Get/Set Methods implementation. The CircleOffset custom property allows the control’s user to offset the circle from the center of the control’s bounding rectangle. The procedure for adding custom properties with an implementation other than Get/Set Methods is very similar.

This same procedure can also be used to add other custom properties you desire. Simply substitute your custom property name for the CircleOffset property name and parameters.

To add the CircleOffset custom property using ClassWizard

  1. Load your control’s project.

  2. On the View menu, click ClassWizard.

  3. Click the Automation tab.

  4. Select the control’s class from the Class Name box.

  5. Click Add Property.

  6. In the External Name box, type CircleOffset.

  7. In the Implementation box, click Get/Set Methods.

  8. In the Type box, select short for the property’s type.

  9. Type unique names for your Get and Set Functions, or accept the default names.

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

  11. Click OK to confirm your choices and close ClassWizard.

ClassWizard Changes for Custom Properties

When you add the CircleOffset custom property, ClassWizard makes changes to the header (.H) and the implementation (.CPP) files of the control class.

The following lines are added to the .H file to declare two functions called GetCircleOffset and SetCircleOffset:

afx_msg short GetCircleOffset( );
afx_msg void SetCircleOffset(short nNewValue);

The following line is added to your control’s .ODL file:

[id(1)] short CircleOffset;

This line assigns the CircleOffset property a specific ID number, taken from the method’s position in the methods and properties list of ClassWizard.

In addition, the following line is added to the dispatch map (in the .CPP file of the control class) to map the CircleOffset property to the control’s two handler functions:

DISP_PROPERTY_EX(CSampleCtrl,"CircleOffset", 
   GetCircleOffset, SetCircleOffset, VT_I2)

Finally, the implementations of the GetCircleOffset and SetCircleOffset functions are added to the end of the control’s .CPP file. In most cases, you will modify the Get function to return the value of the property. The Set function will usually contain code that should be executed either before or after the property changes.

void CFooCtrl::SetCircleOffset(short nNewValue) 
{
   // TODO: Add your property handler here
   SetModifiedFlag();
}

Note that ClassWizard automatically adds a call, to SetModifiedFlag, to the body of the Set function. Calling this function marks the control as modified. If a control has been modified, its new state will be saved when the container is saved. This function should be called whenever a property, saved as part of the control’s persistent state, changes value.

For a detailed implementation of the CircleOffset property, see Adding a Custom Get/Set Property in Tutorials.

See Also   ActiveX Controls: Properties, ActiveX Controls: Methods, COleControl