Delphi类引用示例

…衆ロ難τιáo~ 提交于 2020-04-26 14:10:38

 

相关资料:

https://jingyan.baidu.com/article/bad08e1e2ec23b09c9512148.html

 

实例代码:

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
 8 
 9 type
10   TControlClass = class of TControl;
11 
12   TForm1 = class(TForm)
13     RadioGroup1: TRadioGroup;
14     procedure FormCreate(Sender: TObject);
15     procedure RadioGroup1Click(Sender: TObject);
16     procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
17       Shift: TShiftState; X, Y: Integer);
18   private
19     { Private declarations }
20   public
21     { Public declarations }
22   end;
23 
24 var
25   Form1: TForm1;
26 
27 implementation
28 
29 {$R *.dfm}
30 var
31   CtlClassRef: TControlClass;
32 
33 procedure TForm1.FormCreate(Sender: TObject);
34 begin
35   RadioGroup1.Width := 400;
36   RadioGroup1.Columns := 3;
37   RadioGroup1.Items.Add('Button');
38   RadioGroup1.Items.Add('Edit');
39   RadioGroup1.Items.Add('Checkbox');
40   RadioGroup1.ItemIndex := 0;
41   CtlClassRef := TButton;
42 end;
43 
44 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
45   Shift: TShiftState; X, Y: Integer);
46 var
47   NewCtrl: TControl;
48 begin
49   NewCtrl := CtlClassRef.Create(Self);
50   NewCtrl.Name := CtlClassRef.ClassName + IntToStr(Self.ControlCount);
51   NewCtrl.Parent := Self;
52   NewCtrl.Left := X;
53   NewCtrl.Top := Y;
54 end;
55 
56 procedure TForm1.RadioGroup1Click(Sender: TObject);
57 begin
58   case RadioGroup1.ItemIndex of
59     0: CtlClassRef := TButton;
60     1: CtlClassRef := TEdit;
61     2: CtlClassRef := TCheckBox;
62   end;
63 end;
64 
65 end.
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!