课前准备
win32
Windows使用dwm管理窗口,可以使用user32.dll
来做嵌入窗口
涉及到
user32.EnumWindows
遍历窗口
user32.IsWindow
user32.IsWindowVisible
user32.IsWindowEnabled
筛选窗口
user32.SetParent
设置父窗口
python
可以用ctypes
调用系统API
ctypes.windll
Win系统独有,调用系统dll(即API)
如ctypes.windll.user32
ctypes.windll.kernel32
为什么不用win32gui?
太大了。好像whl都有40MB,如果只需要这么一个常用功能,那么可以单独写一个API
正文
导入
import ctypes
u32 = ctypes.windll.user32 #获取dll
获取所有窗口
_all_hwnds=[] #未处理的
_all_handles=[]#已处理的
TITLE_MAX=25 #最长标题字符数
@ctypes.CFUNCTYPE(None,ctypes.c_int)
def enum(hwnd):
"C函数"
_all_hwnds.append(int(hwnd))
def update_hwnd():
global _all_hwnds,_all_handles
_all_hwnds=[]
_all_handles=[]
u32.EnumWindows(enum) #更新,传入C函数
for hwnd in _all_hwnds:#已更新
if u32.IsWindow(hwnd) and u32.IsWindowVisible(hwnd) and u32.IsWindowEnabled(hwnd):#筛选出合适的窗口
titlelist = (ctypes.c_char*(TITLE_MAX+1))()#C字节串,中文是GBK编码
u32.GetWindowTextA(hwnd,