|
| 1 | +import psutil |
| 2 | +import win32gui |
| 3 | +import win32process |
| 4 | + |
| 5 | + |
| 6 | +class StatusMachine(object): |
| 7 | + known_pid = None |
| 8 | + |
| 9 | + |
| 10 | +def get_window_info(pid): |
| 11 | + """获取指定进程 ID 的所有窗口信息""" |
| 12 | + windows = [] |
| 13 | + |
| 14 | + def callback(hwnd, ctx): |
| 15 | + if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): |
| 16 | + _, window_pid = win32process.GetWindowThreadProcessId(hwnd) |
| 17 | + if window_pid == pid: |
| 18 | + title = win32gui.GetWindowText(hwnd) |
| 19 | + rect = win32gui.GetWindowRect(hwnd) |
| 20 | + windows.append( |
| 21 | + { |
| 22 | + "hwnd": hwnd, |
| 23 | + "title": title, |
| 24 | + "position": (rect[0], rect[1]), # 左上角坐标 |
| 25 | + "size": (rect[2] - rect[0], rect[3] - rect[1]), # 宽高 |
| 26 | + } |
| 27 | + ) |
| 28 | + |
| 29 | + win32gui.EnumWindows(callback, None) |
| 30 | + return windows |
| 31 | + |
| 32 | + |
| 33 | +def find_process_by_name(process_name): |
| 34 | + """查找指定名称的进程并返回其 PID""" |
| 35 | + for proc in psutil.process_iter(["name", "pid"]): |
| 36 | + if proc.info["name"].lower() == process_name.lower(): |
| 37 | + return proc.info["pid"] |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +# 主程序 |
| 42 | +if __name__ == "__main__": |
| 43 | + # 进程名称 |
| 44 | + target_process = "AssetRipper.GUI.Free.exe" |
| 45 | + |
| 46 | + # 查找进程 |
| 47 | + pid = find_process_by_name(target_process) |
| 48 | + if not pid: |
| 49 | + print(f"未找到进程: {target_process}") |
| 50 | + else: |
| 51 | + print(f"找到进程,PID: {pid}") |
| 52 | + |
| 53 | + # 获取窗口信息 |
| 54 | + windows = get_window_info(pid) |
| 55 | + if windows: |
| 56 | + print(f"找到 {len(windows)} 个窗口:") |
| 57 | + for window in windows: |
| 58 | + print(f" - 句柄: {window['hwnd']}") |
| 59 | + print(f" - 标题: {window['title']}") |
| 60 | + print(f" - 位置: {window['position']}") |
| 61 | + print(f" - 大小: {window['size']}") |
| 62 | + print(" -------------------") |
| 63 | + else: |
| 64 | + print("未找到可见窗口或窗口未启用") |
0 commit comments