use*_*465 7 python windows hwnd
我正在开发一个python应用程序,我想获得HWND每个打开的窗口.我需要窗口的名称和HWND过滤列表来管理一些特定的窗口,移动和调整它们的大小.
我试图自己查看信息,但我没有得到正确的代码.我试过这个代码,但我只得到每个窗口的标题(这很棒),但我也需要它HWND.
import ctypes
import win32gui
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append((hwnd, buff.value))
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
for i in range(len(titles)):
print(titles)[i]
win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
Run Code Online (Sandbox Code Playgroud)
这里有一个错误:
win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
TypeError: The object is not a PyHANDLE object
Run Code Online (Sandbox Code Playgroud)
nym*_*ymk 23
你混了ctypes和win32gui.
在hwnd你有经获得ctypes,是一个LP_c_long对象.这就是为什么win32gui.MoveWindow不接受它.你应该把它传递给
ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)
Run Code Online (Sandbox Code Playgroud)
如果要使用win32gui.MoveWindow,可以直接使用python函数作为回调.
例如,
import win32gui
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
win32gui.MoveWindow(hwnd, 0, 0, 760, 500, True)
win32gui.EnumWindows(enumHandler, None)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30885 次 |
| 最近记录: |