Creating a Windows VCL ActiveX Button

From RAD Studio
Jump to: navigation, search

Go Up to How To Build Windows VCL Applications


Like a Delphi control, an ActiveX control generates program code when you place the component on a form or other logical container in the IDE. The main difference between an ActiveX control and a Delphi control is that an ActiveX control is language independent. You can create ActiveX controls for deployment to a variety of programming environments on Windows, not just Delphi or C++Builder.

This procedure uses the VCL forms ActiveX wizard to create an ActiveX control. To test the control, you can install it on your machine as a VCL component in the IDE. To install the control, you first need to create a package for it. This procedure consists of the following major steps:

Metaprocedure for Creating a VCL Forms ActiveX Button:

  1. Create an ActiveX Library project for an ActiveX button control.
  2. Register the ActiveX button so its icon can be displayed in the toolbar.
  3. Create a package for the ActiveX button.
  4. Install the package.
  5. Test the ActiveX button.

To create an ActiveX Library project for an ActiveX button control:

  1. Create a directory on your local drive for the ActiveX project. Give it a name that you can find easily, for example, ActiveX.
  2. Choose File > New > Other and select the ActiveX page in the New Items dialog box.
  3. In the ActiveX folder, double-click ActiveX Library. This creates a Dynamic Link Library [DLL] project that you can use to host in-process ActiveX Objects.
  4. Choose File > New > Other again.
  5. On the ActiveX page, double-click ActiveX Control. The ActiveX Control Wizard displays.
  6. In the Component Name drop-down, select TButton.
  7. By default, ButtonX displays as the New ActiveX Name. Rename ButtonX to the name you want displayed for your ActiveX button, for example, MyActiveXButton.
Note: Modifications you make to the name update the Implementation Unit and Project Name. Leave the remaining fields with default values.
  1. Click OK. The wizard generates the code needed to implement the ActiveX control and adds the code to the project. If the project is already an ActiveX library, the wizard adds the control to the current project.
Note: If the project is not already an ActiveX library, a warning dialog displays and asks you if you want to start a new ActiveX library project.
  1. Click OK to start the new ActiveX Library project.

To register the ActiveX button:

  1. Build the project and save all files to your ActiveX project directory. Dismiss the warning about debugging. The project builds and creates an OCX file in your project directory.
  2. Choose Run > Register ActiveX Server to register the ActiveX button. A dialog box displays a message indicating that registration was successful and it shows the path to the resulting OCX file.
  3. Click OK.

To create a new package for the ActiveX button:

  1. Choose File > New > Other to create a new package. The New Items dialog displays.
  2. Double-click Package on the New page to display the Package - package.dpk dialog and click Add.
  3. On the Add unit tab of the Add dialog, browse to your project directory.
  4. Select the ButtonXControl1_TLB.pas file, and click Open.
  5. Click OK to add the file to the package and return to the Package - package.dpk dialog. The Package - package.dpk dialog displays showing the files in the package and two required files: rtl.dcp and vcl.dcp.

To add the required files and install the package:

  1. In the Package - package.dpk dialog, select rtl.dcp, and click Add.
  2. On the Add unit tab of the Add dialog, browse to the Lib directory in Delphi, select the rtl.dcp file, and click Open; then click OK on the Add dialog.
  3. In the Package - package.dpk dialog, select vcl.dcp, and click Add.
  4. On the Add unit tab of the Add dialog, browse to the Lib directory in Delphi, select the vcl.dcp file, and click Open; then click OK on the Add dialog.
  5. In the Package - package.dpk dialog, click Compile to compile the package. A dialog displays, indicating that the package has been installed. Click OK.
  6. Click the X in the upper right corner of the Package - package.dpk dialog to close it. You are prompted to save the package.
  7. Save the package to your projects directory.

To test the button:

  1. Choose File > New Application.
  2. From the ActiveX page of the Tool Palette, locate your button and place it on the form. The button displays on the form.

See Also