封装TMyPanel类:
type
  TMyPanel = Class(TPanel)
    MyLabel: TLabel;
    MyComBoBox: TComboBox;
    MyBitBtn: TBitBtn;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
end;implementation{ TMyPanel }constructor TMyPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width:= 500;
  Height:= 200;
  Left:= 50;
  top:= 50;
  MyLabel:= TLabel.Create(Self);
  MyLabel.Left:= 10;
  MyLabel.Top:= 10;
  MyLabel.Caption:= 'MyLabel';
  MyLabel.Parent:= Self;
  MyComBoBox:= TComBoBox.Create(Self);
  MyComBoBox.Left:= 100;
  MyComBoBox.Top:= 10;
  MyComBoBox.Parent:= Self;
  MyBitBtn:= TBitBtn.Create(Self);
  MyBitBtn.Left:= 160;
  MyBitBtn.Top:= 60;
  MyBitBtn.caption:='bitbtn1';
  MyBitBtn.Parent:= Self;
end;destructor TMyPanel.Destroy;
begin
  MyLabel.Free;
  MyComBoBox.Free;
  MyBitBtn.Free;
  inherited Destroy;
end;-------------------------------------------------------------
在form1中调用MyPanel
procedure TForm1.FormCreate(Sender: TObject);
begin
  MyPanel:= TMyPanel.Create(self);
  MyPanel.Parent:= Self;
end;
如果要省略 MyPanel.Parent:= Self;那么这个类怎么改造?要能正常显示在form1中

解决方案 »

  1.   

    在构建那里加 show 看看
      

  2.   


    constructor TMyPanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Width:= 500;
      Height:= 200;
      Left:= 50;
      top:= 50;
      Parent:= Self;  // 加上
      ...
      ...
    end;
      

  3.   

    测试了一下,可以显示在窗体上,完整代码:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, Buttons;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  TMyPanel = Class(TPanel)
        MyLabel: TLabel;
        MyComBoBox: TComboBox;
        MyBitBtn: TBitBtn;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      end;var
      Form1: TForm1;implementation{$R *.dfm}constructor TMyPanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Width:= 500;
      Height:= 200;
      Left:= 50;
      top:= 50;
      MyLabel:= TLabel.Create(Self);
      MyLabel.Left:= 10;
      MyLabel.Top:= 10;
      MyLabel.Caption:= 'MyLabel';
      MyLabel.Parent:= Self;
      MyComBoBox:= TComBoBox.Create(Self);
      MyComBoBox.Left:= 100;
      MyComBoBox.Top:= 10;
      MyComBoBox.Parent:= Self;
      MyBitBtn:= TBitBtn.Create(Self);
      MyBitBtn.Left:= 160;
      MyBitBtn.Top:= 60;
      MyBitBtn.caption:='bitbtn1';
      MyBitBtn.Parent:= Self;
    end;destructor TMyPanel.Destroy;
    begin
      MyLabel.Free;
      MyComBoBox.Free;
      MyBitBtn.Free;
      inherited Destroy;
    end;                procedure TForm1.FormCreate(Sender: TObject);
    var
      MyPanel: TMyPanel;
    begin
      MyPanel:= TMyPanel.Create(Self);
      MyPanel.Parent:= Self;
      MyPanel.SetBounds(0, 0, MyPanel.Width, MyPanel.Height);
    end;end.
      

  4.   

    为什么要省略MyPanel.Parent:= Self?可以这样修改代码:constructor TMyPanel.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      if AOwner is TWinControl then Parent := AOwner as TWinControl;
      ...
    end;
      

  5.   

    之前在上面写错,写成下面那样子constructor TMyPanel.Create(AOwner: TComponent; FParent: TWinControl);
    begin
      inherited Create(AOwner);
      Parent:= TWinControl(FParent);
    ....