gpt4 book ai didi

c# - Windows 回调,当事件窗口改变时

转载 作者:行者123 更新时间:2023-12-05 01:48:38 25 4
gpt4 key购买 nike

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

是的,您需要使用 EVENT_SYSTEM_MINIMIZESTARTEVENT_SYSTEM_MINIMIZEEND event constant接收窗口对象被最小化的通知。

使用 SetWinEventHook functioneventMineventMax 参数表示您有兴趣接收这些事件之一的通知EVENT_SYSTEM_FOREGROUND

关于c# - Windows 回调,当事件窗口改变时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271855/

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