随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  btn: TButton;
begin
  {如果 Panel1 不存在则退出}
  if Panel1 = nil then Exit;

  {第一次 btn 依附窗体创建, 也就是它的 Owner 属性是窗体}
  btn := TButton.Create(Self);
  btn.Parent := Self;
  btn.Caption := btn.Owner.ClassName + '- btn';
  btn.Left := 0;
  btn.Top := 0;

  {第二次 btn 依附 Panel1 创建, 也就是它的 Owner 属性是 Panel1}
  btn := TButton.Create(Panel1);
  btn.Parent := Self;
  btn.Caption := btn.Owner.ClassName + '- btn';
  btn.Left := 0;
  btn.Top := btn.Height;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  {当 btn 做依附的 Owner 属性对象销毁时, 它也随之销毁}
  Panel1.Free;
end;

end.

//效果图:


posted on 2008-03-02 22:40  万一  阅读(...)  评论(...编辑  收藏