例如:将VCL中的ListBox控件转换成ActiveX控件,在ActiveX控件生成后,实现单元和类型库单元为何都有二个相同的类?代码如下:
ListBoxImpl.pas
===============
unit ListBoxXImpl...type
  TListboxX=class(TActiveXControl,TListbox)
     ......
end.-----------------------------------------------
ListBoxXControl1_TLB.pasunit ListBoxXControl1_TLB
......
type
  TMyListBoxX = class(TOleControl)
     ...
end.这二个类定义和实现方式都不同,但为何这样定义?
还请大侠指点!谢谢!  

解决方案 »

  1.   

    前面一个是实现类,后面一个是引用类,如果你用DELPHI导入一个已经实现好的类,会产生一个Pas文件,跟后面的TMyListBoxX = class(TOleControl)一样
      

  2.   

    有点不明白,在使用时,是使用_Lib中的类,但有些事件方法调用时却是调用impl单元所定义的方法,在_lib的类中并没有这些事件方法.
    例:
    library MyListBoxXControl1;uses
      ComServ,
      MyListBoxXControl1_TLB in 'MyListBoxXControl1_TLB.pas',
      MyListBoxImpl1 in 'MyListBoxImpl1.pas' {MyListBoxX: CoClass},
      About1 in 'About1.pas' {MyListBoxXAbout};{$E ocx}exports
      DllGetClassObject,
      DllCanUnloadNow,
      DllRegisterServer,
      DllUnregisterServer;{$R *.TLB}{$R *.RES}begin
    end.
    =================================
    unit MyListBoxImpl1;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
      Windows, ActiveX, Classes, Controls, Graphics, Menus, Forms, StdCtrls,
      ComServ, StdVCL, AXCtrls, MyListBoxXControl1_TLB;
    type
      TMyListBoxX = class(TActiveXControl, IMyListBoxX)
      private
        { Private declarations }
        FDelphiControl: TListBox;
        FEvents: IMyListBoxXEvents;
        procedure ClickEvent(Sender: TObject);
        procedure DblClickEvent(Sender: TObject);
        procedure KeyPressEvent(Sender: TObject; var Key: Char);
        procedure DrawItemEvent(control:TWinControl;Index:integer;Rect:TRect;State:TOwnerDrawState);
      protected
        { Protected declarations }
        procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
        procedure EventSinkChanged(const EventSink: IUnknown); override;
        procedure InitializeControl; override;
        function DrawTextBiDiModeFlagsReadingOnly: Integer; safecall;
        function Get_AlignDisabled: WordBool; safecall;
        function Get_AutoComplete: WordBool; safecall;
        function Get_BevelInner: TxBevelCut; safecall;
     ......
      end;implementationuses ComObj, About1;{ TMyListBoxX }procedure TMyListBoxX.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
    begin
      {TODO: Define property pages here.  Property pages are defined by calling
        DefinePropertyPage with the class id of the page.  For example,
          DefinePropertyPage(Class_MyListBoxXPage); }
    end;procedure TMyListBoxX.EventSinkChanged(const EventSink: IUnknown);
    begin
      FEvents := EventSink as IMyListBoxXEvents;
    end;procedure TMyListBoxX.InitializeControl;
    begin
      FDelphiControl := Control as TListBox;
      FDelphiControl.OnClick := ClickEvent;
      FDelphiControl.OnDblClick := DblClickEvent;
      FDelphiControl.OnKeyPress := KeyPressEvent;
      FDelphiControl.OnDrawItem:=DrawItemEvent;
    end;
    ......========================================
    unit MyListBoxXControl1_TLB;......
      TMyListBoxXOnKeyPress = procedure(ASender: TObject; var Key: Smallint) of object;
      TMyListBoxXOnColorItem = procedure(ASender: TObject; index: Integer; var Color: Integer) of object;  TMyListBoxX = class(TOleControl)
      private
        FOnClick: TNotifyEvent;
        FOnDblClick: TNotifyEvent;
        FOnKeyPress: TMyListBoxXOnKeyPress;
        FOnColorItem: TMyListBoxXOnColorItem;
        FIntf: IMyListBoxX;
        function  GetControlInterface: IMyListBoxX;
      protected
        procedure CreateControl;
        procedure InitControlData; override;
        function Get_Items: IStrings;
        procedure Set_Items(const Value: IStrings);
      .....
      published
        .....
        property OnColorItem: TMyListBoxXOnColorItem read FOnColorItem write FOnColorItem;
      end;=========================================
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleCtrls, MyListBoxXControl1_TLB;type
      TForm1 = class(TForm)
        MyListBoxX1: TMyListBoxX;
        procedure FormCreate(Sender: TObject);
        procedure MyListBoxX1ColorItem(ASender: TObject; index: Integer;
          var Color: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.MyListBoxX1ColorItem(ASender: TObject; index: Integer;
      var Color: Integer);
    begin
      if index=2 then
        Color:=clRed;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      MyListBoxX1.Items.Add('xxxxxx');
      MyListBoxX1.Items.Add('yyyyyy');
      MyListBoxX1.Items.Add('zzzzzz');
      MyListBoxX1.Items.Add('aaaaaa');
      MyListBoxX1.Items.Add('bbbbbb');
    end;
    end.好像在使用中MyListBoxXControl1_TLB单元并没有作用。
    注册该ActiveX控件时,是否主要是使用MyListBoxImpl1单元TMyListBoxX类?而MyListBoxXControl1_TLB单元中的TMyListBoxX类有什么用呢?