Adding a Control (ATL Tutorial, Part 2)
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Adding a Control (ATL Tutorial, Part 2).
In this step, you will add a control to your project, build it, and test it on a Web page.
Procedures
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 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 have to make some changes.
The ATL Control Wizard's Names page contains the following fields:
Field | Contents |
---|---|
Short name | The name you entered for the control. |
Class | The C++ class name created to implement the control. |
.h file | The file created to contain the definition of the C++ class. |
.cpp file | The file created to contain the implementation of the C++ class. |
CoClass | The name of the component class for this control. |
Interface | The name of the interface on which the control will implement its custom methods and properties. |
Type | A description for the control. |
ProgID | The readable name that can be used to look up the CLSID of the control. |
You have 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.
The polygon displayed by the object will have a solid fill color, so you have 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:
File | Description |
---|---|
PolyCtl.h | Contains most of the implementation of the C++ class CPolyCtl . |
PolyCtl.cpp | Contains the remaining parts of CPolyCtl . |
PolyCtl.rgs | A text file that contains the registry script used to register the control. |
PolyCtl.htm | 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.
Building and Testing the Control
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.
Note
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.