-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.py
More file actions
39 lines (30 loc) · 1.22 KB
/
Copy pathwindow.py
File metadata and controls
39 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Windows 窗口查找工具"""
import ctypes
from typing import List, Optional, Tuple
import win32gui
try:
ctypes.windll.user32.SetProcessDPIAware()
except Exception:
pass
def _enum_matching_windows(keyword: str) -> list[tuple[int, str]]:
matches: list[tuple[int, str]] = []
def _handler(hwnd: int, _ctx: object) -> None:
if not win32gui.IsWindowVisible(hwnd):
return
if win32gui.IsIconic(hwnd):
return
title = win32gui.GetWindowText(hwnd)
if title and keyword in title:
matches.append((hwnd, title))
win32gui.EnumWindows(_handler, None)
return matches
def list_windows_by_keyword(keyword: str) -> List[Tuple[int, str, Tuple[int, int, int, int]]]:
"""返回所有匹配窗口的 (hwnd, title, (x, y, w, h))。"""
results: List[Tuple[int, str, Tuple[int, int, int, int]]] = []
for hwnd, title in _enum_matching_windows(keyword):
left, top, right, bottom = win32gui.GetClientRect(hwnd)
client_w = right - left
client_h = bottom - top
screen_left, screen_top = win32gui.ClientToScreen(hwnd, (0, 0))
results.append((hwnd, title, (screen_left, screen_top, client_w, client_h)))
return results