当我的应用程序关闭一个子窗口时WINDOWS会发送什么消息?我试了一下WM_CLOSE好像只能抓到本窗口的关闭动作。

解决方案 »

  1.   

    一样呀,也是发送的WM_CLOSE
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
      TForm2 = class(TForm)
      private
        { Private declarations }
        procedure wmclose(var msg:TWMCLOSE);message WM_CLOSE;
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.DFM}procedure TForm2.wmclose(var msg: TWMCLose);
    begin
      showmessage('close');
    end;end.
      

  2.   

    就是发送的WM_Close
    unit Unit2;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
    type
      TForm2 = class(TForm)
      private
        { Private declarations }
        procedure wmclose(var msg:TWMCLose);message WM_CLOSE;
      public
        { Public declarations }
      end;
    var
      Form2: TForm2;implementation{$R *.DFM}
    procedure TForm2.wmclose(var msg: TWMCLose);
    begin
      showmessage('close');
    end;end.
      

  3.   

    楼上的,不是吧,如果是MDI的子窗体,你的信息是不能显示出来的如果写在OnCloseQuery中就可以。
      

  4.   

    是呀,如果是在同一个窗口中可以,但是如果你建两个窗口FormA和FormB,如果此代码是写在FormA中,运行时你把两个窗口都打开,关闭窗口FormB时这样写是不能抓到关闭窗口事件的呀,现在我就想知道当关闭FormB时发给FormA的那个消息是什么?
      

  5.   

    关掉b关a什么事?除非a用子类化技术/钩子去截获b的关闭消息,否则windows不会主动把b关闭的消息发给a
      

  6.   

    你自定义一个消息
    UM_CLOSEWINDOWS = WM_USER + 1;   //当子窗体关闭时发送这个消息给主窗口在子窗体的OnClose事件里面写代码
    SendMessage(主窗口Handle, UM_CLOSEWINDOWS, 0, 0);
    然后再在主窗体中捕捉UM_CLOSEWINDOWS消息不就OK了!
      

  7.   

    我的意思是A是应用程序的主窗口,B是其子窗口,这样也没有消息传给A吗?
    ysai(蓝色忧郁) 你说的钩子技术给否给个简单的例子?谢谢!
      

  8.   

    用個 ApplicationEvents1 的 onMessage 試下
      

  9.   

    这个我刚刚解决了,
    你override  WndProc(var Message: TMessage);
    ,如果搞不定,再问我
      

  10.   

    TO: sinocat(sinoherolast) 
    帮我写一下代码吧,还是不知道:)
      

  11.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure WndProc(var Message: TMessage); override;
      private
        { Private declarations }
      public
        { Public declarations }
        procedure myOnclose(var msg: TWMCLose); message WM_CLOSE;
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}
    procedure TForm1.WndProc(var Message: TMessage);
    begin
      Inherited;
      if message.Msg = WM_close then
      showmessage('a');
    end;procedure TForm1.myOnclose(var msg: TWMCLose);
    begin
    //  if msg.Msg = 0 then exit;
     // showmessage('a');
     // WndProc(msg);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if Form2 = nil then Form2 := TForm2.Create(self);
      Form2.show;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
    //  Application.OnMessage;
    end;end.
    ----------------------------------------------------------------
    TO: sinocat(sinoherolast) 
    我这样子override还是不对呀,
      

  12.   

    谢谢楼主的邀请。
    我知道,这样的效果,在C#中就很容易实现
    在Delphi中应该只有在当前的窗体的OnClose()
    事件中完成吧!
    先收藏,稍候在详细看你的代码!
      

  13.   

    只是子窗体向主窗体通知一下关闭而已,用得着这么麻烦么?连WndProc都重写了,太复杂化了吧推荐用 Linux2001(恋人不如自恋) 的方法
      

  14.   

    就是发送的WM_Close
    unit Unit2;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
    type
      TForm2 = class(TForm)
      private
        { Private declarations }
        procedure wmclose(var msg:TWMCLose);message WM_CLOSE;
      public
        { Public declarations }
      end;
    var
      Form2: TForm2;implementation
     uses Main;
           //  在unit1上 定廇  eventproc:TNotifyEvent 和 str:string兩個全局變量;{$R *.DFM}
    procedure TForm2.wmclose(var msg: TWMCLose);
    begin
       Main.str :='close' ;
       
    end;end.
    --------------------------------------------------------
    unit Main;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Menus, Db, ADODB,  ToolWin, ComCtrls;type
      TFrmMain = class(TForm)
        MenuMain: TMainMenu;
        dbcon: TADOConnection;
        kk: TMenuItem;
        kl: TMenuItem;
        N331: TMenuItem;
        procedure FormCreate(Sender: TObject);
        function CreateAdoQry(Sname:string;sform:Tform): TadoQuery ;
        procedure eventproc(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      FrmMain: TFrmMain;eventproc:TNotifyEvent,str:string ;
    implementation
    uses unit2;procedure TFrmMain.eventproc(Sender: TObject);
    begin
         showMessage(str);
       end;