通过SendMessage或其他东西异步GetForegroundWindow?

时间:2012-01-12 19:15:22

标签: c# winapi

有没有办法通知焦点何时从任何窗口更改到另一个窗口(甚至在Windows应用程序之间),以便我可以在用户更改焦点时立即调用我的委托?

我一直在想,我可能只需要进行民意调查:( :(每1秒钟调用一次GetForegroundWindow,但我真的不想这样做。

3 个答案:

答案 0 :(得分:23)

SetWinEventHook()可能是你最好的选择;您可以收听EVENT_SYSTEM_FOREGROUND以收听前景窗口更改 - 甚至可以收听EVENT_OBJECT_FOCUS以在应用内和控件内收听更多细粒度焦点更改。

您需要将此功能与WINEVENT_OUTOFCONTEXT标志一起使用;这意味着更改通知将异步传递到您自己的应用程序,因此您不需要单独的DLL - 您仍然需要P / Invoke。但是通知不会即时 - 可能会有一点延迟 - 但异步暗示了这一点。如果你想毫不拖延地立即做一些事情,你将需要使用C ++和进程内挂钩(使用WINEVENT_INCONTEXT的SetWinEventHook或者SetSetWindowsHookEx样式的钩子。)

这是一个似乎正在寻找您正在寻找的样本:

using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class ForegroundTracker
{
            // Delegate and imports from pinvoke.net:

    delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
        IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

    [DllImport("user32.dll")]
    static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
       hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
       uint idThread, uint dwFlags);

    [DllImport("user32.dll")]
    static extern bool UnhookWinEvent(IntPtr hWinEventHook);

            // Constants from winuser.h
    const uint EVENT_SYSTEM_FOREGROUND = 3;
    const uint WINEVENT_OUTOFCONTEXT = 0;

    // Need to ensure delegate is not collected while we're using it,
    // storing it in a class field is simplest way to do this.
    static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);

    public static void Main()
    {
        // Listen for foreground changes across all processes/threads on current desktop...
        IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
                procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);

        // MessageBox provides the necessary mesage loop that SetWinEventHook requires.
        MessageBox.Show("Tracking focus, close message box to exit.");

        UnhookWinEvent(hhook);
    }

    static void WinEventProc(IntPtr hWinEventHook, uint eventType,
        IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        Console.WriteLine("Foreground changed to {0:x8}", hwnd.ToInt32()); 
    }
}

答案 1 :(得分:14)

上面的例子就像一个冠军。我重构了一点,以创建一个可能有用的类。我没有定义所有常量,所以如果你想捕获其他事件,你需要添加几个。

using System;
using System.Runtime.InteropServices;

#pragma warning disable 1591
// ReSharper disable InconsistentNaming

namespace MosaiqPerformanceMonitor {
     public class EventHook {
          public delegate void WinEventDelegate(
                IntPtr hWinEventHook, uint eventType,
                IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

          [DllImport("user32.dll")]
          public static extern IntPtr SetWinEventHook(
                uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc,
                uint idProcess, uint idThread, uint dwFlags);

          [DllImport("user32.dll")]
          public static extern bool UnhookWinEvent(IntPtr hWinEventHook);

          public const uint EVENT_SYSTEM_FOREGROUND = 3;
          public const uint WINEVENT_OUTOFCONTEXT = 0;
          public const uint EVENT_OBJECT_CREATE = 0x8000;

          readonly WinEventDelegate _procDelegate;
          readonly IntPtr _hWinEventHook;

          public EventHook(WinEventDelegate handler, uint eventMin, uint eventMax) {
                _procDelegate = handler;
                _hWinEventHook = SetWinEventHook(eventMin, eventMax, IntPtr.Zero, handler, 0, 0, WINEVENT_OUTOFCONTEXT);
          }

          public EventHook(WinEventDelegate handler, uint eventMin)
                : this(handler, eventMin, eventMin) {
          }

          public void Stop() {
                UnhookWinEvent(_hWinEventHook);
          }

          // Usage Example for EVENT_OBJECT_CREATE (http://msdn.microsoft.com/en-us/library/windows/desktop/dd318066%28v=vs.85%29.aspx)
          // var _objectCreateHook = new EventHook(OnObjectCreate, EventHook.EVENT_OBJECT_CREATE);
          // ...
          // static void OnObjectCreate(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) {
          //    if (!Win32.GetClassName(hWnd).StartsWith("ClassICareAbout"))
          //        return;
          // Note - in Console program, doesn't fire if you have a Console.ReadLine active, so use a Form
     }
}

答案 2 :(得分:3)

您可以安装Windows挂钩(需要一些P / Invoke)并监视发送到Windows的消息。 This question列出了将窗口置于前台所涉及的消息。 Here is the MSDN documentation用于安装挂钩