xtn-tools-pro 1.0.1.3.2__py3-none-any.whl → 1.0.1.3.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # 说明:
5
+ # win 操作
6
+ # History:
7
+ # Date Author Version Modification
8
+ # --------------------------------------------------------------------------------------------------
9
+ # 2025/8/11 xiatn V00.01.000 新建
10
+ # --------------------------------------------------------------------------------------------------
11
+ import ctypes
12
+ from ctypes import wintypes
13
+
14
+ user32 = ctypes.WinDLL("user32", use_last_error=True)
15
+
16
+ # 回调类型:BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
17
+ EnumWindowsProc = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
18
+
19
+ # --- 明确声明所有用到的 WinAPI 的参数/返回类型 ---
20
+ user32.EnumWindows.argtypes = [EnumWindowsProc, wintypes.LPARAM]
21
+ user32.EnumWindows.restype = wintypes.BOOL
22
+
23
+ user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]
24
+ user32.GetWindowThreadProcessId.restype = wintypes.DWORD
25
+
26
+ user32.IsWindowVisible.argtypes = [wintypes.HWND]
27
+ user32.IsWindowVisible.restype = wintypes.BOOL
28
+
29
+ user32.GetWindowTextLengthW.argtypes = [wintypes.HWND]
30
+ user32.GetWindowTextLengthW.restype = ctypes.c_int # 长度(TCHAR 数),可能为 0
31
+
32
+ user32.GetWindowTextW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]
33
+ user32.GetWindowTextW.restype = ctypes.c_int # 实际拷贝的字符数
34
+
35
+
36
+ def enum_windows(only_visible=True):
37
+ """
38
+ 遍历顶层窗口,返回 [{hwnd, pid, title}, ...]
39
+ - only_visible=True 只返回可见窗口
40
+ """
41
+ results = []
42
+
43
+ @EnumWindowsProc
44
+ def callback(hwnd, lparam):
45
+ try:
46
+ # 可选过滤
47
+ if only_visible and not user32.IsWindowVisible(hwnd):
48
+ return True # 继续枚举
49
+
50
+ # 取 PID
51
+ pid = wintypes.DWORD(0)
52
+ user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
53
+
54
+ # 取标题
55
+ length = user32.GetWindowTextLengthW(hwnd)
56
+ title = ""
57
+ if length > 0:
58
+ buf = ctypes.create_unicode_buffer(length + 1)
59
+ copied = user32.GetWindowTextW(hwnd, buf, length + 1)
60
+ if copied > 0:
61
+ title = buf.value
62
+
63
+ # 结果里再把 HWND 规范化为无符号整数,避免负数显示
64
+ results.append({
65
+ "hwnd": ctypes.c_size_t(hwnd).value,
66
+ "pid": pid.value,
67
+ "title": title
68
+ })
69
+ except Exception:
70
+ # 回调里不要把异常抛给 WinAPI,否则会出现 “Exception ignored on calling ctypes callback function”
71
+ pass
72
+ return True # 继续
73
+
74
+ user32.EnumWindows(callback, 0)
75
+ return results
76
+
77
+
78
+ if __name__ == "__main__":
79
+ pass
80
+ # 枚举win窗口获取句柄和pid
81
+ # for win in enum_windows(only_visible=True):
82
+ # print(win)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.1.3.2
3
+ Version: 1.0.1.3.3
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -64,9 +64,10 @@ xtn_tools_pro/utils/set_data.py,sha256=UR5S8xBqc4Vk8zpbrNVsxKhwaBAenaEdC7O-N3s7h
64
64
  xtn_tools_pro/utils/shell.py,sha256=s6sJedxLLQokcI1hF5YSD3qgGUPK0brNcP-D3-yv54I,3790
65
65
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
66
66
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
67
- xtn_tools_pro-1.0.1.3.2.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
- xtn_tools_pro-1.0.1.3.2.dist-info/METADATA,sha256=E9GDeG_G6ggOAy8PK0XyZn0qkf2mATpdaXJnR46C38I,574
69
- xtn_tools_pro-1.0.1.3.2.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
70
- xtn_tools_pro-1.0.1.3.2.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
71
- xtn_tools_pro-1.0.1.3.2.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
72
- xtn_tools_pro-1.0.1.3.2.dist-info/RECORD,,
67
+ xtn_tools_pro/utils/win.py,sha256=f8E-KP2a11h6WYl6byaW64x1OoEEK1jcwY796f2p63g,2943
68
+ xtn_tools_pro-1.0.1.3.3.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
69
+ xtn_tools_pro-1.0.1.3.3.dist-info/METADATA,sha256=HzCSqPmpvhmR7uPcnXD6VdxL03INdVyFX3nlfxvaqj8,574
70
+ xtn_tools_pro-1.0.1.3.3.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
71
+ xtn_tools_pro-1.0.1.3.3.dist-info/entry_points.txt,sha256=t8CtXWOgw7nRDW3XNlZh8MT_P4l8EzsFnyWi5b3ovrc,68
72
+ xtn_tools_pro-1.0.1.3.3.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
73
+ xtn_tools_pro-1.0.1.3.3.dist-info/RECORD,,