-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_window_content.py
More file actions
416 lines (310 loc) · 12.2 KB
/
get_window_content.py
File metadata and controls
416 lines (310 loc) · 12.2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
通过句柄获取窗口内容
支持多种方法:
1. Win32 API - 获取窗口文本
2. UI Automation - 获取可访问性树
3. 截图 OCR - 识别屏幕文字
4. 内存读取 - 读取进程内存(高级)
"""
import win32gui
import win32api
import win32con
import win32process
import time
import json
def get_window_text_by_handle(hwnd):
"""方法1: 使用 Win32 API 获取窗口文本"""
print("\n" + "=" * 60)
print("方法1: Win32 API 获取窗口文本")
print("=" * 60)
try:
# 获取窗口标题
title = win32gui.GetWindowText(hwnd)
print(f"窗口标题: {title}")
# 获取类名
class_name = win32gui.GetClassName(hwnd)
print(f"窗口类名: {class_name}")
# 获取窗口矩形
rect = win32gui.GetWindowRect(hwnd)
print(f"窗口位置: {rect}")
# 遍历所有子窗口
print("\n子窗口内容:")
def enum_child(child_hwnd, results):
try:
child_text = win32gui.GetWindowText(child_hwnd)
child_class = win32gui.GetClassName(child_hwnd)
if child_text or child_class in ['Edit', 'RichEdit', 'RICHEDIT50W']:
results.append({
'hwnd': child_hwnd,
'class': child_class,
'text': child_text
})
if child_text:
print(f" [{child_hwnd}] {child_class}: {child_text[:100]}")
except:
pass
return True
results = []
win32gui.EnumChildWindows(hwnd, enum_child, results)
if not results:
print(" (未找到包含文本的子窗口)")
return results
except Exception as e:
print(f"❌ 错误: {e}")
return []
def get_window_content_uiautomation(hwnd):
"""方法2: 使用 UI Automation 获取内容"""
print("\n" + "=" * 60)
print("方法2: UI Automation 获取内容")
print("=" * 60)
try:
from comtypes.client import CreateObject, GetModule
# 加载 UI Automation
GetModule('UIAutomationCore.dll')
import comtypes.gen.UIAutomationClient as UIA
automation = CreateObject(
'{ff48dba4-60ef-4201-aa87-54103eef594e}',
interface=UIA.IUIAutomation
)
# 从句柄获取元素
element = automation.ElementFromHandle(hwnd)
if not element:
print("❌ 无法获取 UI Automation 元素")
return []
print(f"✅ 根元素:")
print(f" 名称: {element.CurrentName}")
print(f" 类名: {element.CurrentClassName}")
print(f" 控件类型: {element.CurrentControlType}")
# 查找所有文本元素
print("\n📋 文本元素:")
# 查找 Text 控件
text_condition = automation.CreatePropertyCondition(
UIA.UIA_ControlTypePropertyId,
UIA.UIA_TextControlTypeId
)
text_elements = element.FindAll(UIA.TreeScope_Descendants, text_condition)
results = []
for i in range(min(text_elements.Length, 50)): # 限制数量
try:
text_elem = text_elements.GetElement(i)
name = text_elem.CurrentName
if name and len(name.strip()) > 0:
results.append({
'type': 'Text',
'name': name,
'class': text_elem.CurrentClassName
})
print(f" [{i}] {name[:100]}")
except:
pass
# 查找 Edit 控件
print("\n📝 输入框内容:")
edit_condition = automation.CreatePropertyCondition(
UIA.UIA_ControlTypePropertyId,
UIA.UIA_EditControlTypeId
)
edit_elements = element.FindAll(UIA.TreeScope_Descendants, edit_condition)
for i in range(min(edit_elements.Length, 20)):
try:
edit_elem = edit_elements.GetElement(i)
name = edit_elem.CurrentName
# 尝试获取值
try:
value_pattern = edit_elem.GetCurrentPattern(UIA.UIA_ValuePatternId)
value = value_pattern.CurrentValue
if value:
results.append({
'type': 'Edit',
'name': name,
'value': value,
'class': edit_elem.CurrentClassName
})
print(f" [{i}] {name}: {value[:100]}")
except:
if name:
print(f" [{i}] {name}")
except:
pass
# 查找 Document 控件(可能包含富文本)
print("\n📄 文档内容:")
doc_condition = automation.CreatePropertyCondition(
UIA.UIA_ControlTypePropertyId,
UIA.UIA_DocumentControlTypeId
)
doc_elements = element.FindAll(UIA.TreeScope_Descendants, doc_condition)
for i in range(min(doc_elements.Length, 10)):
try:
doc_elem = doc_elements.GetElement(i)
name = doc_elem.CurrentName
# 尝试获取文本模式
try:
text_pattern = doc_elem.GetCurrentPattern(UIA.UIA_TextPatternId)
text_range = text_pattern.DocumentRange
text = text_range.GetText(-1) # -1 表示获取所有文本
if text:
results.append({
'type': 'Document',
'name': name,
'text': text,
'class': doc_elem.CurrentClassName
})
print(f" [{i}] {name}:")
print(f" {text[:200]}")
except:
if name:
print(f" [{i}] {name}")
except:
pass
if not results:
print(" (未找到内容)")
return results
except ImportError:
print("❌ 需要安装 comtypes 库")
print(" 运行: pip install comtypes")
return []
except Exception as e:
print(f"❌ 错误: {e}")
import traceback
traceback.print_exc()
return []
def get_window_content_screenshot(hwnd):
"""方法3: 截图并 OCR 识别"""
print("\n" + "=" * 60)
print("方法3: 截图 OCR 识别")
print("=" * 60)
try:
from PIL import ImageGrab
import pytesseract
# 获取窗口位置
rect = win32gui.GetWindowRect(hwnd)
print(f"窗口位置: {rect}")
print("📸 截图中...")
# 截图
screenshot = ImageGrab.grab(bbox=rect)
screenshot.save('window_screenshot.png')
print("✅ 截图已保存: window_screenshot.png")
print("🔍 OCR 识别中...")
# OCR 识别
text = pytesseract.image_to_string(screenshot, lang='chi_sim+eng')
print("\n识别结果:")
print("-" * 60)
print(text)
print("-" * 60)
return [{'type': 'OCR', 'text': text}]
except ImportError as e:
print(f"❌ 需要安装依赖库:")
print(" pip install pillow pytesseract")
print(" 并安装 Tesseract OCR: https://github.com/tesseract-ocr/tesseract")
return []
except Exception as e:
print(f"❌ 错误: {e}")
return []
def get_process_memory_strings(hwnd):
"""方法4: 读取进程内存中的字符串(高级)"""
print("\n" + "=" * 60)
print("方法4: 读取进程内存")
print("=" * 60)
try:
import ctypes
from ctypes import wintypes
# 获取进程 ID
_, pid = win32process.GetWindowThreadProcessId(hwnd)
print(f"进程 ID: {pid}")
# 打开进程
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_READ = 0x0010
process_handle = ctypes.windll.kernel32.OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
False,
pid
)
if not process_handle:
print("❌ 无法打开进程(可能需要管理员权限)")
return []
print("✅ 进程已打开")
print("⚠️ 注意:读取进程内存需要管理员权限,且可能不稳定")
# 这里需要更复杂的内存扫描逻辑
# 简化版本:只显示提示
print("\n💡 完整的内存读取需要:")
print(" 1. 管理员权限")
print(" 2. 了解目标进程的内存布局")
print(" 3. 使用专门的内存扫描工具")
ctypes.windll.kernel32.CloseHandle(process_handle)
return []
except Exception as e:
print(f"❌ 错误: {e}")
return []
def get_all_window_content(hwnd):
"""综合使用所有方法获取窗口内容"""
print("=" * 60)
print("获取窗口内容")
print("=" * 60)
# 获取窗口信息
try:
title = win32gui.GetWindowText(hwnd)
class_name = win32gui.GetClassName(hwnd)
print(f"\n目标窗口:")
print(f" 句柄: {hwnd}")
print(f" 标题: {title}")
print(f" 类名: {class_name}")
except:
print(f"❌ 无效的窗口句柄: {hwnd}")
return
all_results = {}
# 方法1: Win32 API
results1 = get_window_text_by_handle(hwnd)
all_results['win32'] = results1
# 方法2: UI Automation
results2 = get_window_content_uiautomation(hwnd)
all_results['uiautomation'] = results2
# 方法3: 截图 OCR(可选)
# results3 = get_window_content_screenshot(hwnd)
# all_results['ocr'] = results3
# 方法4: 内存读取(高级,可选)
# results4 = get_process_memory_strings(hwnd)
# all_results['memory'] = results4
# 保存结果
print("\n" + "=" * 60)
print("保存结果")
print("=" * 60)
output_file = f'window_content_{hwnd}.json'
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(all_results, f, indent=2, ensure_ascii=False)
print(f"✅ 结果已保存到: {output_file}")
# 统计
total = sum(len(v) if isinstance(v, list) else 0 for v in all_results.values())
print(f"\n📊 共获取 {total} 条内容")
return all_results
def find_kiro_and_get_content():
"""查找 Kiro 窗口并获取内容"""
print("🔍 查找 Kiro 窗口...")
def find_window(hwnd, results):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if "Kiro" in title:
results.append((hwnd, title))
return True
results = []
win32gui.EnumWindows(find_window, results)
if not results:
print("❌ 未找到 Kiro 窗口")
return
hwnd, title = results[0]
print(f"✅ 找到窗口: {title}")
get_all_window_content(hwnd)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
# 使用指定的句柄
hwnd = int(sys.argv[1])
get_all_window_content(hwnd)
else:
# 等待3秒后获取当前窗口
print("⏰ 3 秒后获取当前前台窗口的内容...")
print("💡 请切换到目标窗口")
time.sleep(3)
hwnd = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(hwnd)
print(f"\n当前窗口: {title}")
print(f"句柄: {hwnd}")
get_all_window_content(hwnd)