我重新给你写一个 var BtnList:array of Tbutton; procedure xxx.Onlick(); begin SetLength(BtnList,Length(Btnlist)+1); //add one to btnlist; Btnlist[high(btnlist)]:= TButton.Create(self); //self 这里是指xxx BtnList[High(btnlist)].parent := (Sender as Tbutton).Parent; Btnlist[].属性 := (Sender as Tbutton).属性... Btnlist[].left := ... Btnlist[].top := ... end; 在程序退出的时候,最好将Btnlist销毁 procedure OnClose(); var i:; begin //这句其实我认为是没有必要的,因为form在销毁的时候会自动销毁以它为owner的所有控件。但是为了安全起见,最好还是加上。 for i:=0 to high(Btnlist) do btnlist[i].free; //释放btnlist的空间 Btnlist := nil; end;关于动态数组的帮组,可以查关键字"array"
克隆控件 procedure TForm1.ClickComp(Sender: TObject); var ControlText: string; begin with TControlClass (Sender.ClassType).Create (self) do begin Parent := (Sender as TControl).Parent; Left := (Sender as TControl).Left + 10; Top := (Sender as TControl).Top + 10; SetLength (ControlText, 50); (Sender as TControl).GetTextBuf( PChar(ControlText), 50); ControlText := PChar(ControlText) + ' *'; SetTextBuf (PChar (ControlText)); end; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;type TForm1 = class(TForm) Button1: TButton; ScrollBox1: TScrollBox; Label1: TLabel; CheckBox1: TCheckBox; Label2: TLabel; Button2: TButton; Edit1: TEdit; BitBtn1: TBitBtn; SpeedButton1: TSpeedButton; GroupBox1: TGroupBox; RadioButton1: TRadioButton; RadioButton2: TRadioButton; procedure Button1Click(Sender: TObject); procedure ClickComp(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); var AForm: TForm; begin // clone the 'self' object Application.CreateForm ( TFormClass(Self.ClassType), AForm); // move the form and show it AForm.Left := Left + 50; AForm.Top := Top + 50; AForm.Show; end;procedure TForm1.ClickComp(Sender: TObject); var ControlText: string; begin with TControlClass (Sender.ClassType).Create (self) do begin Parent := (Sender as TControl).Parent; Left := (Sender as TControl).Left + 10; Top := (Sender as TControl).Top + 10; SetLength (ControlText, 50); (Sender as TControl).GetTextBuf( PChar(ControlText), 50); ControlText := PChar(ControlText) + ' *'; SetTextBuf (PChar (ControlText)); end; end;end.
//============================================================================== //复制控件(支持复制子控件和事件)********************************************** //============================================================================== function CopyComponent(Source: TComponent; Parent: TComponent; Owner: TComponent): Boolean; { 返回复制元件是否成功 } var vMemoryStream: TMemoryStream; vComponent: TComponent; vPropList: PPropList; vPropInfo: PPropInfo; vReader: TReader; i: Integer; begin Result := false; vMemoryStream := TMemoryStream.Create; vReader := TReader.Create(vMemoryStream, 256); try try vMemoryStream.WriteComponent(Source); vMemoryStream.Position := 0; vReader.Parent := Parent; vComponent := vReader.ReadRootComponent(nil); for i:=1 to GetPropList(Source, vPropList) do begin vPropInfo := vPropList^[i-1]; if vPropInfo^.PropType^.Kind = tkMethod then SetMethodProp(vComponent, vPropInfo^.Name, GetMethodProp(Source, vPropInfo^.Name)); end; except Result := true; Exit; end; finally vReader.Free; vMemoryStream.Free; end; if Source is TWinControl then for i:=1 to TWinControl(Source).ControlCount do if not CopyComponent(TWinControl(Source).Controls[i-1], vComponent, Owner) then Exit; Result := true; end;
button2.parent=your_form;
button2.属性1:=button1.属性1;
button2.属性2:=button1.属性2;
.....
但是位置的属性不要一样啊!
button2:=TButton.Create;
button2.parent:=Self;
button2.visible:=true;
button2.left:=button1.left;
button2.top:=button1.top;
button2.onclick:=button1.onclick;
這樣把事件也給它了。
button2:=TButton.Create; //创建一个按钮
button2.parent:=Self; //以下为设置一些其它属性
button2.visible:=true;
button2.left:=button1.left;
button2.top:=button1.top;
button2.onclick:=button1.onclick;//这样就ok了。
我这样回去试了试,还是不行,总是显示一个按钮,再按新产生的按钮就什么也没 有了,不知道是增么回事,.left,.top属性我也都设为不同的了,还是不行,请高手指点!!!!!
我这样回去试了试,还是不行,总是显示一个按钮,再按新产生的按钮就什么也没 有了,不知道是增么回事,.left,.top属性我也都设为不同的了,还是不行,请高手指点!!!!!
var button2:TButton; //定义一个对象
这个定义必须放在全局里面,或者是说不可以放在click事件里面,
button2:=TButton.Create; //创建一个按钮
button2.parent:=Self; //以下为设置一些其它属性
button2.visible:=true;
button2.left:=button1.left;
button2.top:=button1.top;
button2.onclick:=button1.onclick;//这样就ok了。
;
var
BtnList:array of Tbutton;
procedure xxx.Onlick();
begin
SetLength(BtnList,Length(Btnlist)+1); //add one to btnlist;
Btnlist[high(btnlist)]:= TButton.Create(self); //self 这里是指xxx
BtnList[High(btnlist)].parent := (Sender as Tbutton).Parent;
Btnlist[].属性 := (Sender as Tbutton).属性...
Btnlist[].left := ...
Btnlist[].top := ...
end;
在程序退出的时候,最好将Btnlist销毁
procedure OnClose();
var i:;
begin
//这句其实我认为是没有必要的,因为form在销毁的时候会自动销毁以它为owner的所有控件。但是为了安全起见,最好还是加上。
for i:=0 to high(Btnlist) do
btnlist[i].free;
//释放btnlist的空间
Btnlist := nil;
end;关于动态数组的帮组,可以查关键字"array"
procedure TForm1.ClickComp(Sender: TObject);
var
ControlText: string;
begin
with TControlClass (Sender.ClassType).Create (self) do
begin
Parent := (Sender as TControl).Parent;
Left := (Sender as TControl).Left + 10;
Top := (Sender as TControl).Top + 10;
SetLength (ControlText, 50);
(Sender as TControl).GetTextBuf(
PChar(ControlText), 50);
ControlText := PChar(ControlText) + ' *';
SetTextBuf (PChar (ControlText));
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;type
TForm1 = class(TForm)
Button1: TButton;
ScrollBox1: TScrollBox;
Label1: TLabel;
CheckBox1: TCheckBox;
Label2: TLabel;
Button2: TButton;
Edit1: TEdit;
BitBtn1: TBitBtn;
SpeedButton1: TSpeedButton;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure ClickComp(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
AForm: TForm;
begin
// clone the 'self' object
Application.CreateForm (
TFormClass(Self.ClassType), AForm);
// move the form and show it
AForm.Left := Left + 50;
AForm.Top := Top + 50;
AForm.Show;
end;procedure TForm1.ClickComp(Sender: TObject);
var
ControlText: string;
begin
with TControlClass (Sender.ClassType).Create (self) do
begin
Parent := (Sender as TControl).Parent;
Left := (Sender as TControl).Left + 10;
Top := (Sender as TControl).Top + 10;
SetLength (ControlText, 50);
(Sender as TControl).GetTextBuf(
PChar(ControlText), 50);
ControlText := PChar(ControlText) + ' *';
SetTextBuf (PChar (ControlText));
end;
end;end.
//复制控件(支持复制子控件和事件)**********************************************
//==============================================================================
function CopyComponent(Source: TComponent; Parent: TComponent; Owner: TComponent): Boolean; { 返回复制元件是否成功 }
var
vMemoryStream: TMemoryStream;
vComponent: TComponent;
vPropList: PPropList;
vPropInfo: PPropInfo;
vReader: TReader;
i: Integer;
begin
Result := false;
vMemoryStream := TMemoryStream.Create;
vReader := TReader.Create(vMemoryStream, 256);
try
try
vMemoryStream.WriteComponent(Source);
vMemoryStream.Position := 0;
vReader.Parent := Parent;
vComponent := vReader.ReadRootComponent(nil);
for i:=1 to GetPropList(Source, vPropList) do
begin
vPropInfo := vPropList^[i-1];
if vPropInfo^.PropType^.Kind = tkMethod
then SetMethodProp(vComponent, vPropInfo^.Name, GetMethodProp(Source, vPropInfo^.Name));
end;
except
Result := true;
Exit;
end;
finally
vReader.Free;
vMemoryStream.Free;
end;
if Source is TWinControl then
for i:=1 to TWinControl(Source).ControlCount do
if not CopyComponent(TWinControl(Source).Controls[i-1], vComponent, Owner) then Exit;
Result := true;
end;