动态产生器
代码
unit pgDynamicGenerate;
//动态产生器
interface
uses
pgGUISprite, pgGUIControls, DXSprite, Classes;
type
TSpriteClass = class of TpgGUISprite;
//定义一个类型数组
TClassArray = array[1..6] of TSpriteClass;
const
ClassArray: TClassArray = (TpgButton, TpgControl, TpgCustomEdit, TpgFontComponent, TpgForm, TpgMouse);
function pgSpriteCreate(SpriteClass: TSpriteClass; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
function GenerateComponent(const ClassName: string; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
procedure initRegisterClasses;
procedure finaUnRegisterClass;
implementation
function pgSpriteCreate(SpriteClass: TSpriteClass; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
begin
Result := SpriteClass.Create(AParent);
with TpgGUISprite(Result) do
begin
if ControlName <> '' then
ControlName := SpriteName;
end;
end;
function GenerateComponent(const ClassName: string; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
var
mClass: TSpriteClass;
mObj: TpgGUISprite;
begin
Result := nil;
mClass := TSpriteClass(GetClass(ClassName));
if mClass = nil then exit
else
begin
mObj := mClass.Create(AParent);
TpgGUISprite(mObj).ControlName := SpriteName;
Result := mObj;
end;
end;
procedure initRegisterClasses;
var
i: Integer;
className: TPersistentClass;
begin
for i:=Low(ClassArray) to High(ClassArray) do
begin
className:= ClassArray[i];
RegisterClasses(className);
end;
end;
procedure finaUnRegisterClass;
var
i: Integer;
className: TPersistentClass;
begin
for i:=Low(ClassArray) to High(ClassArray) do
begin
className:= ClassArray[i];
UnRegisterClass(className);
end;
end;
initialization
initRegisterClasses;
finalization
finaUnRegisterClass;
end.
//动态产生器
interface
uses
pgGUISprite, pgGUIControls, DXSprite, Classes;
type
TSpriteClass = class of TpgGUISprite;
//定义一个类型数组
TClassArray = array[1..6] of TSpriteClass;
const
ClassArray: TClassArray = (TpgButton, TpgControl, TpgCustomEdit, TpgFontComponent, TpgForm, TpgMouse);
function pgSpriteCreate(SpriteClass: TSpriteClass; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
function GenerateComponent(const ClassName: string; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
procedure initRegisterClasses;
procedure finaUnRegisterClass;
implementation
function pgSpriteCreate(SpriteClass: TSpriteClass; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
begin
Result := SpriteClass.Create(AParent);
with TpgGUISprite(Result) do
begin
if ControlName <> '' then
ControlName := SpriteName;
end;
end;
function GenerateComponent(const ClassName: string; AParent: TSprite; const SpriteName: string = ''): TpgGUISprite;
var
mClass: TSpriteClass;
mObj: TpgGUISprite;
begin
Result := nil;
mClass := TSpriteClass(GetClass(ClassName));
if mClass = nil then exit
else
begin
mObj := mClass.Create(AParent);
TpgGUISprite(mObj).ControlName := SpriteName;
Result := mObj;
end;
end;
procedure initRegisterClasses;
var
i: Integer;
className: TPersistentClass;
begin
for i:=Low(ClassArray) to High(ClassArray) do
begin
className:= ClassArray[i];
RegisterClasses(className);
end;
end;
procedure finaUnRegisterClass;
var
i: Integer;
className: TPersistentClass;
begin
for i:=Low(ClassArray) to High(ClassArray) do
begin
className:= ClassArray[i];
UnRegisterClass(className);
end;
end;
initialization
initRegisterClasses;
finalization
finaUnRegisterClass;
end.
调用方法:
mDXedit := TpgCustomEdit(GenerateComponent('TpgCustomEdit', dxForm));