如何知道前台窗口何时更改

Posted

技术标签:

【中文标题】如何知道前台窗口何时更改【英文标题】:how to know when the foreground window has changed 【发布时间】:2012-11-20 15:04:54 【问题描述】:

我正在用 c# 编写一个应用程序,我需要知道地面窗口何时更改 我使用了 SetWindowsHookEx,但在窗口之间切换时我没有收到回叫

我的代码:

private const int WH_CALLWNDPROC = 4;

private delegate IntPtr windowName(int nCode, IntPtr wParam, IntPtr lParam);
private static windowName _name = HookCallback;
private static IntPtr _hook = IntPtr.Zero;

public static void start()
   
    _hook = SetHook(_name);
    Application.Run();
    UnhookWindowsHookEx(_hook);


private static IntPtr SetHook(windowName proc)

    using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        
            return SetWindowsHookEx(WH_CALLWNDPROC, proc, GetModuleHandle(curModule.ModuleName), 0);
        


private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
   
    browser = GetActiveWindow();
    Console.WriteLine(browser);
    return CallNextHookEx(_hook, nCode, wParam, lParam);

【问题讨论】:

您根本没有检查错误,所以您不知道为什么它不起作用。您不能像在 C# 中使用的那样设置全局挂钩。或者 WH_SHELL,你应该使用的那个。它需要将 DLL 注入每个进程,您不能注入托管代码。检查这个项目:codeproject.com/Articles/18638/… 此代码适用于键盘或鼠标事件,我在进程中使用 DLL,但我不知道如何为前台窗口实现它 【参考方案1】:

好的,我有答案

    public static void start()
    
        WinEventDelegate dele = new WinEventDelegate(WinEventProc);
        IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
        string window = GetActiveWindowTitle();
        Console.WriteLine(window);
        while (true)
        
            if (window != GetActiveWindowTitle())
            
                window = GetActiveWindowTitle();
                Console.WriteLine(window);
            
        
    

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

    private static string GetActiveWindowTitle()
    
        const int nChars = 256;
        IntPtr handle = IntPtr.Zero;
        StringBuilder Buff = new StringBuilder(nChars);
        handle = GetForegroundWindow();

        if (GetWindowText(handle, Buff, nChars) > 0)
        
            return Buff.ToString();
        
        return null;
    

    public static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    
        Console.WriteLine(GetActiveWindowTitle());
    

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

    private const uint WINEVENT_OUTOFCONTEXT = 0;
    private const uint EVENT_SYSTEM_FOREGROUND = 3;

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
    #endregion

它有点乱,但它有效

【讨论】:

以上是关于如何知道前台窗口何时更改的主要内容,如果未能解决你的问题,请参考以下文章

如何在 OS X 上将进程窗口置于前台?

在 OSX 上将新的 GTK 3 窗口置于前台

浏览器可以检测浏览器何时在前台或后台模式下运行? [复制]

从后台任务或服务中确定当前前台应用程序

如何将我的窗口投影为前台的活动窗口

如何在 WPF 中将 MessageBox 窗口保留在前台