要求:程序一运行时俩窗体同时显示(子窗体显示在前面),这点已做到;现在子窗体有一button1要求显示主窗体(已存在则直接显示)我现在的问题是:点button1时显示主窗体子窗体没法关闭(其实是主窗体FormCreate中又创建了一次);看看吧或许描述的不清,代码明了:
mainFrom:const WM_MYMSG=WM_APP+1;    procedure WMMYMSG( var msg:Tmessage); message WM_MYMSG;
  end;var
  MainForm: TMainForm;implementation
uses unit2,Unit3;{$R *.dfm}
procedure TMainForm.WMMYMSG( var msg:Tmessage);
var
  f:TForm1;
begin
  f:=TForm1.create(nil);
  f.ShowModal;
end;procedure TMainForm.FormCreate(Sender: TObject);
var
  f:TForm1;
begin
 PostMessage(Handle,WM_MYMSG,0,0);
end;
子窗体:procedure TForm1.Button1Click(Sender: TObject);
var
  f:TMainForm;
begin
  try  
  if not Assigned(f)  then
    begin
      f:=TMainForm.Create(self); 
      f.Show;
    end;
    finally
     if Form1<>nil then
     freeandnil(Form1);
  end;
end;end.

解决方案 »

  1.   

    为什么主窗体要重新创建,按理说主窗体关闭了,整个程序都会关闭的
    何况你的Button1Click代码造成循环了,创建MainForm时会重新PostMessage,又创建Form1只改变他们的hide,show就行了procedure TForm1.Button1Click(Sender: TObject);
    begin
      MainForm.show;
      freeandnil(Form1); //或者可以考虑用Form1.hide
    end;
      

  2.   

    我要实现的功能是:程序一运行时同时显示俩个窗体(form1在前,主窗体在后面,showmodal模式),然后点form1的一个按钮 显示主窗体 功能就是这样 求大侠们帮帮忙啊
      

  3.   

    Use ShowModal to show a form as a modal form.  A modal form is one where the application can抰 continue to run until the form is closed. Thus, ShowModal does not return until the form closes. When the form closes, it returns the value of the ModalResult property.
      

  4.   

    以上是delphi帮助的原话 
    如果 from1是showmodal窗体的子窗体 
    那么 from显示时 只能from1.show;不能用showmodal 要不然 主窗体没法在前
      

  5.   

    那怎么关闭不了 真接 from1.Close;不就行了? 你搞那么复杂干嘛?
      

  6.   

     procedure TMainForm.WMMYMSG( var msg:Tmessage);
     var   f:TForm1;
     begin  
     f:=TForm1.create(nil); 
      f.ShowModal;
     end;procedure TForm1.Button1Click(Sender: TObject);
     var   f:TMainForm;//这是局部变量 
    begin   
       try    
      if not Assigned(f) then //所以你这句 没有意义!
      begin  
        f:=TMainForm.Create(self);   
        f.Show;  
     end;  
    finally   
      if Form1<>nil then //这是什么东西!?
        freeandnil(Form1);  
       end; 
    end;  
    end.
    你写得太深奥! 水平有限,看不懂 
      

  7.   

    点击子窗体的按钮后执行  self.close;
      frmmain.show;//显示主窗体