ne, manipulates the VCL control, and forwards events that the VCL control fires to its container site;
the VCL object interacts with and is embedded into its parent window, and fires events back to the controller object.
Figure 3 shows a diagram of the three objects and their relationship to each other and their environments.
Figure 3. The DAX object architecture
Delphi''''s VCL class frameworks provide classes that implement these relationships: TActiveXFactory, TActiveXControl, and TWinControl. To implement a class derived fromTWinControl, you will need to create a new controller class derived from TActiveXControl. This class is the subject of the next section.
The ActiveX Control Implementation File
This file contains the main implementation code of our ActiveX control''''s ActiveX controller object. This is the object that defines an automation interface and implements the OLE automation-style properties, methods and events.
Let''''s walk through the file and examine the interesting lines of code:
unit ButtonImpl1;
interface
uses Windows, ActiveX, Classes, Controls, Graphics, Menus, Forms, StdCtrls, ComServ, StdVCL, AXCtrls, ButtonXControlLib;
The ActiveX unit is the unit that defines all the system interfaces and data types. It''''s like the OLE2 unit in Delphi 2, except that it''''s implemented using the new language features. The OLE2 unit is still around for compatibility with older code, but any new ActiveX code you write should be written using ActiveX.
AXCtrls defines the Delphi ActiveX class hierarchy, also called DAX.
The ButtonXControlLib unit is the Pascal-language version of the server''''s type library. It defines all the interfaces that are available to any object in the server. Normally you would never edit this file, since it is regenerated from the type library every time you edit and save the type library. Instead, you should edit the type library directly using Delphi''''s Type Library Editor. type TButtonX = class(TActiveXControl, IButtonX)
This clause defines an object type, TButtonX, that will be used to implement the controller object. TActiveXControl is the base class of all ActiveX controls and is implemented in the AXCtrls unit. The statement also says that the class implements IButtonX, which is the control''''s automation interface defined in the type library. private { Private declarations }
FDelphiControl: TButton;
This private member points to the VCL control. It gets initialized in the InitializeControl method, below. In code that appears below, this member is used to get and set properties, call methods, and do other operations on the VCL object. FEvents: IButtonXEvents;
This is a pointer to the container''''s event sink. IButtonXEvents is a dispinterface, not a dual interface, so what is stored is really an IDispatch pointer. This value gets set when the EventSinkChanged method is called, when the control is inserted or removed from a container. FEventSink can be nil at various points in your program''''s execution, so always be aware of this. In fact, your control could be inserted into a container that cares nothing about events, so FEventSink could be nil all the time.
Note that while the DAX class library supports multicast events, it is far easier to write your control to fire unicast events. This works fine for ActiveX controls, where the control is likely to fire events only to its container. procedure ClickEvent(Sender: TObject);
procedure KeyPressEvent(Sender: TObject; var Key: Char);
These are declarations for the event handler proxies. I''''ll discuss these below, where they are implemented. protected
{ Protected declarations }
procedure InitializeControl; override;
procedure EventSinkChanged(const EventSink: IUnknown); override;
procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
The preceding three methods declare implementations of three overridable virtual methods. These are discussed below. function Get_Cancel: WordBool; safecall;
function Get_Caption: WideString; safecall;
function Get_Cursor: Smallint; safecall;
function Get_Default: WordBool; safecall;
function Get_DragCursor: Smallint; safecall;
function Get_DragMode: TxDragMode; safecall;
function Get_Enabled: WordBool; safecall;
function Get_Font: Font; safecall;
function Get_ModalResult: Integer; safecall;
function Get_Visible: WordBool; safecall;
These methods are property getter methods for the control. These methods come from the IButtonX interface. Note that all these automation methods are declared using the safecall calling convention. Safecall is the ObjectPascal convention used for declaring dual interface compatible automation methods. Safecall guarantees that if an exception is thrown it will be caught and returned as an OLE error, following OLE calling conventions. It also copies the return value into a return parameter slot, which is declared as an out parameter in the type library.
procedure Click; safecall;
This method is the only public method a TButton exposes that can be published via OLE automation. Most of TButton''''s public methods are internal to VCL''''s implementation or don''''t make sense for the object to provide for automation. For example, the SendToBack method, which is public in TButton''''s ancestor class TWinControl, is a method that should be provided by the container. This method is first declared in the IButtonX interface.
procedure Set_Cancel(Value: WordBool); safecall;
procedure Set_Caption(const Value: WideString); safecall;
procedure Set_Cursor(Value: Smallint); safecall;
procedure Set_Default(Value: WordBool); safecall;
procedure Set_DragCursor(Value: Smallint); safecall;
procedure Set_DragMode(Value: TxDragMode); safecall;
procedure Set_Enabled(Value: WordBool); safecall;
procedure Set_Font(const Value: Font); safecall;
procedure Set_ModalResult(Value: Integer); safecall;
procedure Set_Visible(Value: WordBool); safecall;
These methods are the property setter methods for the control, and are also defined in the IButtonX interface. They each take a single parameter, which is the new value for the property. end;
implementation
{ TButtonX }
procedure TButtonX.InitializeControl;
This method is called after the control is created, but before the control is shown or inserted into its container. The main purpose of this method is to establish the connection between the COM controller object and the VCL object. In the implementation of this virtual method, the controller gets a pointer to the VCL object, and then hooks its event proxies into the VCL object. begin
FDelphiControl := Control as TButton;
Control is a property (of type TWinControl) declared in TActiveXControl, that is initialized before InitializeControl is called. Of course, it really points to a TButton control, since that''''s what we want this ActiveX control to implement. This line of code coerces the TWinControl pointer back into a TButton, and stores that pointer in this object. FDelphiControl.OnClick := ClickEvent;
FDelphiControl.OnKeyPress := KeyPressEvent;
These lines bind the VCL events in the control to this object''''s event handler proxy methods. This ensures that when the VCL control fires events, this object will receive them. I''''ll describe the detail in the ClickEvent and KeyPressEvent implementations, but obviously the control will forward the event to its container, using the ActiveX event protocol. Bug: The wizard should have generated code for the standard events, and should have bound OnKeyPress to TActiveXControl.StdKeyPressEvent, and OnClick to StdClickEvent. By the time you read this, a fix may be available for the wizard. end;
procedure TButtonX.EventSinkChanged(const EventSink: IUnknown);
begin
FEvents := EventSink as IButtonXEvents;
This code receives the event sink that the container provided, and remembers it in the FEvents member. FEvents will be used later to fire events to the object''''s container. IButtonXEvents is the control''''s event dispinterface, which is declared as the default source interface in the type library. end;
procedure TButtonX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
{ Define property pages here. Property pages are defined by calling DefinePropertyPage with
the class id of the page. For example, DefinePropertyPage(Class_ButtonXPage); }
This protected method starts with no actual implementation code. It provides you with a means of enumerating the property pages that you want shown for your control. Since your project initially has no property pages, this method is left blank, with instructions on how to fill it in. I''''ll come back to this topic later, when we discuss property pages.
end;
Implementing Property Get and Set Methods
The following are typical property getter and setter methods. All of these follow the same basic pattern: they''''re safecall OLE automation method implementati 上一页 [1] [2] [3] [4] [5] [6] [7] [8] 下一页 [办公软件]excel中的VBA中的With语句的使用介绍及实例 [系统软件]OLE with the internet explorer [Delphi程序]Delphi深度探索-数据库明了的ActiveX控件 [Delphi程序]override deal with window closing in database … [Delphi程序]DELPHI实现activex控件的限制 [Delphi程序]Delphi使用VB编写的ActiveX控件全攻略 [Delphi程序]Delphi使用VB6编写的ActiveX控件??? [Delphi程序]MediaPlayer9 ActiveX 攻略(原创) [VB.NET程序]Socket Programming with VB [VB.NET程序]Managing Windows with WMI
|