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
- Create an ActiveX library project for an ActiveX button control.
- Register the ActiveX button so its icon can be displayed in the toolbar.
- Create a package for the ActiveX button.
- Install the package.
- Test the ActiveX button.
To create an ActiveX library project for an ActiveX button control
- Create a directory on your local drive for the ActiveX project. Give it a name that you can find easily, for example, ActiveX.
- Choose FileNewOther and select the ActiveX page in the New Items dialog box.
- 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.
- Choose FileNewOther again.
- On the ActiveX page, double-click ActiveX Control. The ActiveX Control Wizard displays.
- In the Component Name drop-down, select TButton.
- 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.
- 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.
- Click OK to start the new ActiveX Library project.
To register the ActiveX button
- 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.
- Choose RunRegister 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.
- Click OK.
To create a new package for the ActiveX button
- Choose FileNewOther to create a new package. The New Items dialog displays.
- Double-click Package on the New page to display the Package - package.dpk dialog and click Add.
- On the Add unit tab of the Add dialog, browse to your project directory.
- Select the ButtonXControl1_TLB.pas file, and click Open.
- 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
- In the Package - package.dpk dialog, select rtl.dcp, and click Add.
- 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.
- In the Package - package.dpk dialog, select vcl.dcp, and click Add.
- 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.
- In the Package - package.dpk dialog, click Compile to compile the package. A dialog displays, indicating that the package has been installed. Click OK.
- Click the X in the upper right corner of the Package - package.dpk dialog to close it. You are prompted to save the package.
- Save the package to your projects directory.
To test the button
- Choose FileNew Application.
- From the ActiveX page of the Tool Palette, locate your button and place it on the form. The button displays on the form.