An Example: An ATL ActiveX Control Built From Objective Chart
As an illustration of the procedures involved, consider this walkthrough demonstration of building a control that wraps Objective Chart functionality.
1. After launching Visual Studio, start up a new project and select the ATL Project template. Name your new project acmechart:
Figure 155 – New project window
2. Click OK and proceed to the next screen.
Figure 156 – Step 1 of ATL COM AppWizard
3. Clear the Attributed option and select Support MFC.
NOTE >> Remember to select Support MFC. This is required; if you don’t, when you attempt to insert your Objective Chart component as an ATL object, the object wizard will display an error message and will not allow you to proceed.
4. Click Finish.
5. Once your initial project files are generated, from the Project|Add Class menu, choose ATL Control to insert the Objective Chart component. The custom object wizard for the Objective Chart control is shown highlighted in Figure 157.
Figure 157 – Adding an ATL Control
6. With the ATL Control template selected, click Open to produce the following dialog box:
Figure 158 – ATL Control Wizard Properties Window
7. Type the short name myChart. The wizard automatically derives class names, file names, an interface name, and so forth.
8. Accept these defaults by clicking Finish.
9. The wizard should take only a couple of seconds to generate the required source code. Examine the Class View tab of the Project Explorer:
Figure 159 – Class View tab of the Project Explorer
The wizard has generated all the required source code to instantiate and run an Objective Chart control. Note that the one exported method, SetValue(), is provided only as an example. As a developer, you will add method declarations (under the Imychart interface) and implementation (under Cmychart) to export and/or extend the Objective Chart’s functionality in your own custom control.
Pre-Build Set-Up
We are almost ready to build our control, but first there are a few things we must do manually. We’ll add Objective Chart’s resources to the project through the Resource Includes dialog. Then we’ll change some project settings.
1. Select the ResourceView tab of the Project Explorer.
2. Right-click acmechart resources, and select Resource Includes from the pop-up menu.
Figure 160 – Pop-up Menu on Resource View tab
3. Add directives in the dialog box to make it look like the following:
Figure 161 – Including Header Files and Compile-time Directives
The lines we’ve added are:
#include “chart\srgres.h”
...under Read-only symbol directives:
#include “chart\srgres.rc”
...under Compile-time directives:
4. Click OK to dismiss the Resource Includes dialog box, and you’ll see the following message box:
Figure 162 – Message about the directive text you added
5. Click OK.
Building and Testing A Control
We can now build our control!
1. Start the build process.
2. Note the explanatory text output in the build results tab, which recaps the steps we completed immediately before starting the build.
Figure 163 – Build results
3. You’re ready to test. If you set Visual Basic as the executable for debug session (in the Project/Settings dialog, debug tab), press F5 to launch Visual Basic.
NOTE >> You can proceed in one of two ways from this point.
4. Option 1: Use a ready-built VB project that has already been set up for us to contain and test the control we've just built. This project is created by the same wizard that we used to insert the Stingray component into our project as a new custom ATL object, and is placed in our Visual C++ project directory. A name is generated for the VB project by the wizard, and consists of the short name we gave it for the custom object's class, followed by “Proj1”. For example, in our example here, we've used “mychart” for our short name, so the wizard would set up our test VB client project for us and name it “mychartProj1”. When VB initially launches, choose the Existing tab of the opening dialog box and see that your project is there waiting for you.This project already references the control we've just built by GUID, and contains some code to test it in the Form/Load procedure.
Option2: Alternatively, we can start a new VB project from scratch, add in the control we've just built, and proceed from there. This second option is the one we’ve chosen to illustrate in detail.
5. First, after pressing F5 (or from the Build menu, choose Start Debug/Go) to launch VB, create a new Visual Basic project by selecting “Standard EXE”) on the New tab.
6. This leads to the familiar VB default new project development environment…
Figure 164 – Visual Basic project window
7. Now, add your freshly-built ActiveX control to your VB form. On VB’s Project menu, select Components… and find your ActiveX library.
Figure 165 – Component Window
8. After you click OK, the icon for the control (currently the default ATL icon) is placed on VB’s tool palette.
Figure 166 – Chart control icon on VB tool palette
9. Click the chart control icon (circled above) and draw an instance of the Objective Chart control on the VB form.
Figure 167 – Visual Basic Form
10. Add code to the Form_Load procedure:
Figure 168 – Form_Load procedure
11. Finally, run the VB project.
Figure 169 – Running the application