|
| 1 | +""" |
| 2 | +获取当前焦点控件的句柄 |
| 3 | +等待3秒后,获取你光标所在的可输入控件的句柄 |
| 4 | +""" |
| 5 | + |
| 6 | +import win32gui |
| 7 | +import win32api |
| 8 | +import time |
| 9 | +import ctypes |
| 10 | + |
| 11 | + |
| 12 | +def get_focused_control(): |
| 13 | + """获取当前拥有焦点的控件句柄""" |
| 14 | + |
| 15 | + print("⏰ 3 秒倒计时,请将光标放在目标输入框...") |
| 16 | + for i in range(3, 0, -1): |
| 17 | + print(f" {i}...", end="\r") |
| 18 | + time.sleep(1) |
| 19 | + print("\n") |
| 20 | + |
| 21 | + print("🔍 正在获取焦点控件...") |
| 22 | + |
| 23 | + # 方法1:获取前台窗口 |
| 24 | + foreground_hwnd = win32gui.GetForegroundWindow() |
| 25 | + foreground_title = win32gui.GetWindowText(foreground_hwnd) |
| 26 | + print(f"\n✅ 前台窗口: {foreground_title}") |
| 27 | + print(f" 句柄: {foreground_hwnd}") |
| 28 | + |
| 29 | + # 方法2:获取焦点控件(使用 GetFocus) |
| 30 | + # 注意:GetFocus 只能获取当前线程的焦点 |
| 31 | + try: |
| 32 | + # 获取前台窗口的线程ID |
| 33 | + foreground_thread = win32api.GetWindowThreadProcessId(foreground_hwnd)[0] |
| 34 | + current_thread = win32api.GetCurrentThreadId() |
| 35 | + |
| 36 | + # 附加到前台窗口的线程 |
| 37 | + if foreground_thread != current_thread: |
| 38 | + ctypes.windll.user32.AttachThreadInput(current_thread, foreground_thread, True) |
| 39 | + |
| 40 | + # 获取焦点控件 |
| 41 | + focus_hwnd = win32gui.GetFocus() |
| 42 | + |
| 43 | + if focus_hwnd: |
| 44 | + focus_class = win32gui.GetClassName(focus_hwnd) |
| 45 | + focus_text = win32gui.GetWindowText(focus_hwnd) |
| 46 | + |
| 47 | + print(f"\n✅ 焦点控件:") |
| 48 | + print(f" 句柄: {focus_hwnd}") |
| 49 | + print(f" 类名: {focus_class}") |
| 50 | + print(f" 文本: {focus_text}") |
| 51 | + |
| 52 | + # 分离线程 |
| 53 | + if foreground_thread != current_thread: |
| 54 | + ctypes.windll.user32.AttachThreadInput(current_thread, foreground_thread, False) |
| 55 | + |
| 56 | + return focus_hwnd, focus_class |
| 57 | + else: |
| 58 | + print("⚠️ 无法获取焦点控件") |
| 59 | + |
| 60 | + # 分离线程 |
| 61 | + if foreground_thread != current_thread: |
| 62 | + ctypes.windll.user32.AttachThreadInput(current_thread, foreground_thread, False) |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + print(f"⚠️ GetFocus 失败: {e}") |
| 66 | + |
| 67 | + # 方法3:获取光标位置下的控件 |
| 68 | + print("\n🔍 尝试通过光标位置获取控件...") |
| 69 | + |
| 70 | + try: |
| 71 | + # 获取光标位置 |
| 72 | + cursor_pos = win32gui.GetCursorPos() |
| 73 | + print(f" 光标位置: {cursor_pos}") |
| 74 | + |
| 75 | + # 获取光标位置的窗口 |
| 76 | + point_hwnd = win32gui.WindowFromPoint(cursor_pos) |
| 77 | + |
| 78 | + if point_hwnd: |
| 79 | + point_class = win32gui.GetClassName(point_hwnd) |
| 80 | + point_text = win32gui.GetWindowText(point_hwnd) |
| 81 | + |
| 82 | + print(f"\n✅ 光标位置的控件:") |
| 83 | + print(f" 句柄: {point_hwnd}") |
| 84 | + print(f" 类名: {point_class}") |
| 85 | + print(f" 文本: {point_text}") |
| 86 | + |
| 87 | + return point_hwnd, point_class |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + print(f"⚠️ WindowFromPoint 失败: {e}") |
| 91 | + |
| 92 | + # 方法4:枚举前台窗口的所有子控件,找到可输入的 |
| 93 | + print("\n🔍 枚举前台窗口的所有输入控件...") |
| 94 | + |
| 95 | + def find_editable_controls(hwnd, results): |
| 96 | + try: |
| 97 | + class_name = win32gui.GetClassName(hwnd) |
| 98 | + |
| 99 | + # 常见的可输入控件类名 |
| 100 | + editable_classes = ['Edit', 'RichEdit', 'RICHEDIT', 'Chrome', 'Text'] |
| 101 | + |
| 102 | + if any(cls in class_name for cls in editable_classes): |
| 103 | + if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): |
| 104 | + text = win32gui.GetWindowText(hwnd) |
| 105 | + results.append((hwnd, class_name, text)) |
| 106 | + except: |
| 107 | + pass |
| 108 | + return True |
| 109 | + |
| 110 | + editable_controls = [] |
| 111 | + win32gui.EnumChildWindows(foreground_hwnd, find_editable_controls, editable_controls) |
| 112 | + |
| 113 | + if editable_controls: |
| 114 | + print(f"\n✅ 找到 {len(editable_controls)} 个可输入控件:") |
| 115 | + for i, (hwnd, class_name, text) in enumerate(editable_controls): |
| 116 | + print(f" [{i}] 句柄: {hwnd:10d} 类名: {class_name:30s} 文本: {text[:50]}") |
| 117 | + |
| 118 | + # 返回第一个 |
| 119 | + return editable_controls[0][0], editable_controls[0][1] |
| 120 | + |
| 121 | + return None, None |
| 122 | + |
| 123 | + |
| 124 | +def test_input_to_handle(hwnd, text="测试输入"): |
| 125 | + """测试向句柄输入文本""" |
| 126 | + |
| 127 | + if not hwnd: |
| 128 | + print("\n❌ 没有有效的句柄") |
| 129 | + return False |
| 130 | + |
| 131 | + print(f"\n🧪 测试输入到句柄 {hwnd}...") |
| 132 | + print(f" 文本: {text}") |
| 133 | + |
| 134 | + try: |
| 135 | + # 获取父窗口 |
| 136 | + parent_hwnd = win32gui.GetParent(hwnd) |
| 137 | + if parent_hwnd: |
| 138 | + # 激活父窗口 |
| 139 | + win32gui.SetForegroundWindow(parent_hwnd) |
| 140 | + time.sleep(0.2) |
| 141 | + |
| 142 | + # 方法1:WM_SETTEXT |
| 143 | + win32api.SendMessage(hwnd, 0x000C, 0, text) # WM_SETTEXT = 0x000C |
| 144 | + time.sleep(0.2) |
| 145 | + |
| 146 | + # 验证 |
| 147 | + length = win32gui.SendMessage(hwnd, 0x000E, 0, 0) # WM_GETTEXTLENGTH = 0x000E |
| 148 | + |
| 149 | + if length > 0: |
| 150 | + print(f"✅ 成功!文本长度: {length}") |
| 151 | + |
| 152 | + # 发送回车 |
| 153 | + print(" 发送回车...") |
| 154 | + win32api.SendMessage(hwnd, 0x0100, 0x0D, 0) # WM_KEYDOWN, VK_RETURN |
| 155 | + time.sleep(0.05) |
| 156 | + win32api.SendMessage(hwnd, 0x0101, 0x0D, 0) # WM_KEYUP, VK_RETURN |
| 157 | + print("✅ 已按回车") |
| 158 | + |
| 159 | + return True |
| 160 | + else: |
| 161 | + print("⚠️ 输入可能失败") |
| 162 | + return False |
| 163 | + |
| 164 | + except Exception as e: |
| 165 | + print(f"❌ 错误: {e}") |
| 166 | + return False |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + import sys |
| 171 | + |
| 172 | + print("=" * 60) |
| 173 | + print("获取焦点控件句柄") |
| 174 | + print("=" * 60) |
| 175 | + print("\n💡 使用方法:") |
| 176 | + print(" 1. 运行此脚本") |
| 177 | + print(" 2. 在3秒内点击目标输入框") |
| 178 | + print(" 3. 脚本会显示该输入框的句柄") |
| 179 | + print() |
| 180 | + |
| 181 | + # 获取焦点控件 |
| 182 | + hwnd, class_name = get_focused_control() |
| 183 | + |
| 184 | + if hwnd: |
| 185 | + print("\n" + "=" * 60) |
| 186 | + print("📋 结果:") |
| 187 | + print("=" * 60) |
| 188 | + print(f"句柄: {hwnd}") |
| 189 | + print(f"类名: {class_name}") |
| 190 | + print("\n💡 使用此句柄输入文本:") |
| 191 | + print(f" python input_by_handle.py {hwnd} 你的文本") |
| 192 | + print("=" * 60) |
| 193 | + |
| 194 | + # 询问是否测试 |
| 195 | + if len(sys.argv) > 1 and sys.argv[1] == "test": |
| 196 | + test_text = " ".join(sys.argv[2:]) if len(sys.argv) > 2 else "测试" |
| 197 | + print(f"\n🧪 测试模式: 将输入 '{test_text}'") |
| 198 | + time.sleep(1) |
| 199 | + test_input_to_handle(hwnd, test_text) |
| 200 | + else: |
| 201 | + print("\n❌ 未能获取焦点控件") |
| 202 | + print("💡 提示:") |
| 203 | + print(" - 确保在3秒内点击了输入框") |
| 204 | + print(" - 确保输入框是可见和可用的") |
| 205 | + print(" - 尝试重新运行脚本") |
0 commit comments