类引用是指类的类型。即对类引用变量赋值不是字符串,也不是数值,而是一个类。
如:
type
TForm1 = class(TForm)
CreateButtonBtn: TButton;
CreateLabelBtn: TButton;
procedure CreateButtonBtnClick(Sender: TObject);
procedure CreateLabelBtnClick(Sender: TObject);
private
{ Private declarations }
procedure CreateControl(FClass: TWinControlClass; Cap: String);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateButtonBtnClick(Sender: TObject);
begin
CreateControl(TButton, 'This is a button');
end;
procedure TForm1.CreateLabelBtnClick(Sender: TObject);
begin
CreateControl(TCheckBox, 'This is a check box');
end;
procedure TForm1.CreateControl(FClass: TWinControlClass; Cap: String);
begin
Randomize;
with FClass.Create(Self) do begin
Parent := Self;
SetWindowText(Handle, PChar(Cap));
Width := 150;
Height := 20;
Left := Random(Self.Width - Width);
Top := Random(Self.Height - Height);
end;
end;