窗体问题 ( 积分: 30 )

  • 主题发起人 主题发起人 ztao1979
  • 开始时间 开始时间
Z

ztao1979

Unregistered / Unconfirmed
GUEST, unregistred user!
请问以下代码为什么在DELPHI2005 下会出错:
procedure TForm1.OpenForm(FormClass:TFormClass;var Fm;AOwner: TComponent);
var
i: Integer;
Child: TForm;
begin
for i:=0 to screen.FormCount-1 do
begin
if Screen.Forms.ClassType=FormClass then
begin
Child:=Screen.Forms;
if Child.WindowState=wsMinimized then
begin
ShowWindow(Child.Handle,SW_SHOWNORMAL)
end else
begin
ShowWindow(Child.Handle,SW_SHOWNA);
end;
if (not child.Focused) then
child.Visible:=True;
Child.BringToFront;
Child.SetFocus;
TForm(Fm):=Child;
exit;
end;
end;
child:=TForm(FormClass.NewInstance);
TForm(Fm):=Child;
Child.Create(AOwner);
Child.Left:=0;
Child.Top:=0;
end;
 
请问以下代码为什么在DELPHI2005 下会出错:
procedure TForm1.OpenForm(FormClass:TFormClass;var Fm;AOwner: TComponent);
var
i: Integer;
Child: TForm;
begin
for i:=0 to screen.FormCount-1 do
begin
if Screen.Forms.ClassType=FormClass then
begin
Child:=Screen.Forms;
if Child.WindowState=wsMinimized then
begin
ShowWindow(Child.Handle,SW_SHOWNORMAL)
end else
begin
ShowWindow(Child.Handle,SW_SHOWNA);
end;
if (not child.Focused) then
child.Visible:=True;
Child.BringToFront;
Child.SetFocus;
TForm(Fm):=Child;
exit;
end;
end;
child:=TForm(FormClass.NewInstance);
TForm(Fm):=Child;
Child.Create(AOwner);
Child.Left:=0;
Child.Top:=0;
end;
 
child:=TForm(FormClass.NewInstance);
TForm(Fm):=Child;
Child.Create(AOwner);
Child.Left:=0;
Child.Top:=0;
不明白你这段代码的意图。
child:=TForm(FormClass.NewInstance);
这里,我相信你已经创建了一个对象了。
但是为什么你又要再调用多一次Create方法呢?
Child.Create(AOwner);
 
哪该怎么改呢?
 
哪位大侠请帮我一下,谢谢
 
child:=TForm(FormClass.NewInstance);
Child.Create(AOwner);
就是这两行代码出错
 
有没有人帮助我呀?
 
我自己已经解决了,现把代码共享给大家(是Delphi2005关于MDI子窗体显示的):
procedure OpenForm(FormClass:TFormClass;var Fm;AOwner: TComponent);
var
i: Integer;
Child: TForm;
begin
for i:=0 to screen.FormCount-1 do
begin
if Screen.Forms.ClassType=FormClass then
begin
Child:=Screen.Forms;
if Child.WindowState=wsMinimized then
begin
ShowWindow(Child.Handle,SW_SHOWNORMAL)
end else
begin
ShowWindow(Child.Handle,SW_SHOWNA);
end;
if (not child.Focused) then
child.Visible:=True;
Child.BringToFront;
Child.SetFocus;
Fm:=Child;
exit;
end;
end;
Child:=FormClass.Create(Application);
Fm:=Child;
Child.Left:=0;
Child.Top:=0;
end;
 
我們的程序也用這個。
procedure openform(formclass: Tformclass
var fm
Aowner: Tcomponent);
var
i: integer;
Child: TForm;
begin
Child := nil;
for i := 0 to Screen.FormCount - 1 do
if Screen.Forms.Classtype = formclass then
begin
Child := Screen.Forms;
if Child.WindowState = wsMinimized then
ShowWindow(Child.handle, SW_SHOWNORMAL)
else
ShowWindow(Child.handle, SW_SHOWNA);
if (not Child.Visible) then Child.Visible := true;
Child.BringToFront;
Child.SetFocus;
TForm(fm) := Child;
Child := TForm(formclass.NewInstance);
exit;
end;
if Child = nil then
begin
TForm(fm) := Child;
Child := formclass.Create(Application);
end;
end;
 
恭喜!
还有一种是利用类名来创建的,我是这样做的。
不过,对应的类要先向程序注册才能被RTTI认识、找到。
procedure TUrTreeView.OnNodeChange(Sender: TObject
Node: TTreeNode);
var
AChildForm, ATempForm: TForm;
ANodeData: TTreeNodeData;
AFormClass: TFormClass;
begin
ANodeData := Node.Data;
AFormClass := nil;
if Assigned(ANodeData) then
begin
if isExist(ANodeData.FrClassName, AChildForm) then
begin
AChildForm.WindowState := wsMaximized;
AChildForm.Show

end
else
begin
//利用RTTI函数FindClass获得对应的类指针然后创建对象
//FindClass函数要求相应的对象应该注册到本程序
//所以每个TForm类,在单元初始化的时候,都应该调用 RegisterClass
//把进行注册
if ANodeData.FrClassName = '' then
begin
Exit;
end;
//转换成TForm类的类指针
AFormClass := TFormClass(GetClass(ANodeData.FrClassName));
//OK获得以后就创建它吧
if Assigned(AFormClass) then
begin
//Self.Owner是主窗体
ATempForm := AFormClass.Create(self.Owner);
ATempForm.FormStyle := fsMDIChild;
ATempForm.WindowState := wsMaximized;
ATempForm.Caption := ANodeData.Name;
ATempForm.Show;

end;
end;
end;
end;
 
后退
顶部