Classes.RegisterClasses,Classes.FindClass,Classes.GetClass学习
procedure RegisterClasses(AClasses: array of TPersistentClass);
AClasses是一个数组:每个类都通过调用RegisterClass注册到组件流中,删除注册的类调用UnRegisterClass();
function FindClass(const ClassName: string): TPersistentClass;
ClassName:要查找的类名,返回TPersistentClass类型;如果查找的类不存在则触发一个exception信息,返回的类必须是从TPersistent派生的
function GetClass(const AClassName: string): TPersistentClass;
AClassName:类名,如果不存在返回nil,存在则返回对应的持续久化
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm2 = class(TForm) Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject); var myRef: TControlClass; testControl: TControl; begin //返回类型是一个类引用 myRef := TControlClass(Classes.GetClass(edit1.Text)); testControl := TControl(myRef.Create(self)); testControl.Parent := self; testControl.Top := 20; testControl.Left := 20; testControl.Show; end; //程序运行时就执行 initialization //注册类 Classes.RegisterClasses([TIcon, TMemo, TButton,TMemo,TLabel]); //程序关闭时执行 finalization //删除注册的类 Classes.UnRegisterClasses([TIcon, TMemo, TButton,TMemo,TLabel]); end.
应用:
比如树型导航栏不用每一个节点都要写事件.增加节点等.以后会写一个例子