---开发环境Delphi7
----Class of 第一篇 https://www.cnblogs.com/dmqhjp/p/15155742.html
--Unit开始
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 procedure Button1Click(Sender: TObject);
13 private
14 { Private declarations }
15 public
16 { Public declarations }
17 end;
18
19 var
20 Form1: TForm1;
21
22 implementation
23
24 var
25 SerialNumber:Integer=1;
26 {$R *.dfm}
27
28 procedure TForm1.Button1Click(Sender: TObject);
29 var
30 vButton:TButton;
31 begin
32 //这个是不是很神奇?
33 {
34 CreateForm这个不是创建窗体的吗?为啥可以创建按钮!?
35 //TComponentClass= class of TComponent;(在Classes中就有这样的声明)
36 //结合第一篇中说到的,子类型不管第几代,父类都能创建,通过类引用(类的类)达到目的
37 //TButton也是TComponent的N代子类啊!所以这个也可以建立TButton的实例
38
39 procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference);
40 var
41 Instance: TComponent;
42 begin
43 Instance := TComponent(InstanceClass.NewInstance);
44 TComponent(Reference) := Instance;
45 try
46 Instance.Create(Self);
47 except
48 TComponent(Reference) := nil;
49 raise;
50 end;
51 if (FMainForm = nil) and (Instance is TForm) then
52 begin
53 TForm(Instance).HandleNeeded;
54 FMainForm := TForm(Instance);
55 end;
56 end;
57
58 }
59 Application.CreateForm(TButton,vButton);
60 vButton.Parent:=Self;
61 vButton.Height:=20;
62 vButton.Width:=80;
63 vButton.Left:=100;
64 vButton.Top:=10+30*(SerialNumber-1);
65 vButton.Name:='Button_'+inttostr(SerialNumber);
66 Inc(SerialNumber);
67 end;
68
69 end.
------unit结束
----Form开始
1 object Form1: TForm1
2 Left = 757
3 Top = 557
4 Width = 381
5 Height = 416
6 Caption = 'Form1'
7 Color = clBtnFace
8 Font.Charset = DEFAULT_CHARSET
9 Font.Color = clWindowText
10 Font.Height = -11
11 Font.Name = 'MS Sans Serif'
12 Font.Style = []
13 OldCreateOrder = False
14 PixelsPerInch = 96
15 TextHeight = 13
16 object Button1: TButton
17 Left = 8
18 Top = 16
19 Width = 75
20 Height = 25
21 Caption = 'Button1'
22 TabOrder = 0
23 OnClick = Button1Click
24 end
25 end
----Form结束