我正在开发一个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)
这里有一个错误:
win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
TypeError: The object is not a PyHANDLE object