unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls;type
TForm1 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormDblClick(Sender: TObject);
var
Button: TButton;
begin
Button := TButton.Create(Self);
with Button do
begin
Parent := Self;
Caption := 'Button';
Top := 0;
Left := 0;
end;
end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls;type
TForm1 = class(TForm)
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormDblClick(Sender: TObject);
var
Button: TButton;
begin
Button := TButton.Create(Self);
with Button do
begin
Parent := Self;
Caption := 'Button';
Top := 0;
Left := 0;
end;
end;end.
var
myb:tbutton;begin
myb:=tbutton.create(self);
myb.left:=40;
myb.top:=40;myb.width:=40;
myb.caption:='动态生成';
myb.parent:=self;
end;
你自己定义事件把
然后新建的butong.onclick:=btnonclick;
就可以了
var pButton:^TButton;
begin
new(pButton);
pButton:=TButton.create(self)
//下面是设置一些她的属性
end;
//myClick是你自己定义的一个事件处理方法!
//==============================================================================
//动态创建控件******************************************************************
//==============================================================================
function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl;
begin
if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then
begin
Result := nil;
exit;
end;
Result := Owner.FindComponent(CompName) as TControl;
if Result=nil then
begin
Result := CompType.Create(Owner);
with Result do
begin
if Owner is TwinControl then
begin
SetBounds(Left,Top,Width,Height);
Parent := TwinControl(Owner);{如果是可视构件,则显示之}
if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点}
end;
end;
Result.Name := CompName;
end
else {Result<>Nil}
if not(Result is CompType) then
begin
Result := nil;
Exit;
end;
Result.Visible := True;
end;
{ 对于未知数量的控件组,利用TList
var ControlList: Tlist; CreateNum: integer;
const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。
var i:integer; APoint: Pointer;
ControlList := TList.Create;
ControlList.Clear;
CreateNum := 10;
for i:=1 to CreateNum do
begin
APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建
ControlList.Add(APoint);
end;
TButton(ControlList.Items[i]).Caption := 'XXXX';}
第一:你要生成什么样的控件;X:=T?.Create(Application)
第二:你想要把它放在那个容器上;X.Parent:=?
第三:你想要把它摆放在容器的什么地方,坐标以容器为基准(顶部X.Top:=?,左边距X.Left:=?),大小是多少(宽X.Width:=?,高X.Height:=?)!;
第四:把相应的属性设置一下(比如:X.Color:=?,X.Font.Name:=?...);
第五:显示它,X.Show;