Most people think of ActiveX as a web component - Quicktime player, Adobe Reader etc. are all available as ActiveX controls. This isn’t entirely true though, ActiveX is just a way of packaging an application so that it can be used in another application. You can write an ActiveX in many different languages, and the control you create can do anything that an ordinary installed application can do.
There are various reasons why you’d want to write your project as a control, rather than a standalone exe, but I’ll concentrate on the reason why I’d do it. ActiveX gives you the ability to embed VB code into an application written in a different language. For instance, Director is very good for producing attractive media and user interfaces, but is limited when it comes to other functionality. ActiveX allows you to insert anything you can do in VB, straight into a Director application. This makes it very useful for carrying out the backend work of our more complex applications.
This guide relates to programming in Visual Basic 6, and assumes that you have this installed and have some programming experience. It is oriented towards my usual use for ActiveX controls - as invisible plugins to Director.
What follows is not a complete beginners guide to writing an ActiveX - there are plenty of these available already online, and they’re better written than this and have pretty graphics too. This is more of a checklist of the steps you need to go through, and some little bits that weren’t in the beginners’ guides. Follow all the steps in order, as there are several bits that are a complete pain if you want to go back and change them later. For this reason you really need to have a plan of your project, and know before you start what controls it will use and what interfaces you want to expose.
Start a new ActiveX project
If you’re going to write an ActiveX, you need to start from scratch as an ActiveX project - don’t think that you can convert an existing project to an ActiveX. However, you can use any classes or modules in an ActiveX, so if you’ve structured your project well it will be simple to add the functionality you need from your existing project to your new ActiveX.
From the File menu, select “New Project” to get the New Project window open. Normally you’d just select the default “Standard Exe”, but this time choose “ActiveX Control”. A new project will be opened, it will look like a blank form just like a conventional project, but without the border.
Step 1 - Set a sensible name for your project, before you save anything. It’s much easier to do it now than to change it later.
From the project menu, select “Project 1 Properties”. Under the “General” tab, set Project name to something sensible. This will be the filename that that the project is saved under, and the pathname that is installed as by default.
In the Properties window for your control, change the Name field from “UserControl1″ to the name you want to call your control. This name is what your control will be referred to when it is added to a project, so it’s important that it is meaningful. Note that this can’t be the same as the project name.
Now you can save your project.
Add components to your ActiveX
Add any controls, references etc. that you will be using to the project. If you want to make any properties or methods of these controls accessible from outside the control, you need to add them before you create the interface. So it’s good practice to add everything you need at this stage.
Always add a background graphic to your control, even if the end user will never see it. Otherwise, it will appear as a blank square when you add it into the host project. Either make your graphic the correct size for your control, or resize your control to fit your graphic.
If you are making an invisible control for use in Director, make sure you set the following settings. This will prevent your control grabbing the focus and messing up the user experience.
Set Enabled to false
Set CanGetFocus to false
Set InvisibleAtRuntime to True
Create the interface
This is where you define the properties, methods and events that are exposed to the host application, not the visible interface that the user accesses on a control. It’s difficult to modify this afterwards, so make sure that you’ve planned for all the properties, methods and events you’re going to need before you start.
To create the interface, use the ActiveX Control Interface Wizard. To load this, use the “Add-Ins” menu, and select “Add-In Manager”. Select “VB 6 ActiveX Ctrl Interface Wizard” and check the “Loaded” checkbox.
The wizard is pretty self-explanatory, but here’s a few tips:
On the first page “Select Interface Members”, you can choose what properties of the control itself (effectively the main form of the control) are available to the host application. Remove everything - we’re making an invisible control so they are irrelevant.
On the next page you create all the interface members that you want to be accessible. Choose sensible names for these. They have to be legal function names, but if you use an invalid name it won’t warn you until after you close the wizard.
You can then choose to map the interface members directly to a control on your form - I never do this, as I almost always want to verify any input or output before passing it on.
Next, you set the attributes for each of your interface members. The return type and default value are pretty obvious, the Arguments setting is written in the same way as you would if you were creating a function. Always put something meaningful in the “Description” field. It’s optional, but it’s very helpful to the person will end up using your control.
When you finish the wizard you have the option to “View Summary Report”, this is worth reading as it explains how to create a test project for your ActiveX.
Write your code
I strongly recommend that you have your code already written at this point. Debugging the ActiveX can be a pain, so it’s good practice to have all your code tested and debugged in a standard exe, so that all you do now is add the classes and modules, and link these up to the interfaces you created.
If you open the code window for your control, you will see that the wizard has written the code to provide all the interfaces, so you just need to insert code to call your functions.
Compiling the Control
This is straightforward, and you compile the control in the same way you would a standard exe. You can also use the package and deployment wizard to create an installer package for distributing the control.
One thing you need to do once you’ve compiled the first version of the control, is set binary compatibility. This ensures that any updated versions of your control are drop-in compatible with the original version. If you don’t set this, and you update the control, you will have to update and recompile any application that uses your control.
From the project menu, select “Project Properties”. Under the “Component” tab, check “Binary Compatibility”.







