在Delphi下怎么制作一个复合控件。比如在一个TPanel上放一个TLabel。做为一个控件?

解决方案 »

  1.   

    就继承TCUSTOMPANEL增加一个属性:TLABLE应该就可以吧unit CustomPanel1;interfaceuses
      SysUtils, Classes, Controls, ExtCtrls, StdCtrls;type
      TCustomPanel1 = class(TCustomPanel)
      private
        { Private declarations }
        MyLabel: TLabel;
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TCustomPanel1]);
    end;{ TCustomPanel1 }constructor TCustomPanel1.Create(AOwner: TComponent);
    begin
      inherited;
      MyLabel:=TLabel.Create(Self);
      with MyLabel do begin
         Parent:=Self;
         Visible:=True;
         Top:=10;
         ..
      end;
    end;destructor TCustomPanel1.Destroy;
    begin
      FreeAndNil(MyLabel);
      inherited;
    end;end.
      

  2.   

    最好还是多看些VCL的书和源代码。
      

  3.   

    新建一个Package,点击Add按钮,选择New Component页,将其Ancestor Type 项设为TPanel
    unit paneld;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,QStdCtrls;type
      tpaneld = class(tpanel)
      private
        { Private declarations }
        Flbl1:TLabel;
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create(AOwner:TComponent);override;
        destructor Destroy;override;
      published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [tpaneld]);
    end;constructor tpaneld.Create(AOwner:TComponent);
    begin
      inherited Create(AOwner);
      Flbl1 := TLabel.Create(AOwner);
      Flbl1.Caption := 'adf';
    end;destructor tpaneld.Destroy;
    begin
      Flbl1.Free;
      inherited Destroy;
    end;