unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var btn: TButton; begin btn := TButton(Sender); if btn.Parent = Self then btn.Parent := Panel1 else btn.Parent := Self; btn.Left := (btn.Parent.ClientWidth - btn.Width) div 2; btn.Top := (btn.Parent.ClientHeight - btn.Height) div 2; Text := Format('窗体中控件数: %d', [Self.ControlCount]); end; end.
//效果图: