Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Creating an ActiveX Control with Visual C++ and MFC

Previous versions of Visual C++ and MFC enabled development of ActiveX controls for the 16-bit versions of Windows. Microsoft recently stopped providing the Visual C++ 1.52c compiler to registered users of its 32-bit C++ compiler, so this section concentrates on the 32-bit environment only. Building 16-bit OLE controls is possible with Visual C++ 1.52c (if you currently have a copy), but 16-bit development is rapidly being left behind.

Using Visual C++ and MFC for ActiveX Controls

Before the ActiveX control specification was released, OLE controls had to have certain interfaces implemented whether or not they needed them. This meant that controls were larger than they needed to be. This is fine if you are using your controls on a local machine, but with ActiveX controls that may need to be downloaded and installed across a low-bandwidth, high-latency network, any excess baggage is less efficient in achieving this end. For your controls to be useful in a network setting, your ActiveX controls need to be lean, mean, and efficient downloading machines.

Visual C++ comes with the MFC ActiveX ControlWizard to help you create controls. This wizard is one of the fastest ways to create a control. In fact, if you are a newcomer to creating controls, it is the best way to learn, because it creates a framework for you. You can be up and running very quickly. However, you need to be aware of a few drawbacks.

To use a control created with Visual C++ and based on MFC, the MFC DLL must reside on the client machine. This file is about 1.2MB and must be downloaded to the client machine. This file must be downloaded only if the DLL hasn't been downloaded to the client machine already, however. So you take a small performance hit the first time your control is used. Furthermore, MFC-based controls tend to be larger than the controls created using ATL or the Platform SDK directly.

You need to weigh your options carefully, considering performance, programmer skill, timetable, and environment. This is not to say that MFC-based controls are not suitable for use in the ActiveX environment, but simply to make you aware of the factors associated with choosing this method. If you are building controls for a high-bandwidth and potentially low-latency intranet, the size of the control and the associated DLL are not major factors. Speed of development, less complexity, and rich features can be more important. In fact, a basic ActiveX control created with the MFC ActiveX ControlWizard is only 23KB. Even on the sluggish Internet, 23KB is not extremely large, especially in comparison to some of the large graphics files and Microsoft Video (AVI) files embedded in Web pages.

MFC Encapsulation of ActiveX and ActiveX Controls

MFC encapsulates the ActiveX control functional ity in a class called COleControl (see Figure 28.17). COleControl is derived from CWnd and, in turn, from CCmdTarget and CObject.

28fig17.gif

Figure 28.17 The class hierarchy for COleControl.

COleControl is the base class from which you derive to create any ActiveX control you want. What's nice is that your control inherits all the functionality of the base class COleControl. You then can customize the control to include the capabilities you want in it. With MFC, the complexities of dealing with the COM interfaces are abstracted into an easy-to-use class. In addition, MFC provides a framework for your control so you can worry about the details of what you want your control to do instead of re-creating functionality that all controls have to contain in order to work.

MFC itself provides a range of capabilities already created for you when you use it. It also includes functionality to perform messaging and automated data exchange.

The MFC ActiveX ControlWizard

The beauty of Visual C++ and MFC is that they perform the mundane task of creating the framework for your control, leaving you the task of making your control perform the functionality you want it to create. At the center of this is the MFC ActiveX ControlWizard, which you use to create ActiveX controls. In this section, you examine each feature of the ControlWizard and create your first ActiveX MFC control.

You first need to launch Visual C++. After you have Visual C++ up and running, choose File, New. You then see the New dialog box (see Figure 28.18).

28fig18.gif

Figure 28.18 The New dialog box in Visual C++.

Select the Projects tab, and select MFC ActiveX ControlWizard as the project type. You also need to give your control a title and a location. In this case, call it simple control and accept the default location.

You now are looking at the first page of the MFC ActiveX ControlWizard (see Figure 28.19). Here, the wizard asks you a series of questions about what you want in your control:

28fig19.gif

Figure 28.19 Step 1 of the MFC ActiveX ControlWizard in Visual C++.

In this case, you are going to create only one control, so select one control for this project. As you already learned, one DLL hosting ActiveX controls can contain several controls.

Also, select the choice for the wizard to include licensing support for this control. In addition, ask the wizard to document the code it is going to write for you in the control framework with comments.

Lastly, ask the wizard to gen erate a basic help file so that you can provide online help for the programmers who will be using this control. It is extremely important that this control be well-documented. Then, click the Next button and go to page 2 of the ActiveX ControlWizard (see Figure 28.20).

28fig20.gif

Figure 28.20 Step 2 of the MFC ActiveX ControlWizard in Visual C++.

Step 2 of the MFC ActiveX ControlWizard presents you with more options for this ActiveX control:

The MFC ActiveX ControlWizard enables you to control the naming of each of the controls in your project to include the class names, source filenames, and property sheet names (refer to Figure 28.19). If you click the Edit Names button (refer to Figure 28.20), you will see the Edit Names dialog box (see Figure 28.21). The ControlWizard does provide a default naming convention; in this case, accept the defaults provided.

28fig21.gif

Figure 28.21 The Edit Names dialog box in step 2 of the MFC ActiveX ControlWizard in Visual C++.

Next are questions regarding what features you want to have in this control. You need to keep in mind the previous section on optimizations. Does the control need to be active when visible, or is it invisible at runtime like a timer control or a communications control? This control will need to be active and visible. You want this control to be available in the Insert Object dialog box, so you will choose this option. No doubt you are proud of the controls you create, so you can include an About dialog box to post your name or your company's name. Lastly, do you want this control to be a simple frame control and support the ISimpleFrameSite interface? This is so the control can act as a frame for other controls. For this example's purposes, do not choose this option.

You now need to take a look and select the advanced options that support ActiveX enhancements. Click the Advanced button and go to the Advanced ActiveX Features dialog box (see Figure 28.22).

28fig22.gif

Figure 28.22 The Advanced ActiveX Features dialog box of step 2 of the MFC ActiveX ControlWizard in Visual C++.

In the Advanced ActiveX Features dialog box, you can choose one of six options. Keep in mind the previous information you covered on these options.

Choose all but Windowless Activation, and click OK. Then, click Finish. You then see a summary of the features the wizard will create for you in the New Project Information dialog box (see Figure 28.23).

28fig23.gif

Figure 28.23 The New Project Information dialog box of the ActiveX ControlWizard in Visual C++.

Click OK in the New Project Information dialog box, and the wizard creates a basic control for you and implements all the features you selected in it. This control just needs to be compiled before it is up and running. The wizard even added an ellipse in this control's drawing code so that it will have something to display. You now have the framework to start customizing this control. The nice thing is that most of that functionality already is encapsulated in MFC. To assist you in this endeavor, Visual C++ provides you with the ClassWizard. The sky is the limit on what types of creations are possible now that you have the framework built for you.

Share ThisShare This

Informit Network