主窗体form1手动产生了子窗体form2,然后form1.hide,form2.show
form2在关闭的时候需要关闭form1,这个功能怎么做?

解决方案 »

  1.   

    Form2的OnClose事件中添加如下代码:
    Application.Terminate;
      

  2.   

    在FORM2的OnCloseQuery事件中写上:Form1.Close;
      

  3.   


    procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Form1.Close;
    end;
      

  4.   

    那在form1关闭的时候,form2会完全释放么?
    不需要自己写form2.free么?
      

  5.   

    Form2 := TForm2.Create(Application);就可以不用手工释放了
      

  6.   

    可以用消息来实现
    在主窗体先自定义一个消息 主窗体的代码:
    unit Unit4;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;const
      WM_MY_CLOSE = WM_USER + 1234;type
      TFormMain = class(TForm)
      private
        { Private declarations }
        procedure MY_CLOSE(var msg: TMSG); message WM_MY_CLOSE;
      public
        { Public declarations }
      end;var
      FormMain: TFormMain;implementation{$R *.dfm}{ TFormMain }procedure TFormMain.MY_CLOSE(var msg: TMSG);
    begin
      Application.Terminate;
    end;end.
    然后发送消息   PostMessage(FormMain.Handle, WM_USER + 1234, 0, 0)