请问一下,当前windows活动窗口是我的程序,当活动窗口移出,请问,我如何获得这个事件.

解决方案 »

  1.   

    当前,如果我击活我的程序,当前windows的活动窗口就是我的程序,那么有一个事件接收onActivate(); 现在我点击别的软件.那么我的程序就失去了活动窗口.用那种方法可以接收到这个事件.谢谢.请帮助.
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
      private
        procedure m_exit(var Message: TMessage); message CM_MOUSELEAVE;
      public
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}procedure TForm1.m_exit(var Message: TMessage);
    begin
      Edit1.Text := FormatDateTime('HH:NN:SS.zzz', now());
    end;end.
      

  3.   

    单用mouseleave消息不行的。
    第一,鼠标移出窗体外,就算窗体没有失去焦点也会触发。
    第二,鼠标移动到窗体内部的控件上的时候,也会触发。
    查了一下系统消息,好像没有离开程序失去焦点的时候发出的消息。
    所以,可以做如下考虑:
    1,加一个标签判断程序是否被最小化。
    2,加一个标签判断鼠标是否在程序窗体外(所有窗体)
    3,接收鼠标单击的消息(左右都接收)
    然后根据上面两个标签自己判断程序是不是失去了焦点。对系统的消息机制实在不是很在行。希望高手指点。
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
      private
        { Private declarations }
      public
          procedure WMActivate(var Message: TWMActivate); message WM_ACTIVATE;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.WMActivate(var Message: TWMActivate);
    begin
    if Message.Active = WA_INACTIVE then Caption:='Inactive'
    else Caption:='active'
    end;end.
      

  5.   

    楼上的只能获得form1窗口的得到、失去焦点消息。如果form1上又弹出个form2.ShowModal.这时form1就一直处于'Inactive'状态。
    所以应该截获application的消息,而不是mainform的消息。
    方法一:
    最简便的办法是放一个ApplicationEvents控件(在Additional页),在它的onActivate和onDeactivate事件中做控制。方法二:
    设置application.onmessage事件,如:procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnMessage := MyAppMsg;
    end;procedure TForm1.MyAppMsg(var Msg: TMsg; var Handled: Boolean);
    begin
      if Msg.hwnd = application.Handle then
        case Msg.message of
          CM_ACTIVATE:
            Self.Caption := 'activate';
          CM_DEACTIVATE:
            Self.Caption := 'inactivate';
        end;
    end;