Python 获取每个窗口的 HWND?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14653168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get HWND of each Window?
提问by user1618465
I am developing a python application and I want to get the HWNDof each open windows. I need the name of the windows and the HWNDto filter the list to manage some specifics windows, moving and resizing them.
我正在开发一个 python 应用程序,我想获取HWND每个打开的窗口。我需要窗口的名称和HWND过滤列表来管理一些特定的窗口,移动和调整它们的大小。
I have tried to do it myself looking information around but I did not get the correct piece of code. I tried with this codebut I only get the title of each windows (that is great), but I need the HWNDtoo.
我曾尝试自己查找信息,但没有得到正确的代码。我试过这段代码,但我只得到了每个窗口的标题(很好),但我也需要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)
There is a error here:
这里有一个错误:
win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
TypeError: The object is not a PyHANDLE object
采纳答案by nymk
You mixed up ctypesand win32gui.
The hwndyou've got is obtained via ctypesand is a LP_c_longobject. That's why win32gui.MoveWindowdidn't accept it. You should pass it to
你混淆了ctypes和win32gui。
在hwnd你所得到的是通过获得ctypes,是一个LP_c_long对象。这就是为什么win32gui.MoveWindow没有接受它。你应该把它传递给
ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)
If you want to use win32gui.MoveWindow, you can use python function as callback directly.
For example,
如果你想使用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)
回答by Script and Compile
To get the handles of all available main windows you pass 0 to win32gui.EnumChildWindowsthen check to make sure the window has text of length longer than 0 (since you want only actual windows not hidden/temporary/popup/special windows).
要获取所有可用主窗口的句柄,您传递 0 到win32gui.EnumChildWindows然后检查以确保窗口具有长度大于 0 的文本(因为您只想要实际窗口而不是隐藏/临时/弹出/特殊窗口)。
回答by martineau
回答by abarnert
Your problem (now that martineau has fixed your original problem of not storing the HWNDvalues at all) is that you're trying to mix ctypesand win32gui.
您的问题(现在 martineau 已经解决了您根本不存储HWND值的原始问题)是您试图将ctypes和win32gui.
You can do that if you know what you're doing—but if not, just don't do it.
如果您知道自己在做什么,就可以这样做——但如果不知道,就不要这样做。
If you want to get window handles you can use with win32gui, use win32gui.EnumWindowsinstead of calling the raw function out of the user32DLL.
如果你想获得窗口句柄,你可以使用 with win32gui, usewin32gui.EnumWindows而不是从user32DLL中调用原始函数。

