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

Building ActiveX Controls with Delphi 3

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5171 更新时间:2009/4/23 18:31:11
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
    教程录入: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……
    咸宁网络警察报警平台