gpt4 book ai didi

c# - 捕获事件窗口更改事件时出现问题

转载 作者:太空狗 更新时间:2023-10-30 01:09:25 26 4
gpt4 key购买 nike

我正在 try catch Window 的事件窗口更改事件。比方说,如果用户正在使用 VS 而他切换到 IE,我想捕获此事件窗口更改事件。我在互联网上搜索并找到了很多示例,但似乎对我没有任何用处。

这是我到目前为止编写的代码,我不确定其中有什么问题。我无法通过此捕获所需的事件

class Program
{
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;

static void Main(string[] args)
{
WinEventDelegate dele = new WinEventDelegate(WinEventProc);
IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
Console.ReadKey();
}

static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Console.WriteLine("Something");
}
}

WinEventProc 永远不会被调用,任何人都可以识别我在编程上或概念上做错了什么吗?我正在使用 Windows 7

最佳答案

您需要发送消息才能接收 WinEvents - 而 Console.ReadKey() 可能不会这样做。现在尝试使用 MessageBox 进行测试 - 稍后用适当的消息循环替换。

来自 MSDN :

Remarks:
...
The client thread that calls SetWinEventHook must have a message loop in order to receive events

关于c# - 捕获事件窗口更改事件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6855569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com