使用 Python Windows 扩展获取窗口 Z 顺序
有没有办法使用 Python Windows 扩展 获取窗口的 z 顺序?或者,有没有办法使用另一个模块来做到这一点?通常的方法是使用 GetTopWindow< /code>
和
GetNextWindow
,但这两个函数都没有出现在 win32gui
模块中。
目前我正在这样做,但它没有考虑窗口的 z 顺序:
import win32gui
def get_windows():
def callback(hwnd, lst):
lst.append(hwnd)
lst = []
win32gui.EnumWindows(callback, lst)
return lst
理想情况下,我想要这样的东西:(这不起作用)
import win32gui
import win32con
def get_windows():
'''Returns windows in z-order (top first)'''
lst = []
top = win32gui.GetTopWindow()
if top is None: return lst
lst.append(top)
while True:
next = win32gui.GetNextWindow(lst[-1], win32con.GW_HWNDNEXT)
if next is None: break
lst.append(next)
return lst
但是, GetTopWindow
和GetNextWindow
函数丢失,所以我不能。
更新:
我想我寻求帮助有点太快了。我用 ctypes 解决了这个问题。希望其他人觉得这有帮助。
import win32con
import ctypes
def get_windows():
'''Returns windows in z-order (top first)'''
user32 = ctypes.windll.user32
lst = []
top = user32.GetTopWindow(None)
if not top:
return lst
lst.append(top)
while True:
next = user32.GetWindow(lst[-1], win32con.GW_HWNDNEXT)
if not next:
break
lst.append(next)
return lst
Is there a way to get the z-order of windows using the Python Windows Extensions? Or, alternatively, is there a way to do this using another module? The usual way to do this is with GetTopWindow
and GetNextWindow
, but neither of those functions appear in the win32gui
module.
Currently I'm doing this, but it doesn't take into account the z-order of windows:
import win32gui
def get_windows():
def callback(hwnd, lst):
lst.append(hwnd)
lst = []
win32gui.EnumWindows(callback, lst)
return lst
Ideally I'd like something like this: (this doesn't work)
import win32gui
import win32con
def get_windows():
'''Returns windows in z-order (top first)'''
lst = []
top = win32gui.GetTopWindow()
if top is None: return lst
lst.append(top)
while True:
next = win32gui.GetNextWindow(lst[-1], win32con.GW_HWNDNEXT)
if next is None: break
lst.append(next)
return lst
However, the GetTopWindow
and GetNextWindow
functions are missing, so I can't.
UPDATE:
I guess I was a little too quick to ask for help. I figured it out using ctypes. Hopefully someone else finds this helpful.
import win32con
import ctypes
def get_windows():
'''Returns windows in z-order (top first)'''
user32 = ctypes.windll.user32
lst = []
top = user32.GetTopWindow(None)
if not top:
return lst
lst.append(top)
while True:
next = user32.GetWindow(lst[-1], win32con.GW_HWNDNEXT)
if not next:
break
lst.append(next)
return lst
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尸检。
PyWin32 当前URL:[GitHub]:mhammond/ pywin32 - pywin32。
关于CTypes替代方案:
应该发布在答案而不是问题中
包含一个常见缺陷,可能会带来灾难性的后果。检查[SO]:通过 ctypes 从 Python 调用的 C 函数返回不正确的值(@CristiFati 的答案) 了解详细信息
信息em>PyWin32 实现,考虑到它不包含错误(我没有检查):
[MS.Docs]:GetNextWindow 宏 (winuser.h )(如 URL 文本所述),是 #define 到 GetWindow,而不是函数, 事实这是在提问时已知的(因为 CTypes 实现正在使用后者),因此可以使用 win32gui.GetWindow (至少在最新的 PyWin32版本)
我刚刚提交了[GitHub]: mhammond/pywin32 - win32gui 中的更多窗口函数(在 220822 上合并到 main)。检查[SO]:如何使用 python & 更改打印队列中作业的用户名win32print(@CristiFati 的回答) 了解有关如何从此类补丁中受益的详细信息。
本地构建示例:
<块引用>
Necroposting.
PyWin32's current URL: [GitHub]: mhammond/pywin32 - pywin32.
Regarding the CTypes alternative:
Should probably be posted in an answer rather than the question
Contains a common flaw with possibly disastrous consequences. Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for the details
Regarding the PyWin32 implementation, considering it doesn't contain bugs (I didn't check):
[MS.Docs]: GetNextWindow macro (winuser.h) (as the URL text states), is a #define to GetWindow not a function, fact that was known at question time (as CTypes implementation is using the latter), so win32gui.GetWindow can be used (at least in latest PyWin32 versions)
I've just submitted [GitHub]: mhammond/pywin32 - More window functions in win32gui (merged to main on 220822). Check [SO]: How to change username of job in print queue using python & win32print (@CristiFati's answer) for details on how to benefit from such a patch.
Local build example: