【发布时间】:2013-01-17 04:06:05
【问题描述】:
我正在开发一个 python 应用程序,我想获取每个打开的窗口的HWND。我需要窗口的名称和HWND 来过滤列表以管理一些特定的窗口,移动它们并调整它们的大小。
我尝试自己做这件事,四处寻找信息,但我没有得到正确的代码。我试过这个code,但我只得到每个窗口的标题(这很好),但我也需要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
【问题讨论】:
-
SO 不是一个超市,你可以带着购物清单去,让其他人为你工作。你自己做了什么?
-
@Difusio:我尝试自己做这件事,四处寻找信息,但我没有得到正确的代码。我试过这个sjohannes.wordpress.com/2012/03/23/…,但我只得到每个窗口的标题(很好),但我也需要 HWND。
-
我的意思是,请将您编写的 代码 连同 您收到的错误或您所做的其他事情。
-
请不要编辑您的问题以包含答案中的修复,因为这会使您的问题对后来出现并想要理解它的任何人都无用。