Creating a Windows VCL ActiveX Active Form
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, for example.
This procedure uses the ActiveX Active Form wizard to create an ActiveX Active Form containing two components. The wizard uses a VCL Form to create the Active Form. To test the Active Form, you can open it by using a simple HTML file and a Web browser. This procedure consists of the following major steps:
- Create an ActiveX Library project and an ActiveX Active Form.
- Add a button and event handling code.
- Test the active form.
Contents
To create an Active X Library project and an ActiveX Active Form
- Create a directory on your local drive for the ActiveX project. Give it a name that is easy to find, for example, ActiveX.
- Choose:
- File > New > Other > C++Builder > ActiveX > ActiveX Library
- File > New > Other > Delphi > ActiveX > ActiveX Library
- This creates a Dynamic Link Library [DLL] project that you can use to host in-process ActiveX Objects.
- Choose either:
- File > New > Other > C++Builder > ActiveX > Active Form
- File > New > Other > Delphi > ActiveX > Active Form
- The Active Form Wizard displays.
- Accept the default settings and click OK. The wizard generates the code needed to implement the ActiveX control and adds the code to the project. An ActiveX Active Form displays.
To add a button and event to the Active Form
- From the Standard page of the Tool Palette, add TEdit and TButton components to the form.
- Select the button.
- On the Events tab in the Object Inspector, double-click the OnClick event. The Code Editor opens with the cursor in place in the event handler block for either:
- TActiveFormX.Button1Click (Delphi)
- TActiveFormX::Button1Click() (C++)
- Enter the following code at the cursor:
- Delphi:
ShowMessage(Edit1.Text);
- C++:
ShowMessage(Edit1->Text);
- Save the project files to your ActiveX directory.
To test the Active Form
After you have created, built, and registered your Active Form, you can host it in a Web browser by using a simple HTML file such as this one:
<html> <body> <h1>Active Form<h1> <object classid="clsid:3E46CAEF-520F-4B9F-B152-00FF2E006F88" codebase="AnActiveForm.ocx" width=700 height=250 align=center hspace=0 vspace=0> </object> </body> </html>
In the HTML above, the two items that you must change are the values of the:
classid
(the GUID of the Active Form's CoClass)codebase
(the name of the Active Library that contains the Active Form; can have either a .ocx or .dll extension)
In the <project>_TLB unit, find the
classid
(the underlying GUID value). Here is the code for the above example:// *********************************************************************// // GUIDS declared in the TypeLibrary. Following prefixes are used: // Type Libraries : LIBID_xxxx // CoClasses : CLASS_xxxx // DISPInterfaces : DIID_xxxx // Non-DISP interfaces: IID_xxxx // *********************************************************************// const CLASS_TestActiveForm: TGUID = '{3E46CAEF-520F-4B9F-B152-00FF2E006F88}';
So the
classid
in this example is 3E46CAEF-520F-4B9F-B152-00FF2E006F88.Find the correct
codebase
value by looking at the Factory's registration code. Thecodebase
is the fourth parameter when the factory is constructed, and has the "Class_" prefix:initialization TActiveFormFactory.Create( ComServer, TActiveFormControl, TTestActiveForm, Class_TestActiveForm, 1, '', OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL, {$IFDEF SINGLE_THREADED} tmSingle); {$ELSE} tmApartment); {$ENDIF}
So the
codebase
here is TestActiveForm.Edit the HTML file and insert your specific values for
codebase
andclassid
. Save the file.Using a Web browser such as Internet Explorer, open the HTML file.
The Active Form displays in the browser window.
Click the button. A pop-up dialog displays the text in the Edit box.
Change the text, and click the button again. The new text you entered displays in the pop-up.