I. Introduction to the Application of ActiveX:
ActiveX controls, which are commonly called OCX controls, are part of ActiveX technology. ActiveX is a Component Object model COM-based technology introduced by Microsoft, including a series of extensions and enhancements to the Windows 32-bit Application programming interface (Win32 API) and Component Object model, with the goal of integrating the computer desktop environment with the Internet environment. While protecting existing development investments in Windows technology. Microsoft's ActiveX technology is fundamentally a modified OCX technology that allows it to cross the internet, mostly using WWW to pass controls.
ActiveX controls can be integrated into many ActiveX-enabled applications or directly to extend the functionality of the app, including C + + Builder, Delphi, Visual DBASE, Visual Basic, Visual C + +, Internet Explorer, Netscape Navigator, and more. ActiveX controls can also be used on the Internet to publish information and handle interactions with users on a Web page, but you need to use an ActiveX-enabled browser to read these pages. There are a growing number of ActiveX controls that have been developed, and because of the good features of ActiveX controls, we can choose to use the appropriate controls to speed up application development when programming. It is no exaggeration to say that ActiveX controls are an essential weapon in Windows platform programming.
Using ActiveX controls is relatively straightforward. However, developing ActiveX controls is not an easy task, and you must be familiar with, and not just understand, ActiveX technology to develop good ActiveX controls. And the COM, OLE and other ActiveX technology for the general programmer, is more complex difficult to understand. So, the developers of ActiveX controls are usually "veteran" level programmers.
But if you have Inprise company's Borland Delphi, that's another matter. As one of the best program development tools on the Windows platform, Delphi provides powerful support for the development of ActiveX controls, making it easy to develop ActiveX controls. Delphi developers simply need to understand the simple knowledge of some ActiveX controls and can use the tools provided by Delphi to quickly develop their own ActiveX controls. This article describes in detail the techniques provided by Delphi to quickly develop ActiveX controls.
Ii. related knowledge of ActiveX controls
To develop ActiveX controls, let's take a quick look at some of the relevant knowledge of ActiveX controls. In Delphi, an ActiveX control consists of the following parts: VCL (visual Component library) controls, properties, methods, events, and one or more related type libraries, each of which has its own role.
Each section is now described as follows:
1. VCL Components
In Delphi, an ActiveX control is obtained after being packaged by one or more VCL components. Therefore, to use Delphi to develop an ActiveX control, you must first develop or select the VCL component to be packaged. It is for this reason that we can package a large number of excellent VCL components in Delphi into ActiveX controls, which can be used in other development tools (such as Visual Basic, Visual C + +) that are not able to use the VCL artifacts directly.
However, the VCL component that can be used to do ActiveX controls in Delphi must be a descendant of Twincontrol. If you want to use a Tgraphicontrol subclass to do an ActiveX control, in most cases you only need to change its base class to Tcustomcontrol. Some VCL artifacts are registered as non-ActiveX controls, such as Editcontrol, which also cannot be ActiveX controls.
2. Type library
The type library contains the type definition information for the control, provides more details than the interface, and is a way for the control to provide service information to its host program. The purpose of a type library is to provide information about the artifact objects it contains to other application programming tools. When developing an ActiveX control, Delphi stores the type library information in a. tlb file and a corresponding Pascal file (. The Pascal language version of the TLB file), and the compile-time type library will be linked as a resource into the DLL or EXE.
Elements of a type library include:
(1) Type library information: includes the overall information and characteristics of the type library, and other types of libraries referenced by the current type library;
(2) Component class: Represents the entire ActiveX control, including the interface provided to the customer application;
(3) Interface properties and methods, events;
(4) enum type: A related constant used as a data type that can be used to define properties and methods elsewhere in the type library.
Delphi provides a type library editor for observing or modifying a type library, which you can use to add, modify, and delete various elements of a type library. As you can see on the interface of the type library, the left side is a tree structure, the members of various elements appear as child nodes, and the right side is to display a different tab based on the element type selected on the left, and you can edit some properties of the element.
3. Properties, methods, and events
The properties, methods, and events of the VCL artifacts naturally become properties, methods, and events for ActiveX controls. A control's properties, such as color, label, and so on; The control's method is to request the control to perform certain actions; The event is a notification to the host program that something has happened.
4. Property Pages
The property page with the ActiveX control allows the consumer of the control to view and modify the properties of the control. You can bundle several properties on a property page, or you can provide only one property page for one property.
III. rapid development of ActiveX controls
Let's use Delphi to make a specific ActiveX control.
Let's say that the problem is that when developing an application using Visual C + + (VC), you need to use a grid control to handle the interface, and using the FlexGrid control provided by the VC is a hassle to find in Delphi The Tstringgrid component is very suitable. But Tstringgrid is Delphi in the VCL components, and can not be directly used by VC, how to do? Don't worry, we just have to wrap the Tstringgrid into a VC can be used by the ActiveX control can solve the problem.
Delphi provides two wizards for the development of ActiveX controls:
The 1>, ActiveX Control Wizard lets you wrap a VCL component into an ActiveX control. The following will be used in this wizard.
The 2>, ActiveForm Wizard can wrap several VCL components into an ActiveX control or develop a complete ActiveX application.
Iv. Examples of development:
The main steps for wrapping Tstringgrid as an ActiveX control in Delphi:
1, click "File", "new" and "Other" menu item, open "New Items", select "ActiveX";
2. Select the "ActiveX Library" tab and click OK to create the project.
3, click "File", "new" and "Other" menu item, open "New Items", select "ActiveX";
4, select the "ActiveX Control" tab, click OK, open the "ActiveX Control Wizard" window;
5, in the "VCL class Name" combo box select the ' Vcl ' class (such as Tstringgrid), click OK;
6, add code;
7. Run the program to generate the OCX file.
8, in the Delphi click "Component", "Import ActiveX Control" menu item, open the Import ActiveX window, click "Add" in the window, then select the OCX file, Then click the Install button to set up the component. If successful, the component will be displayed in the ActiveX Components page.
1. First, to start the ActiveX Control Wizard, create an ActiveX project
First, to start the ActiveX Control Wizard, select the new command in the File menu, select the ActiveX tab in the Popup dialog box, and then double-click the ActiveX control icon. The first field in the wizard requires you to select a VCL widget to be packaged, and click Select Tstringgrid. Several other domains take the default values, and if you need additional information, such as version information, you can also choose. Click the OK button and the wizard will generate a Delphi ActiveX Control development Project STRINGGRIDXCONTROL1, including the following files:
1. An ActiveX project file (DPR suffix name) that contains the code needed to start and initialize an ActiveX control. There is usually no need to modify this file.
2. A type library file (TLB suffix name) that defines and implements the interfaces, properties, methods, and events that the ActiveX control provides outward. Delphi specifically provides a Pascal version of the file (TLB extension) for this file, and automatically maintains the consistency of the two files.
3. An ActiveX implementation of the file (PAS extension), using the Delphi ActiveX Framework (DAX) to define and implement the ActiveX control, specifically implement the interface defined in the type library. This file is a regular change we need. Dax is the way Delphi implements the COM specification for ActiveX controls.
4. If you select include a "about" dialog box, there is a corresponding dialog box implementation file.
The Delphi ActiveX Control Wizard generates the appropriate properties, methods, and events for the ActiveX control based on the properties, methods, and events of the wrapped VCL artifacts. However, it does not convert the VCL component to use properties, methods, and events of non-OLE types such as collections, arrays, and so on. For example, the options property of Tstringgrid is a collection type, rowheights is an array type, and these properties are not in the properties generated by the Control Wizard. But sometimes these attributes, methods, and events are important, and you need to add some properties, methods, and events manually!
2. Add a property and an implementation method to the ActiveX control:
Now add an Browselect property to this ActiveX control, which would have been a child of the options property of Tstringgrid, controlling whether the grid is in row-selected state. In the StringGridXControl1 project, open the ActiveX implementation file Stringgridimp1.pas, select the "Edit" menu "Add to Interface" command (note: Only open the implementation file, this command can be used), Delphi will pop up the Add Interface dialog box. The interface item in the dialog box selects Properties/methods-istringgridx and then writes the declaration of the Browselect attribute in declaration (if you select the syntax Helper option in the dialog box, Delphi will always prompt you to enter the content): property browselect:wordbool; When you click OK, information about this attribute is automatically added to the control's implementation file, type library file, and the corresponding Pascal file. Because the interface of the control is bidirectional, two methods of reading and writing are used in the Pascal file to implement the properties of the control.
1>, Delphi automatically added the following declaration in the type library:
Property Browselect:wordbool read Get_browselect write set_browselect;
This shows: For Browselect This property, there is a read method Get_browselect, a write method Set_browselect. Delphi automatically adds a declaration and implementation of a read and write access method to the Browselect property in the control's implementation file:
function Get_browselect:wordbool; Safecall;
Procedure Set_browselect (Value:wordbool); Safecall;
At this time the implementation of the two methods is empty, you have to fill in the implementation of the Code:
2>, writing code in the implementation file
function TStringGridX.Get_BRowSelect:WordBool;
Begin
The following code is written by itself as needed to get the row selected now
With Fdelphicontrol do
Result: = Gorowselect in Options;
End
Procedure Tstringgridx.set_browselect (Value:wordbool);
Begin
The following code is written by itself as needed to set the row selection state
With Fdelphicontrol do
Begin
If Value Then
Options: = Options + [Gorowselect]
Else
Options: Options-[gorowselect];
End
End
3. Build ActiveX control (OCX control)
At this point, using the Type library editor to view the type library, you can see the newly added Browselect property. Well, when you're done with the previous work, choose the Build StringGridXControl1 command from the Project menu to generate the ActiveX controls we need. In the project directory, we can find stringgridxcontrol1.ocx this file, this is our ActiveX control!
4. Register the ActiveX control (OCX control) in the system
1>, in the Delphi click the "Component", "Import ActiveX Control" menu item, open the Import ActiveX window, click "Add" in the window, then select the OCX file, Then click the Install button to set up the component. If successful, the component will be displayed in the ActiveX Components page.
2>, copy it to the Windows 9x system directory or the System32 directory of Windows nt/2000. In the DOS command line state, run the command: Regsvr32.exe c:\winnt\system32\alasunsmscon.ocx (regsvr32 Stringgridxcontrol1.ocx) Register this STRINGGRIDX control in the system.
3>, in Delphi, open the Project (*.DPR) file that created the ActiveX control, and then select Run->register ActiveX Server.
Note that the 2nd and 3rd methods above only registered the ActiveX control with the Windows Messaging system, but did not add it to Delphi's VCL library. It cannot be called in other programs. We also need to do the operation in the 1th method. If you have already registered, you can find it in import ActiveX, if there is no need to click on the "Add" file to add the path.
V. Summary
Similarly, we can add more properties to the ActiveX control. You can also add new methods, events, and joins to the ActiveX control in a way that is similar to the Join property. And after joining, Delphi will automatically add corresponding methods and events to the corresponding document declaration and implementation, the specific implementation of the body has to write their own, there is no more to say.
We can note that Delphi automatically generates the wrapper code needed to wrap the VCL component into an ActiveX control. When we add new properties, methods, and events to an ActiveX control, we only need to link the ActiveX control's data to the data of the VCL component in the implementation of the interface method, or use the VCL widget to complete the behavior of the ActiveX control. There's basically no need to be concerned with the specifics of writing an ActiveX control, such as a COM protocol.
As mentioned earlier, you can also add custom property pages for ActiveX controls. There is no need to add a new property page specifically, because most ActiveX-enabled IDE programming environments already provide a property page for each ActiveX control that contains all the properties, which we can use with this property page.
With Delphi's strong support for developing ActiveX controls, we quickly got a new ActiveX control without much effort. Delphi lets developers who use it to develop ActiveX controls focus on the VCL artifacts that are the basis of ActiveX controls, without having to care too much about the details of ActiveX controls, so let ActiveX technology is not too familiar to developers can also enjoy the development of ActiveX control fun, but also greatly enriched the VC, VB development can be used in the control.
http://blog.csdn.net/zang141588761/article/details/51248269
Delphi Write ActiveX control (OCX control) knowledge and samples (with detailed steps)