Using Visual Basic 6

Previous chapterNext chapterContents


- 27 -
Creating Your Own ActiveX Controls with Visual Basic


Creating an ActiveX Control


Save some time and effort

Whenever you need a control, especially in a work environment, you should always check to see first if the control is available as a commercial product. The last resort should be to program or create it yourself. Building your own controls requires a lot of time and effort.


It used to be that the ability to make custom ActiveX controls was the sole domain of C++ programmers. This is no longer true. With Visual Basic 6, you can make full-fledged ActiveX controls for use not only in Visual Basic programming but also within other programming environments such as C++ and Delphi. If you're involved with Internet programming, the ActiveX controls that you make with Visual Basic can bring a new level of functionality and interactivity to your Web pages.

Make an ActiveX control with Visual Basic

1. Create a new project. Select ActiveX Control in the New Project dialog (see Figure 27.1).

FIGURE 27.1 You make an ActiveX control within a separate project.


What is a UserControl?

The UserControl is the name by which Visual Basic refers to the palette on which you apply intrinsic controls to make a custom control. If you use non-intrinsic controls, be sure you secure a license for them.


2. Name the project CursorReporter and save the project to the file rptcursr.vbp.

3. Change the name of the UserControl from UserControl1 to CursorReport (see Figure 27.2). (To avoid a name collision, be sure to name the control CursorReport, not CursorReporter.)

4. Add a Label control to the UserControl Designer. Set the control's properties to values such as those shown in Table 27.1.


Download the code for this project

The code for this chapter is in the project rptcursr.vbp, which you can find at http://www.mcp.com/info. Also included with the project is the bitmap necessary to complete this example.


TABLE 27.1 The Label Control Property Settings

Property Value
Name lblMain
BorderStyle 1 - Fixed Single
Property Value
BackColor &H0000FFFF
Caption (blank)
Height 375
Left 0
Top 0
Width 1215

FIGURE 27.2 The UserControl is the base object that has four elements: the User control, Designer, properties, and ToolboxPicture.

5. Select the ToolboxBitmap value of UserControl CursorReport in the Properties window. Click the value's ... button and assign the bitmap cursor.bmp (which you can download from http://www.mcp.com/info) to the ToolboxPicture property (see Figure 27.3).

FIGURE 27.3 The ToolboxBitmap is the image that will represent your control in the Visual Basic toolbox.

6. Resize the UserControl container so that it's just a little bigger than the perimeter of the Label control (see Figure 27.4).

FIGURE 27.4 When you add your ActiveX control to an application's form, the control's initial dimensions are the size of the UserControl Designer.

7. Save the project. Make sure that the files are named as follows:

Type Name File Name
Project CursorReporter rptcursr.vbp
UserControl CursorReport rptcursr.ctl

8. Choose Close from the control menu (the icon on the menu bar).

Your ActiveX control's ToolBoxPicture now appears in the Visual Basic Toolbox (see Figure 27.5).

FIGURE 27.5 The CursorReport ActiveX control in the Visual Basic ToolBox was made from the cursor.bmp bit-map, which is a Windows cursor.

You've just performed the basics of creating an ActiveX control. You add the control to a form as you would any other ActiveX control. Granted, the control doesn't do much, but you'll find that even when you create controls with more useful functionality, you'll use these same steps.

Understanding the UserControl Object

At the heart of every ActiveX control that you make with Visual Basic is the UserControl object. The UserControl object is the base on which you can add other Visual Basic ActiveX controls to create a uniquely new control. Controls that you add to a UserControl object are called constituent controls. In the example created earlier, the UserControl is CursorReport, and the constituent control is the Label control lblMain.

One more object is important with regard to making ActiveX controls--the AmbientProperties object, which is a set of properties that represent the state of the form or other container object that's using your ActiveX control. The AmbientProperties object is accessed by referencing the UserControl object's Ambient property. For example, if you want to set the value of your UserControl's BackColor property to the same value as the BackColor property of the container that's using your control, you use the following code:

UserControl.BackColor = Ambient.BackColor


Using the ActiveX Control Interface Wizard

Visual Basic 6 ships with the ActiveX Control Interface Wizard, which allows you to easily define the properties and methods of constituent controls that you want to expose in your custom control. The wizard also allows you to create properties and methods specific to your custom control. You access the ActiveX Control Interface Wizard by attaching it to the IDE as a Visual Basic add-in. You normally use the ActiveX Control Interface Wizard when you want to create simple ActiveX controls. Generally, the wizard will meet all your needs. Sometimes when you want a specific control that the Wizard can't produce, you would want to custom program your own. You can do this as described previously.


In this code,


Using project groups

Visual Basic allows you to work with multiple projects at one time. The collection of projects can be managed as a project group within a .vbg file. You also can work with multiple projects independent of a project group by opening multiple instances of Visual Basic. For more information about working with project groups, see Chapter 5, "Working with Projects in Visual Basic 6." You can save all open projects in the IDE into a project group file, which has the extension .vbg.


Adding a UserControl to a Form

Add the CursorReport ActiveX control to a new project's form

1. Return to the project rptcursr.vbp that you made in the preceding steps. Make sure that the UserControl Designer window is closed and the CursortReport icon is enabled in the toolbox.

2. Choose Add Project from the File menu; don't select New Project.

3. Select a Standard EXE from the Add Project dialog.

4. Select the CursorReport custom ActiveX control from the toolbox and add it to the main form in the new project (see Figure 27.6).

5. Choose Save Project Group from the File menu. Save the added project with the default names and save the project group to the file MyGroup.vbp.

6. Press F5 to run the new project.

FIGURE 27.6 Select the custom ActiveX control CursorReport from the toolbox.

Adding Functionality to an ActiveX Control


Adding Projects to the IDE

When you add more than one project to the IDE, Visual Basic will automatically try to create a project group for all the projects.


So far, you've seen how you can adapt an intrinsic control to be a part of your custom ActiveX control. Now you can add some functionality to your control to make your control useful to other programmers.


The Application Programming Interface

API stands for the Application Programming Interface. The Windows API is a collection of hundreds of functions that make up the Windows operating system. For a detailed discussion of the Windows API in relation to Visual Basic, refer to Chapter 23, "Programming Beyond Visual Basic Using the Windows API."


You're going to enhance the control that you started to create in the preceding section so that it reports back the location of the mouse pointer anywhere on the computer screen. The ActiveX control will also be resizable.

Normally, applications written in Visual Basic can't determine the location of the mouse pointer anywhere other than within the boundaries of forms that are part of the given application. To enable your control to report the location of the mouse pointer onscreen, you use the Windows API GetCursorPos.


Using GetCursorPos

The ActiveX control that you make in this chapter is an enhancement of the APIStuff project that you saw in Chapter 23. The project APIStuff.vbp in Chapter 23 also uses the API function GetCursorPos. You can download APIStuff.vbp from http://www.mcp.com/ info.


To allow your ActiveX control to determine the location of the cursor onscreen at any time, you add a Timer control to the UserControl and program the Timer event procedure to call the Windows API function GetCursorPos. GetCursorPos returns the location of the cursor anywhere onscreen. After the call to the function, you assign a string containing the returned location of the cursor to the Caption property of the constituent control lblMain.

Set up your ActiveX control to display the screen location of the mouse pointer

1. Return to the project group MyGroup.vbg, which contains the custom ActiveX control CursorReport (rtpcursr.vbp).

2. Add a Timer control to the custom ActiveX control. Set the value of the Interval property to 100.

3. Select the custom ActiveX control, CursorReport (which is saved to the file rtpcursr.vbp) in the Project Explorer window.

4. Click the View Code icon in the Project Explorer to display the Code window for the ActiveX control (see Figure 27.7).

FIGURE 27.7 You view code for any control or module by clicking the View Code icon in the Project Explorer.

5. Add the following code to the General Declarations section of the custom ActiveX control:

Private Declare Function GetCursorPos _
   Lib "user32"
(lpPoint As POINTAPI) As Long
Private Type POINTAPI
        x As Long
        y As Long
End Type


6. Add the code in Listing 27.1 to the Timer1_Timer() event procedure.

LISTING 27.1  27LIST01.TXT--Code to Report the Location of the Mouse
Pointer Anywhere Onscreen

01 `Return variable for the API function
02 Dim l As Long
03
04 `Variable of the Windows defined type, POINTAPI.
05 `This is the structure to which the API function
06 `will return the coordinates of the cursor
07 Dim pt As POINTAPI
08
09 `Get the mouse pointer coordindates
10 l = GetCursorPos(pt)
11
12 `Get the x and y elements from the POINTAPI type
13 `and create a display string. Assign that string
14 `to the constituent label's Caption property.
15 lblMain.Caption = CStr(pt.x) & " ," & CStr(pt.y)
7. Add the following code to the UserControl_Resize() event procedure:

lblMain.Width = UserControl.Width
lblMain.Height = UserControl.Height


8. Close the UserControl window.

9. Open the Form Designer window for the main form of the other project in the project group, the one that uses the custom ActiveX control. Notice that the custom ActiveX control now reports back the location of the mouse in the custom ActiveX control (see Figure 27.8).

10. Save all projects within the project group, as well as the project group itself.

FIGURE 27.8 Resize the Label control within the UserControl's Resize event procedure.

You now have a fully functional ActiveX control that displays the location of a cursor anywhere onscreen. It can be added to any program, whether the program is written in Visual Basic or Visual C++. The control also can be used in a Web page. Before it can be incorporated into these various environments, however, it must be compiled into an OCX file.

Compiling Custom ActiveX Controls

Before you can deploy your custom ActiveX control, you must compile it into an OCX file.

OCX filename defaults

The OCX filename is, by default, the same name as the project filename.

Compile your ActiveX control into an OCX

1. Select the project CursorReporter (rptcursr.vbp) in the Project Explorer.

2. Select Make RPTCURSR.OCX from the File menu to display the Make Project dialog.

3. Click OK in the Make Project dialog (see Figure 27.9).

4. Save the project and close the project group.

FIGURE 27.9 The Make process for an OCX is similar to the Make process for an EXE file. If you click the Options button, you can set version numbers and other particulars.

After you compile your custom ActiveX control into an OCX, you no longer need to use the "two-project" technique to work with the ActiveX control; you can simply use the control as you would any other ActiveX control. You can use the Package and Deployment Wizard to distribute the OCX to other programmers.

Use the control in your projects

1. Choose Components from the Projects menu.

2. In the Components dialog, click Browse.

3. In the Add ActiveX Control dialog, navigate to the folder that contains your ActiveX control.

4. Select the control by marking it in the Components list (see Figure 27.10).

FIGURE 27.10 You add a custom ActiveX control made in Visual Basic to the IDE as you would any other ActiveX control.

Deploying Custom ActiveX Controls


Licensing and copyright issues

If you use commercial controls, all the licensing agreements you made with the control vendor when you bought the control are in force. It's illegal to repackage third-party ActiveX controls as constituent controls and call them your own. Check the documentation that comes with all the ActiveX controls that you plan to use with your custom ActiveX controls to see how the licensing agreements apply.


After you compile your ActiveX project into an OCX file, you're ready to deploy it. To deploy the OCX, you must use the Setup Wizard. As with any other Visual Basic project, certain runtime files must ship with the OCX. The OCX also must be registered to the user's system. The Package and Deployment Wizard automatically takes care of all this in the setup process. If you make a special ActiveX control that uses controls other than the standard Visual Basic controls, those controls also must be included by the Package and Deployment Wizard and shipped with your special control.

Practically all Windows programs must be formally installed. Being able to simply copy and invoke executable files from a hard disk--although valid under older DOS programs--is very rare with Windows programs. Recent architectural developments in Windows over the past few years require that a lot of information about a program be entered into the Windows Registry. This is particularly true for custom ActiveX controls. This information is entered when you run a program's or ActiveX control's Setup.exe.

It's also a good idea to always use the Uninstall version of a setup program when you want to remove a program from your system. The Visual Basic Package and Deployment Wizard automatically leaves an uninstall option for your program in the Add/Remove Program Control Panel applet.

If you plan to use your ActiveX control in a Web page on the Internet, you'll have to do a certain amount of programming in VBScript and HTML. You'll also have to embed your OCX files in a .cab file that will reside on the Internet server. The Package and Deployment Wizard is a very useful tool for this process.


Custom control runtime files

Custom ActiveX controls made with Visual Basic require more than the OCX file at runtime. The Package and Deployment Wizard automatically adds these files to your deployment when it comes time to distribute your ActiveX control. However, be advised that the size of these associated files can add up to approximately 2MB. On a desktop application this isn't a big imposition, but for ActiveX controls used over the Internet, this could cause a significant increase in download time. The good news is that the runtime download happens only once, not every time you download an ActiveX control.


Being able to make your own ActiveX controls brings a whole new dimension to the scope of your programming. Using Visual Basic to make ActiveX controls makes your code truly reusable, with little or no dependence on extraneous source code file. Be advised that making an ActiveX control requires a lot of detail work. Many properties, methods, and events take a long while to master, even for the more experienced programmers. However, if you take it slowly, you will discover some very challenging programming opportunities.


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.