Step 2: Adding a Control
Updated: January 2011
In this step, you will add a control to your project, build it, and test it on a Web page.
To add an object to an ATL project
In Class View, right-click the Polygon project.
Point to Add on the shortcut menu, and click Add Class in the submenu.
The Add Class dialog box appears. The different object categories are listed in the tree structure on the left.
Click the ATL folder.
From the list of templates on the right, select ATL Control. Click Add. The ATL Control Wizard will open, and you can configure the control.
Type PolyCtl as the short name and note that the other fields are automatically completed. Do not click Finish yet, because you need to make some changes.
The ATL Control Wizard's Names page contains the following fields:
The name you entered for the control.
The C++ class name created to implement the control.
The file created to contain the definition of the C++ class.
The file created to contain the implementation of the C++ class.
The name of the component class for this control.
The name of the interface on which the control will implement its custom methods and properties.
A description for the control.
The readable name that can be used to look up the CLSID of the control.
You need to make several additional settings in the ATL Control Wizard.
To enable support for rich error information and connection points
Click Options to open the Options page.
Select the Connection points check box. This will create support for an outgoing interface in the IDL file.
You can also make the control insertable, which means it can be embedded into applications that support embedded objects, such as Excel or Word.
To make the control insertable
Click Appearance to open the Appearance page.
Select the Insertable check box, which by default will be cleared.
The polygon displayed by the object will have a solid fill color, so you need to add a Fill Color stock property.
To add a Fill Color stock property and create the control
Click Stock Properties to open the Stock Properties page.
Under Not supported, scroll down the list of possible stock properties. Double-click Fill Color to move it to the Supported list.
This completes the options for the control. Click Finish.
As the wizard created the control, several code changes and file additions occurred. The following files were created:
Contains most of the implementation of the C++ class CPolyCtl.
Contains the remaining parts of CPolyCtl.
A text file that contains the registry script used to register the control.
A Web page containing a reference to the newly created control.
The wizard also performed the following code changes:
Added an #include statement to the stdafx.h and stdafx.cpp files to include the ATL files necessary for supporting controls.
Changed Polygon.idl to include details of the new control.
Added the new control to the object map in Polygon.cpp.
Now you can build the control to see it in action.
To build and test the control
On the Build menu, click Build Polygon.
Once the control finishes building, right-click PolyCtl.htm in Solution Explorer and select View in Browser. The HTML Web page containing the control will be displayed. You should see a page with the title “ATL 8.0 test page for object PolyCtl” and the text PolyCtl. This is your control.
When completing this tutorial, if you receive an error message where the DLL file cannot be created, close the PolyCtl.htm file and the ActiveX Control Test container and build the solution again. If you still cannot create the DLL, reboot the computer or log off (if you are using Terminal Services).
Next, you will add a custom property to the control.