检测使用C#更改的活动窗口而不进行轮询
每当当前活动窗口改变时,如何调用callback函数。 我已经看到了如何使用CBTProc完成。 但是,全局事件并不容易与托pipe代码挂钩。 我有兴趣find一种不需要轮询的方式。 我更喜欢事件驱动的方法。
问候
创build一个新的Windows窗体项目,添加一个文本框,使其多行,并设置文本框的Dock属性来填充,命名它日志并粘贴在下面的代码(您需要添加System.Runtime.InteropServices到您的使用)。 ..
WinEventDelegate dele = null; public Form1() { InitializeComponent(); dele = new WinEventDelegate(WinEventProc); IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT); } 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); 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); private 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 void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { Log.Text += GetActiveWindowTitle() + "\r\n"; }
我知道这个线程是旧的,但为了将来的使用:当运行代码,你会发现一段时间后崩溃。 这是由Form构造函数中的行引起的:
public Form1() { InitializeComponent(); WinEventDelegate dele = new WinEventDelegate(WinEventProc);//<-causing ERROR IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT); }
而不是上面做了以下修改:
public Form1() { InitializeComponent(); dele = new WinEventDelegate(WinEventProc); IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT); } WinEventDelegate dele = null;
..works现如预期!
您可以使用SetWinEventHook
并监听EVENT_SYSTEM_FOREGROUND
事件。 使用WINEVENT_OUTOFCONTEXT
标志来避免全局挂钩问题。