如何把一个类的名字做一个参数传递啊?

nkym0626 2011-03-24 03:47:39
RT
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿发伯 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nkym0626 的回复:]
传类名,然后再根据类名创建实例呀。。。
2L的好像可以用
[/Quote]
先把问题搞清楚在提问。应该是“如何把一个类作为参数传递”,而不是“如何把一个类的名字作为参数传递”。可惜我的沙发!浪费我的表情!
cntigercat 2011-03-24
  • 打赏
  • 举报
回复

procedure TApplication.CreateForm(InstanceClass: TComponentClass; var Reference);
var
Instance: TComponent;
begin
// Set flag that TCustomForm constructor can read, so it knows if it's being
// created as a main form or not (required when MainFormOnTaskbar is True)
FCreatingMainForm := (FMainForm = nil) and InstanceClass.InheritsFrom(TForm);
Instance := nil;
try
{$IF DEFINED(CLR)}
Instance := InstanceClass.Create(Self);
Reference := Instance;
{$ELSE}
Instance := TComponent(InstanceClass.NewInstance);
TComponent(Reference) := Instance;
try
Instance.Create(Self);
except
TComponent(Reference) := nil;
raise;
end;
{$IFEND}

if (FMainForm = nil) and (Instance is TForm) then
begin
TForm(Instance).HandleNeeded;
FMainForm := TForm(Instance);
if MainFormOnTaskBar then
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_NOACTIVATE);
ChangeAppWindow(Handle, not MainFormOnTaskBar, not MainFormOnTaskBar);
end;
finally
if (FMainForm = nil) and (Instance is TForm) then
TForm(Instance).FCreatingMainForm := False;
end;
end;



Forms单元里的一个原型
nkym0626 2011-03-24
  • 打赏
  • 举报
回复
传类名,然后再根据类名创建实例呀。。。
2L的好像可以用
bdmh 2011-03-24
  • 打赏
  • 举报
回复
你是要传递类名呀,还是 要根据类名创建实例呀
cntigercat 2011-03-24
  • 打赏
  • 举报
回复

type
TFormClass = class of TForm;
........

procedure ShowForm(aFormClass: TFormClass);
var
instance: TForm;
begin
instance := TForm(aFormClass.NewInstance);
try
instance.Create(Application).Show;
except
end;
end;

阿发伯 2011-03-24
  • 打赏
  • 举报
回复
类.ClassName

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧