活动窗口更改时的Windows回调 [英] Windows Callback When The Active Window Changed

查看:173
本文介绍了活动窗口更改时的Windows回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

Hi all,

public class ActiveWindow
{
    public delegate void ActiveWindowChangedHandler(object sender, String windowHeader, IntPtr hwnd);
    public event ActiveWindowChangedHandler ActiveWindowChanged;
 
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

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

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

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

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

    IntPtr m_hhook;
    private WinEventDelegate _winEventProc;

    public ActiveWindow()
    {
        _winEventProc = new WinEventDelegate(WinEventProc);
        m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
            EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _winEventProc,
            0, 0, WINEVENT_OUTOFCONTEXT);
    }

    void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
        int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        if (eventType == EVENT_SYSTEM_FOREGROUND)
        {
            if (ActiveWindowChanged != null)
                ActiveWindowChanged(this,GetActiveWindowTitle(hwnd),hwnd);
        }
    }

    private string GetActiveWindowTitle(IntPtr hwnd)
    {
        StringBuilder Buff = new StringBuilder(500);
        GetWindowText(hwnd, Buff, Buff.Capacity);
        return Buff.ToString();
    }

    ~ActiveWindow()
    {
        UnhookWinEvent(m_hhook);
    }
}



当我在活动窗口之间切换时,我得到了回调
但是当我最大化最小化窗口时,我没有回电,

我找到解决该问题的方法,但我正在寻求更好的解决方案



when i switch between the active windows i get the callback
but when i maximize a minimized window i don''t get a call back,

i find a work throw to solve this problem, but i am seeking for better solution

any help will be appreciated

推荐答案

为什么不使用CBTProc回调函数呢?您可以将其用于您感兴趣的所有事件(甚至更多).
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms644977%28v=vs.85%29.aspx [
Why not use the CBTProc callback function instead? You can use it for all the events you are interested in (and even more).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644977%28v=vs.85%29.aspx[^]

Good luck!


我需要使用EVENT_SYSTEM_MINIMIZESTART或EVENT_SYSTEM_MINIMIZEEND SetWinEventHook 函数的eventMin和eventMax参数来表示我是有兴趣接收这些事件之一和EVENT_SYSTEM_FOREGROUND的通知.
i need to use either the EVENT_SYSTEM_MINIMIZESTART or EVENT_SYSTEM_MINIMIZEEND event constant to receive notification of window objects being minimized.

i Used the eventMin and eventMax parameters of the SetWinEventHook function to indicate that i am interested in receiving notifications for one of these events and EVENT_SYSTEM_FOREGROUND.


这篇关于活动窗口更改时的Windows回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆