如何获得每个窗口的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)
这里有个错误:
win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
TypeError: The object is not a PyHANDLE object
4 个回答
2
你的问题是,现在martineau已经解决了你最初没有存储HWND
值的问题,但你现在在尝试混合使用ctypes
和win32gui
。
如果你知道自己在做什么,这样做是可以的——但如果不清楚,就别这么做。
如果你想获取可以与win32gui
一起使用的窗口句柄,可以使用win32gui.EnumWindows
,而不是直接调用user32
DLL里的原始函数。
2
要获取所有可用的主窗口的句柄,你需要把0传给win32gui.EnumChildWindows
这个函数。然后,你要检查一下这个窗口的标题是否有文字,并且长度大于0(因为你只想要真正的窗口,而不是那些隐藏的、临时的、弹出的或者特殊的窗口)。
34
你把 ctypes
和 win32gui
搞混了。
你得到的 hwnd
是通过 ctypes
获取的,它是一个 LP_c_long
对象。这就是为什么 win32gui.MoveWindow
不接受它的原因。你应该把它传给
ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)
如果你想使用 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)