For i:=1 to 10 Do Begin Button[i]:=TButton.Create(Self); Button[i].Parent:=Self; Button[i].Onclick:=BtnClkHandler; End;procedure TMain.BtnClkHandler(Sender:TObject); begin with Sender as TButton do begin //做你想做的 end; end;
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';}
一、Delphi中生成控件的两种方法 ---- 1、 Form(表单)设计中生成控件 ---- 在进行Form设计时,直接在控件工具箱选择所需控件,再设置其属性与响应事件,这种方法比较常见。 ---- 2、 程序中动态生成控件 ---- 有时候,我们需要在程序运行时动态生成控件,这样做有两大优点:一是可以增加程序的灵活性;二是如果生成控件的多少与程序中间运行结果相关,显然方法一是无法的实现的,必须用程序中动态生成方法。 ---- 程序中动态生成控件的方法分为三步,首先,定义生成的控件类型,再用Create函数生成控件,最后对控件的相关属性赋值。以TButton控件为例,步骤如下: ---- (1) 定义控件类型 var Button1:TButton; ---- (2) 生成控件 Button1:=TButton. Create(self); Button1.Parent:=Self; //一般将其父控件设置为Self,如果不设置Parent的值, 则控件不会在屏幕 //显示出来 ---- (3) 设置其它属性及定义相关事件响应函数,如Caption,Left,Top,Height,Width,Visible,Enabled,Hint和onClick事件响应函数等。 二、动态生成控件方法的应用 ---- 在开发生产调度与管理系统中,需要动态生成排产计划图,以甘特图表示,应用Shape控件来显示零件的加工状况(每道工序的加工开始时间与结束时间)是非常适合的。应用Chart控件,对加工设备利用率以三维直方图显示,非常直观。现分别将在程序中动态生成Shape控件和Chart控件的过程加以说明。 ---- 1、动态生成Shape控件显示排产计划图(甘特图) procedure TCreateMultiCharts.ProcCreateCharts; var i,j,Rows,Columns,RowSpace,ChartsHeight:Integer; ShapeChart:array of array of TShape; begin Rows:=16; //Shape控件数组行数 Columns:=8; // Shape控件数组列数 RowSpace:=20; // Shape控件行间距 ChartsHeight:=20; // Shape控件高度 SetLength(ShapeChart,Rows,Columns); //设置ShapeChart数组大小 for i:=0 to Rows do for j:=0 to Columns do begin ShapeChart[i][j]:=TShape.Create(self); with ShapeChart[i,j] do begin Parent:=Self; //此行必不可少, 否则Shape控件在屏幕显示不出 Shape:=stRectangle; // Shape控件形状为矩形 Top:=45+i*(RowSpace+ChartsHeight); Left:=Round(180+Q[i,j].StartTime); //因Q[i,j].StartTime为实数,故需进行四舍五入取整 Width:=Round(Q[i,j].Value) Height:=ChartsHeight; Brush.Color:=RandomColor; //自定义函数,说明附后 Brush.Style:=bsSolid; //设置填充方式 Enabled:=True; end; end; end; ---- 注: ---- (1)Q为一记录型二维数组,定义如下: type TempData=Record Value:Real; StartTime:Real; end; Q:array of array of TempData ---- 并且在另一过程已对Q的分量进行赋值。 ---- (2)为了区分不同的零件,Shape以不同颜色显示,此时,调用了函数RandomColor。该函数为: function TCreateMultiCharts.RandomColor; var red,green,blue:byte; begin red:=random(255); green:=random(255); blue:=random(255); result:=red or (green shl 8) or (blue shl 16); end; ---- 2、动态生成Charts控件的ChartSeries组件,显示设备利用率 procedure TFormMultiMachinesBurthen. ShowMachineBurthenCharts; var i:Integer; Burthen:Real; SeriesClass:TChartSeriesClass; NewSeries:array of TChartSeries; begin SetLength(NewSeries,CreateMultiCharts.Rows); MachinesBurthenCharts.height:=200; MachinesBurthenCharts.Width:=550; for i:=0 to CreateMultiCharts.Rows do begin SeriesClass:=TBarSeries; //设置形状为三维条形图 NewSeries[i]:=SeriesClass.Create(Self); NewSeries[i].ParentChart:=MachinesBurthenCharts; NewSeries[i].Clear; Burthen:=MachineBurthen[i]; Burthen:=Round(Burthen*100)/100; //只取小数点后两位数字 NewSeries[i].add(Burthen,'',NewSeries[i].SeriesColor); end; end; ---- 注: ---- (1) MachineBurthen[i]为一实型数组,其值为对应设备的利用率,已在另一函数中计算得到; ---- (2) MachinesBurthenCharts为TChart控件,在type段说明。
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private procedure EditChange(Sender: TObject); public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.EditChange(Sender: TObject); begin Caption:=(Sender As TEdit).Text; end;procedure TForm1.Button1Click(Sender: TObject); var Edit:TEdit; begin Edit:=TEdit.Create(Self); Edit.Left:=10; Edit.Top:=10; Edit.Width:=100; Edit.OnChange:=EditChange; Edit.Parent:=Self; end;end.
Begin
Button[i]:=TButton.Create(Self);
Button[i].Parent:=Self;
Button[i].Onclick:=BtnClkHandler;
End;procedure TMain.BtnClkHandler(Sender:TObject);
begin
with Sender as TButton do
begin
//做你想做的
end;
end;
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';}
---- 1、 Form(表单)设计中生成控件 ---- 在进行Form设计时,直接在控件工具箱选择所需控件,再设置其属性与响应事件,这种方法比较常见。 ---- 2、 程序中动态生成控件 ---- 有时候,我们需要在程序运行时动态生成控件,这样做有两大优点:一是可以增加程序的灵活性;二是如果生成控件的多少与程序中间运行结果相关,显然方法一是无法的实现的,必须用程序中动态生成方法。 ---- 程序中动态生成控件的方法分为三步,首先,定义生成的控件类型,再用Create函数生成控件,最后对控件的相关属性赋值。以TButton控件为例,步骤如下: ---- (1) 定义控件类型 var
Button1:TButton;
---- (2) 生成控件 Button1:=TButton. Create(self);
Button1.Parent:=Self;
//一般将其父控件设置为Self,如果不设置Parent的值,
则控件不会在屏幕
//显示出来
---- (3) 设置其它属性及定义相关事件响应函数,如Caption,Left,Top,Height,Width,Visible,Enabled,Hint和onClick事件响应函数等。 二、动态生成控件方法的应用
---- 在开发生产调度与管理系统中,需要动态生成排产计划图,以甘特图表示,应用Shape控件来显示零件的加工状况(每道工序的加工开始时间与结束时间)是非常适合的。应用Chart控件,对加工设备利用率以三维直方图显示,非常直观。现分别将在程序中动态生成Shape控件和Chart控件的过程加以说明。 ---- 1、动态生成Shape控件显示排产计划图(甘特图) procedure TCreateMultiCharts.ProcCreateCharts;
var
i,j,Rows,Columns,RowSpace,ChartsHeight:Integer;
ShapeChart:array of array of TShape;
begin
Rows:=16; //Shape控件数组行数
Columns:=8; // Shape控件数组列数
RowSpace:=20; // Shape控件行间距
ChartsHeight:=20; // Shape控件高度
SetLength(ShapeChart,Rows,Columns);
//设置ShapeChart数组大小
for i:=0 to Rows do
for j:=0 to Columns do
begin
ShapeChart[i][j]:=TShape.Create(self);
with ShapeChart[i,j] do
begin
Parent:=Self; //此行必不可少,
否则Shape控件在屏幕显示不出
Shape:=stRectangle; // Shape控件形状为矩形
Top:=45+i*(RowSpace+ChartsHeight);
Left:=Round(180+Q[i,j].StartTime);
//因Q[i,j].StartTime为实数,故需进行四舍五入取整
Width:=Round(Q[i,j].Value)
Height:=ChartsHeight;
Brush.Color:=RandomColor;
//自定义函数,说明附后
Brush.Style:=bsSolid; //设置填充方式
Enabled:=True;
end;
end;
end;
---- 注: ---- (1)Q为一记录型二维数组,定义如下: type
TempData=Record
Value:Real;
StartTime:Real;
end;
Q:array of array of TempData
---- 并且在另一过程已对Q的分量进行赋值。 ---- (2)为了区分不同的零件,Shape以不同颜色显示,此时,调用了函数RandomColor。该函数为: function TCreateMultiCharts.RandomColor;
var
red,green,blue:byte;
begin
red:=random(255);
green:=random(255);
blue:=random(255);
result:=red or (green shl 8) or (blue shl 16);
end;
---- 2、动态生成Charts控件的ChartSeries组件,显示设备利用率 procedure TFormMultiMachinesBurthen.
ShowMachineBurthenCharts;
var
i:Integer;
Burthen:Real;
SeriesClass:TChartSeriesClass;
NewSeries:array of TChartSeries;
begin
SetLength(NewSeries,CreateMultiCharts.Rows);
MachinesBurthenCharts.height:=200;
MachinesBurthenCharts.Width:=550;
for i:=0 to CreateMultiCharts.Rows do
begin
SeriesClass:=TBarSeries; //设置形状为三维条形图
NewSeries[i]:=SeriesClass.Create(Self);
NewSeries[i].ParentChart:=MachinesBurthenCharts;
NewSeries[i].Clear;
Burthen:=MachineBurthen[i];
Burthen:=Round(Burthen*100)/100; //只取小数点后两位数字
NewSeries[i].add(Burthen,'',NewSeries[i].SeriesColor);
end;
end;
---- 注: ---- (1) MachineBurthen[i]为一实型数组,其值为对应设备的利用率,已在另一函数中计算得到; ---- (2) MachinesBurthenCharts为TChart控件,在type段说明。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure EditChange(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.EditChange(Sender: TObject);
begin
Caption:=(Sender As TEdit).Text;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Edit:TEdit;
begin
Edit:=TEdit.Create(Self);
Edit.Left:=10;
Edit.Top:=10;
Edit.Width:=100;
Edit.OnChange:=EditChange;
Edit.Parent:=Self;
end;end.