WindowGuard 补上窗口位置纠正(窗口出界会让抓图整体错位)

Browse Source
现象: 任务日志一直在动、进程正常,但几乎不点击 —— 实测 30 秒只点了 1 次。
      同时图像匹配相似度全在 0.2~0.5(AALC 阈值通常 0.8)。

排查: 游戏窗口位置是 (-8, 39) —— 左边 8 像素在屏幕外。

原因: 前台模式(background_click=false)下 AALC 从**屏幕**抓图(BitBlt 屏幕 DC),
      窗口一出界, 抓到的画面就整体偏移, 模板匹配自然全部失败; 而 AALC 的
      set_win_position=free 表示它不碰窗口位置, 所以没人管这件事。

      手动把窗口挪到 (0,0) 后, 30 秒内点击立刻恢复到 5 次 —— 确认因果。

修复: WindowGuard 原来只纠正客户区尺寸, 现在先纠正位置:
      用 monitor_info["Work"](工作区, 已扣任务栏)判断窗口是否完整落在里面,
      不在就用 SetWindowPos 夹回边界内(保留尺寸、不夺焦点)。
      位置放在尺寸检查之前 —— 出界时尺寸检查本身也没意义。

游戏每次启动都会恢复自己保存的位置, 所以这个纠正必须常驻, 不能只在启动时做一次。
This commit is contained in:
Codex Agent
2026-10-22 17:15:35 +08:00
parent 8a9495b185
commit 13c038a175
3 changed files with 230 additions and 120 deletions
+49
View File
@@ -63,12 +63,61 @@ class WindowGuard(threading.Thread):
size = int(self.cfg.set_win_size)
return int(size * 16 / 9), size
def check_position(self):
"""窗口有一截跑到屏幕外时挪回工作区。
背景(2026-10-22 实测): 游戏会恢复自己保存的窗口位置, 出现过 (-8, 39) 这种
—— 左边 8 像素在屏幕外。前台模式(background_click=false)下 AALC 从**屏幕**
抓图, 窗口一出界抓到的画面就整体错位, 模板匹配相似度掉到 0.2~0.5, 表现是
"日志一直在动但几乎不点击"(实测 30 秒只点 1 次, 挪回来立刻恢复)。
set_win_position=free 时 AALC 不碰位置, 所以这里补上。
返回 'ok' / 'fixed' / None(没法判断)。
"""
try:
import win32con
import win32gui
except ImportError:
return None
handle = self.screen.handle
hwnd = handle._hwnd
try:
left, top, right, bottom = handle.monitor_info["Work"]
except Exception:
return None
try:
x, y, r, b = win32gui.GetWindowRect(hwnd)
except Exception:
return None
width, height = r - x, b - y
if width <= 0 or height <= 0:
return None
# 已经完整落在工作区内就不动
if x >= left and y >= top and r <= right and b <= bottom:
return 'ok'
new_x = max(left, min(x, right - width))
new_y = max(top, min(y, bottom - height))
try:
win32gui.SetWindowPos(hwnd, 0, new_x, new_y, 0, 0,
win32con.SWP_NOSIZE | win32con.SWP_NOZORDER |
win32con.SWP_NOACTIVATE)
except Exception as e:
self.log('[lc_mcp] window guard: 挪窗口失败 %s: %s' % (type(e).__name__, e))
return None
self.fixes += 1
self.log('[lc_mcp] window guard: 窗口 (%d,%d) 有部分在屏幕外, 已挪到 (%d,%d)'
% (x, y, new_x, new_y))
return 'fixed'
def check_once(self):
"""返回 'ok' / 'fixed' / 'failed' / None(没有可检查的窗口)。"""
handle = self.screen.handle
# set_windows 关闭表示用户自己管窗口, AALC 也不调, 这里同样不插手
if self.cfg.simulator or not self.cfg.set_windows or not handle._hwnd or handle.isMinimized:
return None
# 位置先修: 出界会让抓图错位, 那种情况下尺寸检查也没意义
if self.check_position() == 'fixed':
time.sleep(0.5)
current = (handle.width(True), handle.height(True))
want = self.want()
if current == want or 0 in current: