Building ActiveX Controls

See Also

Using the component model of the Windows Foundation Classes for Java (WFC), you can create ActiveX controls that can be used in WFC applications or in other development environments that support ActiveX. To create an ActiveX control from a WFC control, you register the WFC control's class as a COM class. Once the control's class has been registered as a COM class, you can package the class file for the control into a COM DLL and register it as an ActiveX control in the registry. Once the control is registered as an ActiveX control, it can be accessed from an ActiveX client.

In this scenario, you use the WFC control documented in Creating a Control. If you have not created it, build the control documented in the topic, and continue with the procedures in this topic. You will learn:

Note   The following procedure assumes that you have an existing WFC component project opened in Visual J++.

Defining a WFC Control as a COM Object

To access your control from other ActiveX clients, you define your control as a COM object. So that a class can be exposed as a COM object, it needs to have an @com.register comment tag placed above the class definition. Visual J++ provides an automated way to generate @com.register comment tags for your classes.

Note   If your control’s project was created using the Control template, the control already contains a comment tag to register it as a COM object. Remove the forward slashes (//) to enable the comment tag.

To define a WFC component as a COM object

  1. On the Project menu, click <Project> Properties (where <Project> is the name of your control project).

  2. In the <Project> Properties dialog box, click the COM Classes tab.

  3. In the list of classes, select your control's class.

  4. Click the Options button.

  5. (Optional) In the Type Library Options dialog box, change the name of the type library file that is created to define the interface to your control, the name of the library, the name of the control as it will be displayed to development environments, and Help file information, and click OK.

  6. In the <Project> Properties dialog box, click OK.

    Visual J++ adds a comment tag at the top of your control’s class definition that registers the class as a COM object.

Note   If you do not need to define multiple classes as COM classes, you can define a COM class in the Class Properties dialog box. To display the Class Properties dialog box, in Class Outline, right-click the name of the class, and then click Class Properties. In the Class Properties dialog box, select the COM Class check box.

Packaging the Control in a COM DLL

After you have defined your WFC control as a COM object, you package the control's class files into a COM DLL file. Your control must be packaged in a COM DLL file to be available as an ActiveX control. The COM DLL provides the interface that is used by ActiveX clients to access your control and its members.

Note   To distribute your ActiveX controls over the Internet, you can package your control in a CAB file instead of a COM DLL.

To build a control as a COM DLL

  1. On the Project menu, click <Project> Properties (where <Project> is the name of your control project).

  2. In the <Project> Properties dialog box, click the Output Format tab.

  3. Select the Enable Packaging check box.

    The other controls on the tab should now be enabled.

  4. In the Packaging type drop-down list, select COM DLL.

  5. In the File name box, type a name for your COM DLL. (A default name is created using the name of the project.)

  6. In the associated drop-down list, select the Outputs of type and the Java Classes & Resources options.

  7. Click OK.

Building the Project

After you have configured the packaging options for the project, you need to build the project. Visual J++ then adds a type library to your project that defines the COM interface for your control. The type library also contains information that the registry uses to register the COM class as a control. When the type library has been generated, Visual J++ registers the COM classes in your project in the registry using the type library file that was created. After the control's classes have been registered, Visual J++ packages the project's class files and the type library into a COM DLL.

To build the project

Registering the COM DLL

Once you have registered your WFC control's classes as COM classes and packaged them in a COM DLL, you register the COM DLL in the system registry. To do this, you use the Regsvr32.exe program. Because the project's type library flags the WFC control's COM classes as a control, Regsvr32 registers the COM DLL as an ActiveX control. When the COM DLL has been registered, other applications can see your WFC control in the list of ActiveX controls that are registered on the system.

To register the COM DLL

  1. Click the Start button, and then click Run.

  2. In the Open box, type:

    Regsvr32.exe <DLL path and filename>

    where <DLL path and filename> is the path and file name of your control’s DLL. For this scenario, type:

    Regsvr32 C:\Project1\Project1.dll

  3. Click OK.

    If you receive a message that the registration failed, ensure that the path to the control's DLL is correct and that the file exists.

Testing the Control in Microsoft Visual Basic

To test your ActiveX control, you add the control to a programming tool or application that supports ActiveX. For this scenario, you can use Microsoft Visual Basic version 5.0 or later to add the control and test its features.

To add a WFC based ActiveX control to a Visual Basic form

  1. Run Visual Basic.

  2. On the File menu in Visual Basic, click New Project.

  3. In the New Project dialog box, click the Standard EXE icon, and then click OK.

  4. Right-click the Toolbox, and then click Components.

  5. In the Components dialog box, select your control, and then click OK.

    For this scenario, select the Project1 control.

  6. In the Toolbox, double-click your control to add it to the form.

    The control is added in the center of the form.

  7. Press F5 to run the project.

    The form is displayed with your control.

If you are working with the control that is documented in Creating a Control, you can scroll the horizontal scroll bar. The text in the control changes to reflect the position of the scroll bar.

For information on importing an ActiveX control into a WFC application, see Importing ActiveX Controls.