Adding a Property Using Attributes

Adding a property or method to an ActiveX control becomes a quick and simple exercise when using attributes. This topic demonstrates the steps needed to add a simple read-write custom property using attributes, and discusses the changes to the control project.

For demonstration purposes, the sample program adds a custom property, called Data, which gets or sets the current value of a numerical value. This property will be stored in a data member of type short.

To fully implement the custom property, you need to add the property to the control's default interface and to the class implementing the control. You can do this with the Add Property Wizard and the propget and propput attributes. These attributes implement a standard set of read-write property methods for the control interface. These methods are used by clients of the control to access and set the value (in this case a data string), using standard COM procedures.

To add the custom Data property

  1. In Class View, expand MyAxCtrl and right-click the IMyCtl node.

  2. On the shortcut menu, click Add, and then click Add Property.

    The Add Property Wizard appears.

  3. For Property type, enter SHORT.

  4. For Property name, enter Data.

  5. Click Finish to implement the custom property.

The resulting modifications are as follows:

  • MyCtl.cpp   The get and set function bodies are added to the CMyCtl class. These will be modified later.

    STDMETHODIMP CMyCtl::get_Data(SHORT* pVal)
    {
       // TODO: Add your implementation code here
       return S_OK;
    }
    
    STDMETHODIMP CMyCtl::put_Data(SHORT newVal)
    {
       // TODO: Add your implementation code here
       return S_OK;
    }
    
  • MyCtl.h   The get and set methods are added to the IMyCtl interface.

    [propget, id(1), helpstring("property Data")] HRESULT Data([out, retval] SHORT* pVal);
    [propput, id(1), helpstring("property Data")] HRESULT Data([in] SHORT newVal);
    
  • MyCtl.h   The get and set methods declarations are added to the CMyCtl class.

    STDMETHOD(get_Data)(SHORT* pVal);
    STDMETHOD(put_Data)( SHORT newVal);
    

To fully implement the Data property, a data member must be added to the control class (storing the current value) and code added to the get/put functions to retrieve and update the property value.

To complete the implementation of the custom Data property

  1. In Class View, right-click the CMyCtl node.

  2. On the shortcut menu, click Add, and then click Add Variable.

    The Add Variable Wizard appears.

  3. For Variable type, enter short.

  4. For Variable name, enter m_iData.

  5. Click Finish.

  6. In MyCtl.cpp, modify the get_Data function by replacing the comment with the following code:

    *pVal= m_iData;
    
  7. Modify the put_Data function by replacing the comment with the following code:

    m_iData= newVal;
    
  8. Build the solution.

To move to the next step, see Adding an Event Using Attributes.

See Also

Concepts

Walkthrough: Creating an ActiveX Control with Attributes

Creating the ActiveX Control Project

Inserting the ATL Control Component

Other Resources

Attributes Walkthroughs