我有一个主窗口mainfrm和两个子窗口childfrm1,childfrm2,在主窗口上点button1显示子窗口1,点button2显示子窗口2,发现每点一次都显示一个新的子窗口,我需要的是点一下(如button1),先判断childfrm1是否打开状态,如是则让其显示,不必再打开一个,如何做?
我这样写错在哪了:
procedure Mainfrm.N1Click(Sender: TObject);var childfrm1:Tchildfrm1;
 begin
          if childfrm1.WindowState =Active then //这一行active是我加的,因该如何写
               if childfrm1.WindowState =wsMinimized then
                 ShowWindow(childfrm1.Handle  ,SW_SHOWNORMAL)
                    if (not childfrm1.Visible ) then //打开,但不可见
                        childfrm1.Visible :=True;
                        childfrm1.BringToFront;
                        childfrm1.SetFocus ;
                        Exit;
                     end;
          else
             childfrm1:=TFm_MDIChild.Create(Self);
             childfrm1.Show ; 以上代码有问题吗?

解决方案 »

  1.   

    childfrm1:Tchildfrm1; 声明为全局变量 if not Assigned(childfrm1) then
      childfrm1:=TFm_MDIChild.Create(Self); 
    childfrm1.Show;   
      

  2.   


    var
      form:Tform2;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if form=nil then  //也可写成if not Assigned(form) then 
      begin 
        form:=Tform2.Create(application);
      end;
      else
      begin
        if form.WindowState=wsMinimized then
        begin
          ShowWindow(form.Handle,SW_SHOWNORMAL);
          if (not form.Visible ) then //打开,但不可见
          begin
             form.Visible :=True;
             form.BringToFront;
             form.SetFocus ;
             Exit;
          end;
        end;
      end;
      form.Show;
    end;
      

  3.   


    var
      form:Tform2;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if form=nil then  //也可写成if not Assigned(form) then 
      begin 
        form:=Tform2.Create(application);
      end
      else
      begin
        if form.WindowState=wsMinimized then
        begin
          ShowWindow(form.Handle,SW_SHOWNORMAL);
          if (not form.Visible ) then //打开,但不可见
          begin
             form.Visible :=True;
             form.BringToFront;
             form.SetFocus ;
             Exit;
          end;
        end;
      end;
      form.Show;
    end;
      

  4.   

    if Application.FindComponent('childfrm1') = nil then
        childfrm1 := Tchildfrm1.Create(application);
     frmUserlogin.show;
      

  5.   

    如果不想声明为全局变量,可以通过窗口的name属性加以判断
    procedure TForm1.btn1Click(Sender: TObject);
    var
      childfrm1:TForm2;  //Tchildfrm1
      i:Integer;
    begin
      childfrm1 := nil;
      for i:=0 to Form1.MDIChildCount-1 do
        if Form1.MDIChildren[i].Name = 'childfrm1' then
        begin
           childfrm1 := TForm2(Form1.MDIChildren[i]);  //指向已经存在的窗体
           Break;
        end;  if  not Assigned(childfrm1) then //不存在,则创建
      begin
        childfrm1:=TForm2.Create(Self);
        childfrm1.Name := 'childfrm1';
      end;
      childfrm1.WindowState := wsNormal;
      childfrm1.Visible := True;
      childfrm1.Show;
    end;
      

  6.   

    childfrm1:Tchildfrm1; 声明为全局变量 if not Assigned(childfrm1) then 
      childfrm1:=TFm_MDIChild.Create(Self); 
    childfrm1.Show; 
     楼上的,不过我一般
    childfrm1:Tchildfrm1; 声明为全局变量 if (childfrm1=nil)then
       childfrm1:=TchildFrm.create(nil);
    childfrm1.show();d对了我看好多childfrm1:=TchildFrm.create(nil);
    create里面那个参数有application,self
    我想问问有什么区别没有???