From c7c25b104f8d19a2969e5e2f431f6962cb8e6ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sun, 14 Dec 2025 12:15:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20SetWinEventHook=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E9=81=AE=E7=BD=A9=E9=92=88?= =?UTF-8?q?=E5=AF=B9=E5=8E=9F=E7=A5=9E=E7=AA=97=E5=8F=A3=E7=9A=84=E8=B7=9F?= =?UTF-8?q?=E9=9A=8F=E5=BB=B6=E8=BF=9F=20#2540?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GameTask/TaskTriggerDispatcher.cs | 55 +++++++++++++++++-- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs b/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs index 75e2989b..ffc045e6 100644 --- a/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs +++ b/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs @@ -39,6 +39,15 @@ namespace BetterGenshinImpact.GameTask private static readonly object _triggerListLocker = new(); + private User32.HWINEVENTHOOK _winEventHookMoveSize; + private User32.HWINEVENTHOOK _winEventHookLocation; + private User32.WinEventProc _winEventProc; + private const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A; + private const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B; + private const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B; + private const uint WINEVENT_SKIPOWNTHREAD = 0x0001; + private const uint WINEVENT_SKIPOWNPROCESS = 0x0002; + public event EventHandler? UiTaskStopTickEvent; public event EventHandler? UiTaskStartTickEvent; @@ -132,6 +141,12 @@ namespace BetterGenshinImpact.GameTask } ); + // 使用 SetWinEventHook 监听窗口移动和大小变化事件 + _winEventProc = WinEventCallback; + var flags = (User32.WINEVENT)(WINEVENT_SKIPOWNPROCESS | WINEVENT_SKIPOWNTHREAD); + _winEventHookMoveSize = User32.SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, default, _winEventProc, 0, 0, flags); + _winEventHookLocation = User32.SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, default, _winEventProc, 0, 0, flags); + // 启动定时器 _frameIndex = 0; _timer.Interval = interval; @@ -147,6 +162,16 @@ namespace BetterGenshinImpact.GameTask GameCapture?.Stop(); _gameRect = RECT.Empty; _prevGameActive = false; + if (_winEventHookMoveSize != default) + { + User32.UnhookWinEvent(_winEventHookMoveSize); + _winEventHookMoveSize = default; + } + if (_winEventHookLocation != default) + { + User32.UnhookWinEvent(_winEventHookLocation); + _winEventHookLocation = default; + } } public void StartTimer() @@ -272,11 +297,11 @@ namespace BetterGenshinImpact.GameTask } _prevGameActive = active; - // 移动游戏窗口的时候同步遮罩窗口的位置,此时不进行捕获 - if (SyncMaskWindowPosition()) - { - return; - } + // // 移动游戏窗口的时候同步遮罩窗口的位置,此时不进行捕获 + // if (SyncMaskWindowPosition()) + // { + // return; + // } } if (_triggers == null || !_triggers.Exists(t => t.IsEnabled)) @@ -382,6 +407,24 @@ namespace BetterGenshinImpact.GameTask return rect.Width == 0 || rect.Height == 0; } + private void WinEventCallback(User32.HWINEVENTHOOK hWinEventHook, uint @event, HWND hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) + { + var target = TaskContext.Instance().GameHandle; + if (target == IntPtr.Zero) + { + return; + } + if (idObject != 0) + { + return; + } + var hwndPtr = hwnd.DangerousGetHandle(); + if (hwndPtr == target) + { + SyncMaskWindowPosition(); + } + } + public void TakeScreenshot() { try @@ -430,4 +473,4 @@ namespace BetterGenshinImpact.GameTask } } } -} \ No newline at end of file +}