在程序中打开子窗口,但子窗口的标题是相同的,只是显示内容不同,该如何避免重复打开呢?

解决方案 »

  1.   

    if assign(frm) then
       begin
         frm:=TForm.create(application);
       end;
       frm.show;
      

  2.   

    希望对你有点用。
    procedure TFrmMain.menuItem(Sender:TObject);
    Var FrmName ,FrmCaption:String ;
        ExecForm :TForm ;
        ActiveMenu,mnuItem:TMenuItem ;
        i :integer;
    begin
      try
        FrmName :=sc.getblockstr(TMenuItem(Sender).name,2,'_');
        FrmCaption := TMenuItem(Sender).Caption;  
        ExecForm :=TForm(Application.FindComponent(FrmName)) ;
        if ExecForm=nil then
        begin
          Application.CreateForm(TComponentClass(FindClass('T'+FrmName)),ExecForm) ;
          ExecForm.Parent :=PnlMain;
          ActiveMenu :=TMenuItem(FindComponent('mnuActive')) ;
          if ActiveMenu=nil then
          begin
            ActiveMenu :=TMenuItem.Create(Self) ;
            ActiveMenu.Name :='mnuActive' ;
            ActiveMenu.Caption :='活动窗口' ;
            MainMenu.Items.Add(ActiveMenu) ;        mnuItem :=TMenuItem.Create(Self) ;
            mnuItem.Name :='mnuClossAll' ;
            mnuItem.Caption :='关闭所有' ;
            mnuItem.onClick :=CloseAllWin;
            ActiveMenu.Add(mnuItem);        mnuItem :=TMenuItem.Create(Self) ;
            mnuItem.Name :='mnuspliter' ;
            mnuItem.Caption :='-' ;
            ActiveMenu.Add(mnuItem) ;
          end;  //ActiveMenu=nil
          mnuItem :=TMenuItem.Create(Self) ;
          mnuItem.Name :='mnuWin_'+FrmName;
          mnuItem.Caption :=FrmCaption ;
          mnuitem.OnClick :=menuItem;
          ActiveMenu.Add(mnuItem);
        end;// ExecForm=nil
        for i:=0 to Screen.formCount-1 do
           if (Screen.Forms[i].Parent=pnlMain) and (Screen.Forms[i]<>ExecForm) then
             Screen.Forms[i].Hide;
        ExecForm.Show ;
        btnClose.Visible :=True ;
      except
      on E:Exception do
        Messagedlg('加载 '+FrmName+' 出错! '+e.Message,mtinformation,[mbOk],0);
      end;//try
    end;
      

  3.   

    这是我写的一个函数,让MDI子窗体只创建一次:
    function TfrmMain.CreateChildForm(FormClass: TFormClass; Name: String): TForm;
    var ChildForm: TForm;
    begin
      ChildForm := TForm(Application.FindComponent(Name));
      if not (Assigned(ChildForm)) then
        ChildForm := FormClass.Create(Application)
      else
      begin
        ChildForm.WindowState := wsNormal;
        ChildForm.BringToFront;
      end;
      Result := ChildForm;
    end;
    然后这么调用:
    Frm01 := TFrm01(CreateChildForm(TFrm01, 'Frm01));