|
| 1 | +from ctypes import WINFUNCTYPE, c_bool, c_int, POINTER, create_unicode_buffer |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from je_auto_control.windows.core.utils.win32_ctype_input import user32 |
| 5 | +from je_auto_control.windows.keyboard.win32_ctype_keyboard_control import press_key |
| 6 | + |
| 7 | +EnumWindows = user32.EnumWindows |
| 8 | +EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) |
| 9 | +GetWindowText = user32.GetWindowTextW |
| 10 | +GetWindowTextLength = user32.GetWindowTextLengthW |
| 11 | +IsWindowVisible = user32.IsWindowVisible |
| 12 | +FindWindowW = user32.FindWindowW |
| 13 | +PostMessageW = user32.PostMessageW |
| 14 | +SendMessageW = user32.SendMessageW |
| 15 | + |
| 16 | + |
| 17 | +def get_all_window_hwnd(): |
| 18 | + window_info = [] |
| 19 | + |
| 20 | + def _foreach_window(hwnd, l_param): |
| 21 | + if IsWindowVisible(hwnd): |
| 22 | + length = GetWindowTextLength(hwnd) |
| 23 | + buff = create_unicode_buffer(length + 1) |
| 24 | + GetWindowText(hwnd, buff, length + 1) |
| 25 | + window_info.append((hwnd, buff.value)) |
| 26 | + return True |
| 27 | + |
| 28 | + EnumWindows(EnumWindowsProc(_foreach_window), 0) |
| 29 | + return window_info |
| 30 | + |
| 31 | + |
| 32 | +def get_one_window_hwnd(window_class: Union[None, str], window_name: Union[None, str]): |
| 33 | + return FindWindowW(window_class, window_name) |
| 34 | + |
| 35 | + |
| 36 | +def send_key_to_window(window_name: str, action_message: int, |
| 37 | + key_code_1: int, key_code_2: int): |
| 38 | + _hwnd = FindWindowW(window_name) |
| 39 | + post_status = SendMessageW(_hwnd, action_message, key_code_1, key_code_2) |
| 40 | + return _hwnd, post_status |
| 41 | + |
| 42 | + |
| 43 | +def post_key_to_window(window_name: str, action_message: int, |
| 44 | + key_code_1: int, key_code_2: int): |
| 45 | + _hwnd = FindWindowW(window_name) |
| 46 | + post_status = PostMessageW(_hwnd, action_message, key_code_1, key_code_2) |
| 47 | + return _hwnd, post_status |
0 commit comments