转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Building ActiveX Controls with Delphi 3         ★★★★

Building ActiveX Controls with Delphi 3

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5175 更新时间:2009/4/23 18:31:11
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]  下一页


[办公软件]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
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台