在MDI环境中如何打开一个子窗口,打开后如何关闭它?请给出源码,正确者,一百分全给他.

解决方案 »

  1.   

    给你一个通用函数:procedure OpenForm(FormClass: TFormClass; var fm; AOwner:TComponent);
    var
      i: integer;
      Child:TForm;
    begin
      for i := 0 to Screen.FormCount -1 do
          if Screen.Forms[i].ClassType=FormClass then
          begin
            Child:=Screen.Forms[i];
            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;
            exit;
          end;
      Child:=TForm(FormClass.NewInstance);
      TForm(fm):=Child;
      Child.Create(AOwner);
    end;这样你每次调用即可:如:  OpenForm(TfrmChild1,frmChild1, self);别忘了在close的时候:  Action:=caFree;
      

  2.   

    在close 事件中
    action:=cafree;
      

  3.   

    procedure TForm2.Button1Click(Sender: TObject);
    begin
      close;
    end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      action:=cafree;
    end;
      

  4.   

    在属性中将主窗体设为MDI,子窗体设为CHILD
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    if mdichildcount>0 then
       begin
       activemdichild.Close;
       end;
       Application.CreateForm(TForm2,Form2);
       form2.Show;
    end;
    在子窗体的关闭事件里写上
    action:=cafree;
    这样功能就实现了
    在打开一个另一个子窗体的同时前一个子窗体自动就关闭了
      

  5.   

    告诉楼主hxora (hxora) :
     renzhm(戴尔飞) 的代码是经典.