Adding a Property to the Control (ATL Tutorial, Part 3)
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
IPolyCtl is the interface that contains the control's custom methods and properties, and you will add a property to it.
In Class View, expand the Polygon branch.
On the shortcut menu, click Add, and then click Add Property.
The Add Property Wizard will appear.
In the drop-down list of property types, select
Sidesas the Property name.
Click Finish to finish adding the property.
When you add the property to the interface, MIDL (the program that compiles .idl files) defines a
Get method for retrieving its value and a
Put method for setting a new value. The methods are named by prepending
get_ to the property name.
The Add Property Wizard adds the necessary lines to the .idl file. It also adds the
Put function prototypes to the class definition in PolyCtl.h and adds an empty implementation to PolyCtl.cpp. You can check this by opening PolyCtl.cpp and looking for the functions
Although you now have skeleton functions to set and retrieve the property, it needs a place to be stored. You will create a variable to store the property and update the functions accordingly.
From Solution Explorer, open PolyCtl.h and add the following line after the definition of
Set the default value of
m_nSides. Make the default shape a triangle by adding a line to the constructor in PolyCtl.h:
put_Sidesfunction declarations have been added to PolyCtl.h. Replace the code in PolyCtl.cpp for
put_Sideswith the following code:
get_Sides method returns the current value of the
Sides property through the
pVal pointer. In the
put_Sides method, the code ensures the user is setting the
Sides property to an acceptable value. The minimum must be 2, and because an array of points will be used for each side, 100 is a reasonable limit for a maximum value.
You now have a property called
Sides. In the next step, you will change the drawing code to use it.