pywgc 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pywgc/__init__.py +87 -0
- pywgc/_win32.py +348 -0
- pywgc/capture.py +872 -0
- pywgc/errors.py +41 -0
- pywgc/py.typed +0 -0
- pywgc-0.1.0.dist-info/METADATA +389 -0
- pywgc-0.1.0.dist-info/RECORD +10 -0
- pywgc-0.1.0.dist-info/WHEEL +5 -0
- pywgc-0.1.0.dist-info/licenses/LICENSE +21 -0
- pywgc-0.1.0.dist-info/top_level.txt +1 -0
pywgc/__init__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""pywgc —— 低延迟 Windows 窗口抓屏。
|
|
2
|
+
|
|
3
|
+
``Windows.Graphics.Capture`` 优先、GDI ``BitBlt`` 降级的双后端封装,对外只暴露
|
|
4
|
+
一个拉取式的 ``grab()``。
|
|
5
|
+
|
|
6
|
+
>>> from pywgc import WindowGrabber
|
|
7
|
+
>>> with WindowGrabber(title="记事本") as g:
|
|
8
|
+
... frame = g.grab()
|
|
9
|
+
... print(g.backend, frame.shape)
|
|
10
|
+
|
|
11
|
+
单帧成本对比(同一窗口、800x684 ROI):
|
|
12
|
+
|
|
13
|
+
=========== ========================= ==================
|
|
14
|
+
后端 单次 ``grab()`` 耗时 CPU / 帧
|
|
15
|
+
=========== ========================= ==================
|
|
16
|
+
``wgc`` 0.4 ~ 0.7 ms 约 3.1 ms
|
|
17
|
+
``gdi`` 16.4 ~ 17.0 ms 约 12.4 ms
|
|
18
|
+
=========== ========================= ==================
|
|
19
|
+
|
|
20
|
+
行为差异(这才是选型的决定性因素,不是速度):
|
|
21
|
+
|
|
22
|
+
* ``wgc`` 抓**窗口表面** —— 窗口被遮挡、失去前台、部分移出屏幕都照常出帧;
|
|
23
|
+
* ``gdi`` 抓**屏幕像素** —— 被遮挡时抓到遮挡物,完全遮挡时返回全黑。
|
|
24
|
+
|
|
25
|
+
因此 ``backend="auto"``(默认)总是先尝试 ``wgc``,失败才降级。
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
from . import _win32 as win32
|
|
31
|
+
from ._win32 import (
|
|
32
|
+
client_rect,
|
|
33
|
+
enable_dpi_awareness,
|
|
34
|
+
frame_rect,
|
|
35
|
+
is_foreground,
|
|
36
|
+
is_minimized,
|
|
37
|
+
is_visible,
|
|
38
|
+
list_windows,
|
|
39
|
+
move_window,
|
|
40
|
+
pick_main_window,
|
|
41
|
+
show_no_activate,
|
|
42
|
+
window_ops_blocked,
|
|
43
|
+
)
|
|
44
|
+
from ._win32 import is_alive as is_window_alive
|
|
45
|
+
from .capture import AsyncGrabber, WindowGrabber, available_backends, wait_for_window
|
|
46
|
+
from .errors import (
|
|
47
|
+
BackendUnavailable,
|
|
48
|
+
BlankFrameError,
|
|
49
|
+
CaptureError,
|
|
50
|
+
StaleFrameError,
|
|
51
|
+
WindowNotFound,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
__version__ = "0.1.0"
|
|
55
|
+
|
|
56
|
+
__all__ = [
|
|
57
|
+
# 抓屏
|
|
58
|
+
"WindowGrabber",
|
|
59
|
+
"AsyncGrabber",
|
|
60
|
+
"wait_for_window",
|
|
61
|
+
"available_backends",
|
|
62
|
+
# 窗口工具
|
|
63
|
+
"win32",
|
|
64
|
+
"enable_dpi_awareness",
|
|
65
|
+
"list_windows",
|
|
66
|
+
"pick_main_window",
|
|
67
|
+
"client_rect",
|
|
68
|
+
"frame_rect",
|
|
69
|
+
"is_window_alive",
|
|
70
|
+
"is_visible",
|
|
71
|
+
"is_minimized",
|
|
72
|
+
"is_foreground",
|
|
73
|
+
"show_no_activate",
|
|
74
|
+
"move_window",
|
|
75
|
+
"window_ops_blocked",
|
|
76
|
+
# 异常
|
|
77
|
+
"CaptureError",
|
|
78
|
+
"WindowNotFound",
|
|
79
|
+
"BackendUnavailable",
|
|
80
|
+
"StaleFrameError",
|
|
81
|
+
"BlankFrameError",
|
|
82
|
+
"__version__",
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
# 导入即声明 DPI 感知:否则在 125%/150% 缩放下客户区坐标是逻辑坐标,
|
|
86
|
+
# 而 GDI 按物理像素工作,抓到的区域会整体偏移。
|
|
87
|
+
enable_dpi_awareness()
|
pywgc/_win32.py
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"""Win32 底层封装:窗口枚举、几何查询、窗口操作与权限诊断。
|
|
2
|
+
|
|
3
|
+
本模块只依赖标准库 ``ctypes``,不引入任何第三方包,方便单独导入和测试。
|
|
4
|
+
所有函数在非 Windows 平台导入时即失败(见文件末尾的平台检查),
|
|
5
|
+
因为这里的每一项能力都直接对应 ``user32`` 的符号。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import ctypes
|
|
11
|
+
import ctypes.wintypes as wt
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
if sys.platform != "win32":
|
|
15
|
+
raise ImportError(
|
|
16
|
+
"pywgc only supports Windows: this module binds user32/gdi32 directly. "
|
|
17
|
+
f"Current platform is {sys.platform!r}."
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"enable_dpi_awareness",
|
|
22
|
+
"iter_windows",
|
|
23
|
+
"list_windows",
|
|
24
|
+
"pick_main_window",
|
|
25
|
+
"client_rect",
|
|
26
|
+
"frame_rect",
|
|
27
|
+
"is_alive",
|
|
28
|
+
"is_minimized",
|
|
29
|
+
"is_foreground",
|
|
30
|
+
"is_visible",
|
|
31
|
+
"show_no_activate",
|
|
32
|
+
"move_window",
|
|
33
|
+
"window_ops_blocked",
|
|
34
|
+
"ERROR_ACCESS_DENIED",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
user32 = ctypes.WinDLL("user32", use_last_error=True)
|
|
38
|
+
|
|
39
|
+
# ---- 常量 ---------------------------------------------------------------
|
|
40
|
+
SW_SHOWNOACTIVATE = 4
|
|
41
|
+
SWP_NOSIZE = 0x0001
|
|
42
|
+
SWP_NOMOVE = 0x0002
|
|
43
|
+
SWP_NOZORDER = 0x0004
|
|
44
|
+
SWP_NOACTIVATE = 0x0010
|
|
45
|
+
ERROR_ACCESS_DENIED = 5
|
|
46
|
+
|
|
47
|
+
WNDENUMPROC = ctypes.WINFUNCTYPE(wt.BOOL, wt.HWND, wt.LPARAM)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class RECT(ctypes.Structure):
|
|
51
|
+
_fields_ = [
|
|
52
|
+
("left", wt.LONG),
|
|
53
|
+
("top", wt.LONG),
|
|
54
|
+
("right", wt.LONG),
|
|
55
|
+
("bottom", wt.LONG),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class POINT(ctypes.Structure):
|
|
60
|
+
_fields_ = [("x", wt.LONG), ("y", wt.LONG)]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# ---- 函数签名 -----------------------------------------------------------
|
|
64
|
+
# 必须显式声明:不设 argtypes/restype 时 HWND 会按 c_int 截断,
|
|
65
|
+
# 而 GetForegroundWindow 的返回值在高位为 1 时会变成负数,比较永远不等。
|
|
66
|
+
user32.IsWindow.argtypes = [wt.HWND]
|
|
67
|
+
user32.IsWindow.restype = wt.BOOL
|
|
68
|
+
user32.IsIconic.argtypes = [wt.HWND]
|
|
69
|
+
user32.IsIconic.restype = wt.BOOL
|
|
70
|
+
user32.IsWindowVisible.argtypes = [wt.HWND]
|
|
71
|
+
user32.IsWindowVisible.restype = wt.BOOL
|
|
72
|
+
user32.GetWindowTextLengthW.argtypes = [wt.HWND]
|
|
73
|
+
user32.GetWindowTextLengthW.restype = ctypes.c_int
|
|
74
|
+
user32.GetWindowTextW.argtypes = [wt.HWND, wt.LPWSTR, ctypes.c_int]
|
|
75
|
+
user32.GetWindowTextW.restype = ctypes.c_int
|
|
76
|
+
user32.GetWindowThreadProcessId.argtypes = [wt.HWND, ctypes.POINTER(wt.DWORD)]
|
|
77
|
+
user32.GetWindowThreadProcessId.restype = wt.DWORD
|
|
78
|
+
user32.GetWindowRect.argtypes = [wt.HWND, ctypes.POINTER(RECT)]
|
|
79
|
+
user32.GetWindowRect.restype = wt.BOOL
|
|
80
|
+
user32.GetClientRect.argtypes = [wt.HWND, ctypes.POINTER(RECT)]
|
|
81
|
+
user32.GetClientRect.restype = wt.BOOL
|
|
82
|
+
user32.ClientToScreen.argtypes = [wt.HWND, ctypes.POINTER(POINT)]
|
|
83
|
+
user32.ClientToScreen.restype = wt.BOOL
|
|
84
|
+
user32.GetForegroundWindow.argtypes = []
|
|
85
|
+
user32.GetForegroundWindow.restype = wt.HWND
|
|
86
|
+
user32.ShowWindow.argtypes = [wt.HWND, ctypes.c_int]
|
|
87
|
+
user32.ShowWindow.restype = wt.BOOL
|
|
88
|
+
user32.SetWindowPos.argtypes = [
|
|
89
|
+
wt.HWND,
|
|
90
|
+
wt.HWND,
|
|
91
|
+
ctypes.c_int,
|
|
92
|
+
ctypes.c_int,
|
|
93
|
+
ctypes.c_int,
|
|
94
|
+
ctypes.c_int,
|
|
95
|
+
wt.UINT,
|
|
96
|
+
]
|
|
97
|
+
user32.SetWindowPos.restype = wt.BOOL
|
|
98
|
+
|
|
99
|
+
_DPI_SET = False
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def enable_dpi_awareness() -> None:
|
|
103
|
+
"""声明本进程为 per-monitor DPI aware(幂等)。
|
|
104
|
+
|
|
105
|
+
不做这一步的话,在 125% / 150% 缩放下 ``GetClientRect`` 与
|
|
106
|
+
``ClientToScreen`` 给出的是**逻辑坐标**,而 GDI 抓屏按**物理像素**工作,
|
|
107
|
+
结果就是抓到的区域整体偏移 —— 表现为"画面错位",极难联想到 DPI。
|
|
108
|
+
"""
|
|
109
|
+
global _DPI_SET
|
|
110
|
+
if _DPI_SET:
|
|
111
|
+
return
|
|
112
|
+
try:
|
|
113
|
+
# PROCESS_PER_MONITOR_DPI_AWARE = 2
|
|
114
|
+
ctypes.windll.shcore.SetProcessDpiAwareness(2)
|
|
115
|
+
except Exception: # noqa: BLE001 - Win7 没有 shcore,退回老 API
|
|
116
|
+
try:
|
|
117
|
+
user32.SetProcessDPIAware()
|
|
118
|
+
except Exception: # noqa: BLE001
|
|
119
|
+
pass
|
|
120
|
+
_DPI_SET = True
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _window_text(hwnd: int) -> str:
|
|
124
|
+
n = user32.GetWindowTextLengthW(hwnd)
|
|
125
|
+
buf = ctypes.create_unicode_buffer(n + 2)
|
|
126
|
+
user32.GetWindowTextW(hwnd, buf, n + 2)
|
|
127
|
+
return buf.value
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def iter_windows(title_prefix: str | None = None, pid: int | None = None):
|
|
131
|
+
"""枚举顶层窗口,按需按标题前缀 / 进程号过滤,逐个 yield 字典。
|
|
132
|
+
|
|
133
|
+
每个字典的键:``hwnd`` ``pid`` ``title`` ``visible`` ``w`` ``h`` ``x`` ``y``。
|
|
134
|
+
其中 ``x`` / ``y`` / ``w`` / ``h`` 指的是**客户区**(不含标题栏与边框)
|
|
135
|
+
在屏幕上的位置,这也是抓屏 ROI 使用的坐标系。
|
|
136
|
+
"""
|
|
137
|
+
out = []
|
|
138
|
+
|
|
139
|
+
def _cb(hwnd, _lparam):
|
|
140
|
+
title = _window_text(hwnd)
|
|
141
|
+
wpid = wt.DWORD()
|
|
142
|
+
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(wpid))
|
|
143
|
+
if title_prefix is not None and not title.startswith(title_prefix):
|
|
144
|
+
return True
|
|
145
|
+
if pid is not None and wpid.value != pid:
|
|
146
|
+
return True
|
|
147
|
+
cr = RECT()
|
|
148
|
+
user32.GetClientRect(hwnd, ctypes.byref(cr))
|
|
149
|
+
cw, ch = cr.right - cr.left, cr.bottom - cr.top
|
|
150
|
+
pt = POINT(cr.left, cr.top)
|
|
151
|
+
user32.ClientToScreen(hwnd, ctypes.byref(pt))
|
|
152
|
+
out.append(
|
|
153
|
+
{
|
|
154
|
+
"hwnd": int(hwnd),
|
|
155
|
+
"pid": wpid.value,
|
|
156
|
+
"title": title,
|
|
157
|
+
"visible": bool(user32.IsWindowVisible(hwnd)),
|
|
158
|
+
"w": cw,
|
|
159
|
+
"h": ch,
|
|
160
|
+
"x": pt.x,
|
|
161
|
+
"y": pt.y,
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
user32.EnumWindows(WNDENUMPROC(_cb), 0)
|
|
167
|
+
yield from out
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def list_windows(title_prefix: str | None = None, pid: int | None = None) -> list:
|
|
171
|
+
"""``iter_windows`` 的列表版本。"""
|
|
172
|
+
return list(iter_windows(title_prefix, pid))
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def pick_main_window(windows, min_side: int = 100):
|
|
176
|
+
"""从一堆同名窗口里挑出最可能是"主窗口"的那个;没有候选返回 ``None``。
|
|
177
|
+
|
|
178
|
+
为什么不能简单地"可见优先":应用最小化到托盘时,渲染窗口会被
|
|
179
|
+
``SW_HIDE``(``visible=False``),反而留下一个
|
|
180
|
+
``160x28 @ (-32000,-32000)`` 的最小化壳仍是 visible。可见优先就会选中壳,
|
|
181
|
+
拿到面积仅数千、坐标在屏幕外的假窗口。
|
|
182
|
+
|
|
183
|
+
打分维度:客户区面积(主导)、可见性(加分但压不过面积)、
|
|
184
|
+
最小化(坐标在 -32000 附近或 ``IsIconic``,重罚)。
|
|
185
|
+
"""
|
|
186
|
+
cands = [w for w in windows if w.get("w", 0) > min_side and w.get("h", 0) > min_side]
|
|
187
|
+
if not cands:
|
|
188
|
+
return None
|
|
189
|
+
|
|
190
|
+
def score(w):
|
|
191
|
+
s = w["w"] * w["h"]
|
|
192
|
+
if w["visible"]:
|
|
193
|
+
s += 200_000
|
|
194
|
+
if w["x"] < -10_000 or w["y"] < -10_000:
|
|
195
|
+
s -= 500_000 # 最小化壳的经典坐标
|
|
196
|
+
try:
|
|
197
|
+
if user32.IsIconic(w["hwnd"]):
|
|
198
|
+
s -= 500_000
|
|
199
|
+
except Exception: # noqa: BLE001
|
|
200
|
+
pass
|
|
201
|
+
return s
|
|
202
|
+
|
|
203
|
+
return max(cands, key=score)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def client_rect(hwnd: int):
|
|
207
|
+
"""客户区在屏幕上的 ``(x, y, w, h)``;取不到返回 ``None``。"""
|
|
208
|
+
cr = RECT()
|
|
209
|
+
try:
|
|
210
|
+
if not user32.GetClientRect(hwnd, ctypes.byref(cr)):
|
|
211
|
+
return None
|
|
212
|
+
pt = POINT(cr.left, cr.top)
|
|
213
|
+
if not user32.ClientToScreen(hwnd, ctypes.byref(pt)):
|
|
214
|
+
return None
|
|
215
|
+
w, h = cr.right - cr.left, cr.bottom - cr.top
|
|
216
|
+
return (pt.x, pt.y, w, h)
|
|
217
|
+
except Exception: # noqa: BLE001
|
|
218
|
+
return None
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def frame_rect(hwnd: int):
|
|
222
|
+
"""窗口**外框**在屏幕上的 ``(left, top, w, h)``;取不到返回 ``None``。
|
|
223
|
+
|
|
224
|
+
注意与 :func:`client_rect` 的区别:要把窗口挪走再挪回原位,必须用外框
|
|
225
|
+
坐标 —— 用客户区坐标恢复会少挪一个标题栏的高度,抓屏 ROI 随之偏掉。
|
|
226
|
+
"""
|
|
227
|
+
r = RECT()
|
|
228
|
+
try:
|
|
229
|
+
if not user32.GetWindowRect(hwnd, ctypes.byref(r)):
|
|
230
|
+
return None
|
|
231
|
+
return (r.left, r.top, r.right - r.left, r.bottom - r.top)
|
|
232
|
+
except Exception: # noqa: BLE001
|
|
233
|
+
return None
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def is_alive(hwnd: int) -> bool:
|
|
237
|
+
"""窗口句柄是否仍然有效(进程重启后旧 hwnd 会失效)。"""
|
|
238
|
+
try:
|
|
239
|
+
return bool(user32.IsWindow(hwnd))
|
|
240
|
+
except Exception: # noqa: BLE001
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def is_minimized(hwnd: int) -> bool:
|
|
245
|
+
"""窗口是否被最小化。
|
|
246
|
+
|
|
247
|
+
最小化时两种后端都拿不到画面:GDI 抓到空图/黑图,WGC 因为没有可见表面
|
|
248
|
+
**一帧都不出**。所以这是两个后端都必须处理的情况。
|
|
249
|
+
"""
|
|
250
|
+
try:
|
|
251
|
+
return bool(user32.IsIconic(hwnd))
|
|
252
|
+
except Exception: # noqa: BLE001
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def is_visible(hwnd: int) -> bool:
|
|
257
|
+
try:
|
|
258
|
+
return bool(user32.IsWindowVisible(hwnd))
|
|
259
|
+
except Exception: # noqa: BLE001
|
|
260
|
+
return False
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def is_foreground(hwnd: int) -> bool:
|
|
264
|
+
try:
|
|
265
|
+
return user32.GetForegroundWindow() == hwnd
|
|
266
|
+
except Exception: # noqa: BLE001
|
|
267
|
+
return False
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def error_text(err: int) -> str:
|
|
271
|
+
"""把 Win32 错误码转成可读文本(失败返回空串)。"""
|
|
272
|
+
if not err:
|
|
273
|
+
return ""
|
|
274
|
+
try:
|
|
275
|
+
return ctypes.FormatError(err)
|
|
276
|
+
except Exception: # noqa: BLE001
|
|
277
|
+
return ""
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def uipi_hint(err: int) -> str:
|
|
281
|
+
"""``err == 5`` 时的可操作解释。
|
|
282
|
+
|
|
283
|
+
UIPI(User Interface Privilege Isolation)只允许**同级或更高**完整性级别的
|
|
284
|
+
进程向窗口发送消息,而 ``SetWindowPos`` / ``ShowWindow`` /
|
|
285
|
+
``SetForegroundWindow`` 本质上都是在发窗口消息。因此当目标进程以管理员
|
|
286
|
+
权限运行、而本进程没有提权时,这些调用会**静默返回 0**,``GetLastError``
|
|
287
|
+
为 ``5``,不抛任何异常。
|
|
288
|
+
|
|
289
|
+
注意 ``SendInput`` **不受**此限制(它注入的是输入队列,不是窗口消息),
|
|
290
|
+
所以"能往窗口注入按键、却挪不动窗口"是完全正常的组合,不必怀疑代码。
|
|
291
|
+
"""
|
|
292
|
+
if err == ERROR_ACCESS_DENIED:
|
|
293
|
+
return (
|
|
294
|
+
" (UIPI: the target window belongs to a process with a higher "
|
|
295
|
+
"integrity level. Run this process elevated, or run the target "
|
|
296
|
+
"process without elevation.)"
|
|
297
|
+
)
|
|
298
|
+
return ""
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def window_ops_blocked(hwnd: int):
|
|
302
|
+
"""本进程能否对该窗口做窗口操作。返回 ``(blocked, reason)``。
|
|
303
|
+
|
|
304
|
+
为什么要单独探测:这类调用失败时**不抛异常**,只给出返回值和
|
|
305
|
+
``GetLastError``。若不主动检查,故障会表现成"某项检查莫名失败但看不出
|
|
306
|
+
原因"。
|
|
307
|
+
|
|
308
|
+
探测方式为 ``SWP_NOMOVE | SWP_NOSIZE`` 且坐标/尺寸全零 —— 不会真正改变
|
|
309
|
+
窗口状态,只是让 Windows 走一遍权限检查,因此可以安全地反复调用。
|
|
310
|
+
"""
|
|
311
|
+
try:
|
|
312
|
+
ctypes.set_last_error(0)
|
|
313
|
+
user32.SetWindowPos(
|
|
314
|
+
hwnd, None, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE
|
|
315
|
+
)
|
|
316
|
+
err = ctypes.get_last_error()
|
|
317
|
+
if err == ERROR_ACCESS_DENIED:
|
|
318
|
+
return True, "ERROR_ACCESS_DENIED(5): target process has a higher integrity level"
|
|
319
|
+
return False, ""
|
|
320
|
+
except Exception as e: # noqa: BLE001
|
|
321
|
+
return False, str(e)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def show_no_activate(hwnd: int) -> bool:
|
|
325
|
+
"""显示窗口但不激活它(不抢焦点)。"""
|
|
326
|
+
try:
|
|
327
|
+
return bool(user32.ShowWindow(hwnd, SW_SHOWNOACTIVATE))
|
|
328
|
+
except Exception: # noqa: BLE001
|
|
329
|
+
return False
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def move_window(hwnd: int, x: int, y: int):
|
|
333
|
+
"""只改窗口位置:不动尺寸、不动 Z 序、不抢焦点。返回 ``(ok, err)``。
|
|
334
|
+
|
|
335
|
+
``SWP_NOSIZE`` 是必需的 —— 尺寸一变,抓屏侧就得重建整个 WGC 会话,
|
|
336
|
+
白白付出一次 D3D 会话初始化的代价。
|
|
337
|
+
|
|
338
|
+
失败时返回值里带 ``GetLastError``:``SetWindowPos`` 返回 0 但**不抛异常**,
|
|
339
|
+
只包一层 ``try/except`` 的话会静默失败,排查时完全无从下手。
|
|
340
|
+
"""
|
|
341
|
+
try:
|
|
342
|
+
ctypes.set_last_error(0)
|
|
343
|
+
ok = user32.SetWindowPos(
|
|
344
|
+
hwnd, None, int(x), int(y), 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE
|
|
345
|
+
)
|
|
346
|
+
return bool(ok), (0 if ok else ctypes.get_last_error())
|
|
347
|
+
except Exception as e: # noqa: BLE001
|
|
348
|
+
return False, getattr(e, "winerror", 0) or 0
|