bool  TForm1::CreateForm(TForm *CForm)
{
    int No;
    bool FormExist=false;
    if (CForm==NULL)
    return false;
    for (No=0;No<Screen->FormCount;No++)
    {
        if (Screen->Forms[No]->ClassType()==CForm->ClassType())
        {
            FormExist=true;
            break;
        }
    }
    if(FormExist==false)
    return false;
    if (CForm->WindowState==wsMinimized)
        ShowWindow(CForm->Handle,SW_SHOWNORMAL);
    else
        ShowWindow (CForm->Handle,SW_SHOWNA);
    if (!CForm->Visible)
        CForm->Visible=true;
    CForm->BringToFront();
    CForm->SetFocus();
    return true;}
//--------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if ((!CreateForm(Form2)))
    {
        Form2 = new TForm2(Application);
        Form2->Show();
    }}
这是c++代码。请问怎么使在delphi中不重复创建mdi子窗体

解决方案 »

  1.   

    if not form.active   then
            form.Show;
      

  2.   

    你的例子是bcb的
    delphi里应该这样判断
    if Form1<>nil then
    beginend;
      

  3.   

    if not Assigned(Form1) then
      Form1:=TForm1.Create(Application);
    Form1.show;
      

  4.   

    delphi里这样判断
    if not Assigned(Form1) then
       Form1:=TForm1.Create(Application);
    Form1.show;
      

  5.   

    给我一百分。这份代码我研究了很久。传入类的字符,来创建窗体。每次调用减少两句代码。的用 100 次 减少 200 句代码。现在在优化她,传入类的引用。//控件单元
    {*
    单元说明          创建模式窗口,和非模式窗口的类。
    作者              易一  E-Mail:  [email protected]
    创建时间          2005年5月20日
    及最后修改时间
    修改人修改时间及
    修改说明
    *}   
    unit ShowFormClass;interfaceuses
      SysUtils, Classes, Forms;type           
      TShowFormClass = class(TComponent)
      private
        { Private declarations }
        //保存要创建的窗体的类名
        FFrmName:string;    
        //判断窗体是否存在。
        function IsFormExist:boolean;
        //得到窗体。
        function GetForm:TForm;
        //创建一个类
        function CreateAClass(const AClassName: string): TForm;
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); virtual;
        destructor Destroy; override;
        
        //创建并显示窗体。模式窗体。
        procedure ShowModalForm(const AStrForm:string);
        //创建并显示窗体。非模式窗体。
        procedure ShowModalLessForm(const AStrForm:string);  published
        { Published declarations }
      end;procedure Register;implementation
    procedure Register;
    begin
      RegisterComponents('Yeeyee', [TShowFormClass]);
    end;constructor TShowFormClass.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
    end;destructor TShowFormClass.Destroy;
    begin
      inherited Destroy;
    end;function TShowFormClass.GetForm:TForm;
    var
      i:integer;
    begin
      for i := 0 to (Application.ComponentCount - 1) do
      begin
        if (Application.Components[i] is TForm) then
        begin
          //注意,关键判断这个类型名称是否存在。
          if (application.Components[i] as TForm).ClassType.ClassName = FFrmName then
          begin
            Result:=(application.Components[i] as TForm);
            exit;
          end
        end;
      end;
    end;function TShowFormClass.IsFormExist:boolean;
    var
      i:integer;
    begin
      Result:=False;
      for i := 0 to (Application.ComponentCount - 1) do
      begin
        if (Application.Components[i] is TForm) then
        begin
          //注意,关键判断这个类型名称是否存在。
          if (application.Components[i] as TForm).ClassType.ClassName = FFrmName then
          begin
            Result:=True;
            exit;
          end
        end;
      end;
    end;//创建一个类
    function TShowFormClass.CreateAClass(const AClassName: string): TForm;
    var
      LFormClass : TFormClass;
      LForm: TForm;
    begin
      LFormClass := TFormClass(FindClass(AClassName));
      LForm := LFormClass.Create(Application);
      Result := LForm;
    end;//创建并显示窗体。模式窗体。
    procedure TShowFormClass.ShowModalLessForm(const  AStrForm:string);
    var
      LForm: TForm;
    begin
      FFrmName:=AStrForm;
      //窗体不存在,则创建。
      if not IsFormExist then
      begin
        LForm := CreateAClass(FFrmName);
        LForm.Show;
      end
      else
      begin
        //存在,则得到窗体。带到最前头。
        LForm:=GetForm;
        LForm.BringToFront;
      end;
    end;//创建并显示窗体。模式窗体。
    procedure TShowFormClass.ShowModalForm(const AStrForm:string);
    var
      LForm: TForm;
    begin
      FFrmName:=AStrForm;
      LForm := CreateAClass(FFrmName);
      try
        LForm.ShowModal;
      finally
        LForm.Free;
      end;
    end;end.//调用单元
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      ShowModalLessForm('TForm2');
    end;procedure TForm1.BitBtn2Click(Sender: TObject);
    begin
      ShowModalLessForm('TForm3');
    end;
    (*
    procedure TForm1.BitBtn3Click(Sender: TObject);
    begin
      ShowForm(TForm2);
    end;procedure TForm1.BitBtn4Click(Sender: TObject);
    begin
      ShowForm(TForm3);
    end;
    *)
    initialization
    begin
      RegisterClasses([TForm2,TForm3]);
    end;finalization
    begin
      UnRegisterClasses([TForm2,TForm3]);
    end;
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var Temp1:TForm;
        I:integer;
        Exist:Boolean;
    begin
        Exist:=False;
        for I:=0 to Screen.FormCount-1 do
        begin
            if Screen.Forms[I].Caption='Temp1' then Exist:=True;
        end;
        if Exist then
        begin
            showmessage('窗口已经被创建!');
        end
        else
        begin
            Temp1:=TForm.Create(self);
            Temp1.Caption:='Temp1';
        end;
    end;
      

  7.   

    yeeyee(易 一 想找 M*M) 可以给我看下你代码吗?看不太明白呀。他们的我试了但是没有达到效果。
    如果可以做成dll免费帮我弄下呀。我对delphi还不太熟悉呀。谢谢拉。