From 568c763084b926a6f2d632b7ac65b9ec8280752f Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 10 六月 2025 15:41:34 +0800 Subject: [PATCH] 网页功能完善 --- win32_util.py | 77 +++++++++++++++++++++++++++++++++----- 1 files changed, 67 insertions(+), 10 deletions(-) diff --git a/win32_util.py b/win32_util.py index 87d1a9d..5c5fc10 100644 --- a/win32_util.py +++ b/win32_util.py @@ -2,34 +2,71 @@ import cv2 import numpy +import win32api import win32con import win32gui import win32ui +import ctypes +from ctypes import wintypes + +from utils import invalid_hwnds_manager def is_visible(hwnd): return win32gui.IsWindowVisible(hwnd) +# 瀹氫箟甯搁噺 +SMTO_NORMAL = 0x0000 +SMTO_BLOCK = 0x0001 +SMTO_ABORTIFHUNG = 0x0002 +SMTO_NOTIMEOUTIFNOTHUNG = 0x0008 + +text_hwnds_doing = set() + + +def get_title(hwnd): + length = ctypes.c_int(256) + buffer = ctypes.create_unicode_buffer(length.value) + ctypes.windll.user32.SendMessageTimeoutW( + hwnd, + ctypes.c_int(0x000D), # WM_GETTEXT + ctypes.c_int(255), + ctypes.c_void_p(buffer), + ctypes.c_int(0x0002), # SMTO_ABORTIFHUNG + ctypes.c_int(1000), + ctypes.pointer(ctypes.c_int(0)) + ) + return buffer.value + + def getText(hwnd): - bufSize = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1 - buffer = array.array('b', b'\x00\x00' * bufSize) - win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, bufSize, buffer) - text = win32gui.PyGetString(buffer.buffer_info()[0], bufSize - 1) - return text.replace("\x00", "").strip() + text_hwnds_doing.add(hwnd) + try: + bufSize = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1 + buffer = array.array('b', b'\x00\x00' * bufSize) + win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, bufSize, buffer) + text = win32gui.PyGetString(buffer.buffer_info()[0], bufSize - 1) + return text.replace("\x00", "").strip() + finally: + text_hwnds_doing.discard(hwnd) # 鏍规嵁鏍囬妯$硦鍖归厤 -def search_window(title): +def search_window(title, max_count=3): + invalid_hwnds = invalid_hwnds_manager.get_hwnds() hwnds = [] try: hwnd = win32gui.GetDesktopWindow() temp = None while True: - if temp and win32gui.IsWindowVisible(temp): - str_ = getText(temp) - if str_.find(title) > -1: - hwnds.append(temp) + if temp not in invalid_hwnds: + if temp and win32gui.IsWindowVisible(temp): + str_ = getText(temp) + if str_.find(title) > -1: + hwnds.append(temp) + if len(hwnds) >= max_count: + break temp = win32gui.FindWindowEx(hwnd, temp, None, None) if not temp: break @@ -122,6 +159,26 @@ return im_opencv +def move_window(hwnd, x, y): + """ + 绉诲姩绐楀彛 + :param hwnd: + :param x: + :param y: + :return: + """ + try: + # 鑾峰彇绐楀彛鐨勫綋鍓嶇煩褰㈠尯鍩� + rect = win32gui.GetWindowRect(hwnd) + width = rect[2] - rect[0] + height = rect[3] - rect[1] + # 绉诲姩绐楀彛鍒版寚瀹氫綅缃� (x, y)锛屽苟淇濇寔绐楀彛鐨勫搴﹀拰楂樺害涓嶅彉 + win32gui.MoveWindow(hwnd, x, y, width, height, True) + print(f"绐楀彛宸茬Щ鍔ㄥ埌浣嶇疆 ({x}, {y})") + except Exception as e: + print(f"绉诲姩绐楀彛澶辫触: {e}") + + if __name__ == "__main__": # print(search_window("鍓睆1")) # visual_click(0x00152876, (110, 90)) -- Gitblit v1.8.0