关于在MDI窗体中防止子窗体重复打开的问题,改制的函数一直不能用。(100分)

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

zhbforce

Unregistered / Unconfirmed
GUEST, unregistred user!
关于在MDI窗体中防止子窗体重复打开的问题,改制的函数一直不能用。
请大家看以下代码:
function TfrmMain.OpenForm(var WForm:TForm):Boolean;
var
i:Integer;
FormExist:Boolean;
begin
//Determine if the form has been built.
if (WForm=nil) then
begin
Result:=false;
Exit;
end;

FormExist:=false;
for i:=0 to Screen.FormCount-1 do
begin
if Screen.Forms.ClassType=WForm.ClassType then
begin
FormExist:=true;
break;
end;
end;

if FormExist=false then
begin
Result:=false;
Exit;
end;

//If the form has been built,determine if the form is minimized or invisible
if Wform.WindowState=wsMinimized then
ShowWindow(WForm.Handle,SW_SHOWNORMAL)
else
ShowWindow(WForm.Handle,SW_SHOWNA);

if not WForm.Visible then
WForm.Visible:=True;

WForm.BringToFront;
WForm.SetFocus;
Result:=True;
end;

调用方法:
if OpenForm(AboutBox)=false then
begin
AboutBox:=TAboutBox.Create(Owner);
AboutBox.Show;
end;

为什么一到改变WFORM属性的时候,就报出访问冲突的错误,这个写法在C++Builder中却是可以的。
 
procedure OpenForm(FormClass: TFormClass; var AForm;
AOwner:TComponent=nil);
//根据窗口类名建立窗口,如果窗口存在则只激活它
var
i: integer;
Child:TForm;
begin
for i := 0 to Screen.FormCount -1 do
if Screen.Forms.ClassType=FormClass then
begin
Child:=Screen.Forms;
if Child.WindowState=wsMinimized then
Child.WindowState:=wsNormal;
Child.BringToFront;
Child.Setfocus;
TForm(AForm):=Child;
exit;
end;
Child:=TForm(FormClass.NewInstance);
TForm(AForm):=Child;
if not assigned(aowner) then aowner:=application;
Child.Create(AOwner);
end;

function ActiveForm(FormTitle:string):boolean;
//根据窗口标题激活窗口,如果窗口不存在则返回假
var
i:integer;
Child:TForm;
begin
Result:=False;
for i:=0 to screen.FormCount-1 do
begin
Child:=Screen.Forms;
if Child.caption=formtitle then
begin
Child:=Screen.Forms;
if Child.WindowState=wsminimized then
Child.WindowState:=wsnormal;
Child.BringToFront;
Child.SetFocus;
Result:=true;
end;
end;
end;
 
谢谢,可是我想知道为什么我的函数不好用呢?
 
在打开子窗体的时候,
if not assigned(FormChild) then
FormChild:=TFormChild.Create(Application);
FormChile.Show;

然后在子窗体的OnClose事件里面
Action:=caFree;

在子窗体的OnDestroy事件里面
FormChild:=nil;

就可以了,试一下吧
 
顶部