打印本文 打印本文 关闭窗口 关闭窗口
Building ActiveX Controls with Delphi 3
作者:武汉SEO闵涛  文章来源:敏韬网  点击数5554  更新时间:2009/4/23 18:31:11  文章录入:mintao  责任编辑:mintao
ons for property get and set calls. Since the only thing you have to do to set a property in Delphi is to assign the value, most of these methods look like the following two methods:
function TButtonX.Get_Cancel: WordBool;
begin
  Result := FDelphiControl.Cancel;
end;
procedure TButtonX.Set_Cancel(Value: WordBool); begin FDelphiControl.Cancel := Value; end;

There are a number of cases where the property accessor code may be more complicated. When the data type of the property is an integer-derived type, the value parameter in a setter function is passed in as a SmallInt. Your code needs to typecast this number into the appropriate Pascal type type before assigning it to a Pascal property. For example, the Cursor property is of type TCursor, which is declared:

  type TCursor = -32768..32767;
The implementation of Cursor''''s getters and setters look like this:
function TButtonX.Get_Cursor: Smallint;
begin
  Result := Smallint(FDelphiControl.Cursor);
end;
procedure TButtonX.Set_Cursor(Value: Smallint); begin FDelphiControl.Cursor := TCursor(Value); end;

ActiveX string properties (BSTRs) are compatible with Delphi''''s WideString type, and must be used even if the VCL component exposes a property as an AnsiString. You can do this by converting the AnsiString value to a WideString in the getter function, and vice versa in the setter function. (in this example, remember the TCaption type is a synonym for String).

function TButtonX.Get_Caption: WideString;
begin
  Result := WideString(FDelphiControl.Caption);
end;

procedure TButtonX.Set_Caption(const Value: WideString);
begin
  FDelphiControl.Caption := TCaption(Value);
end;

Another interesting case concerns properties that have complex OLE types, such as fonts, pictures, and string lists. Since a font is a separate object that has a dispatch interface, it can be modified independently of the control, and the control needs to refresh appropriately when this happens. For example, you could say in VB:

myFont = control.Font
myFont.Facename = ''''Arial''''

In this case, the control needs to change its font to Arial and refresh the display. This necessitates that the Get_Font method should create and return an OLE object that can expose the properties of the font as OLE properties. Conversely, setting the VCL''''s property in the TFont variable should update the OLE font.

Fortunately, the DAX library provides builtin functions for handling these common types. The font property''''s getter and setter method implementations demonstrate the use of the GetOleFont and SetOleFont functions.

function TButtonX.Get_Font: Font;
begin 
  GetOleFont(FDelphiControl.Font, Result);
end;

procedure TButtonX.Set_Font(const Value: Font);
begin
  SetOleFont(FDelphiControl.Font, Value);
end;

You''''ll notice that the ActiveX Control wizard does not generate a complete list of all the properties that TButton publishes to the Delphi form designer. The wizard has decided that the Height, HelpContext, Hint, Left, Name, ParentFont, ParentShowHint, PopupMenu, ShowHint, TabOrder, Tag, Top and Width properties should not be exposed to OLE automation, because they don''''t make sense for an ActiveX control. This can be because the container implements the behavior itself using extended properties (in the case of position and tabbing properties), because ActiveX containers do not implement the behavior (ParentFont, HelpContext and hints), or because the property type is not standard OLE property type (PopupMenu).

The TActiveXControl class also contributes a number of property accessors for the properties available to any TWinControl. These include the following properties: BackColor, Caption, Enabled, Font, ForeColor, HWnd, TabStop, and Text.

Implementing Methods

Passing on automation methods to the VCL control is fairly straightforward. Simply call the appropriate method in the VCL control that is kept in FDelphiControl. Methods that have parameters may need to be modified, but this control doesn''''t have any.

procedure TButtonX.Click;
begin
  FDelphiControl.Click;
end;

Event Handling

The following two methods demonstrate how a Delphi-style event handler forwards an event to the object''''s container. The event handlers are connected to the VCL control in the InitializeObject method, above.

procedure TButtonX.ClickEvent(Sender: TObject);
begin
  if 
    FEvents <> nil then
       FEvents.OnClick;
This implementation simply passes on the event to the container''''s event sink, if it has been installed. FEvents was set in the EventSinkChanged method described above. FEvents is a dispinterface, which means it is really just an IDispatch pointer.

Historical note: When I first implemented this, FEvents was a Variant from Delphi 2 until the compiler folks had dispinterfaces working properly. Calling a method on a dispinterface works just like calling a method on a Variant that contains an IDispatch pointer, but there are two key differences. The first is performance: calling through a dispinterface binds the method''''s dispid at compile-time, eliminating the sometimes costly GetDispIDsOfNames call.

The second difference is that while ActiveX control containers expose their event sinks using an IDispatch pointer, in this case the IDispatch implementation is not required to implement GetDispIDsOfNames at all! It turns out that some containers do implement this code, but most do not. If you want your event firing to work in all containers, you must use dispinterfaces to fire the events.

end;

procedure TButtonX.KeyPressEvent(Sender: TObject; var Key: Char);
  var TempKey: Smallint;
begin
In a this case, the parameters expected for OLE events are not the same as for the Delphi events. In these cases the event handler proxy may need to massage the event''''s parameters before firing the event to the container. In this case, the OnKeyPress event passes a pointer to a SmallInt to the container, but the Delphi control passes a pointer to a Char to the event handler.

OLE events don''''t have a Sender parameter, so that parameter is dropped before passing on the event to the parent.

  TempKey := Smallint(Key); 
  if FEvents <> nil then 
    FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

TActiveXControl contributes handlers for common events like Click, DblClick, KeyDown, KeyPress, KeyUp, MouseDown, MouseMove, and MouseUp.

initialization 
  TActiveXControlFactory.Create( 
    ComServer, TButtonX, TButton, Class_ButtonX, 1, '''''''', 0);

This line of code, which gets executed when the library is loaded, creates the class factory (based on the class, TActiveXControlFactory) for the control.

ComServer is a global variable that represents the library itself. Among other things, the ComServer contains a list of all the factories that have been created in the library. The other parameters to the factory are:

TButtonX - the ActiveX implementation class defined above

TButton - the VCL control class

Class_ButtonX - the ClassID of the object. This GUID is imported from the ButtonXControlLib unit, generated from the type library.

ToolbarBitmapID - this is a resource identifier of a bitmap resource. The wizard generates a bitmap resource for each control, based on the control''''s registered icon. ActiveX containers extract this bitmap to show on their control palettes.

LicenseString - This is blank because we didn''''t select

MiscControl flags - these are a combination of OLEMISC_* values that you can use to request special container behavior. DAX always adds the following flags to any VCL-derived control: OLEMISC_RECOMPOSEONRESIZE, OLEMISC_CANTLINKINSIDE, OLEMISC_INSIDEOUT, OLEMISC_ACTIVATEWHENVISIBLE, OLEMISC_SETCLIENTSITEFIRST

end.


The Type Library

The ActiveX type library is a binary file containing the meta-data for each of the controls listed in an ActiveX library. It describes the objects in the library, the properties, methods and events and other interfaces available to each control, and the user-defined data types used for these. In addition to containing symbol names and type information, a type library contains a variety of other information, including human- readable descriptive text, a reference to a help file and GUIDs for each of these items. When you compile an ActiveX library, the type library gets copied into the DLL as a resource, where it can be loaded by any interested client program.

The ActiveX wizards generate a type library for you when you first create the ActiveX Control from a Delphi VCL, and stores the type library in a .TLB file. This library defines all the properties and methods for your ActiveX control.

For any properties or parameters that convert to OLE compatible types, the wizard generates properties and parameters using those OLE types. When your control contains enumeration properties or parameters, the wizard generates a type declaration for that enumeration in the type library. In the case where the data type is a TFont, TPicture, or TStrings, the wizard assigns the property or parameter an IFont, IPicture or IStrings type and generates adapter code to convert between the data types.

If your VCL control contains properties or parameters that aren''''t standard or adaptable, generally records or non-COM object types, the wizard will skip that data item. This doesn''''t mean that the pr

上一页  [1] [2] [3] [4] [5] [6] [7] [8]  下一页

打印本文 打印本文 关闭窗口 关闭窗口