使用 Python Windows 扩展获取窗口 Z 顺序

发布于 2024-11-15 18:18:12 字数 1569 浏览 8 评论 0原文

有没有办法使用 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

但是, GetTopWindowGetNextWindow 函数丢失,所以我不能。

更新:

我想我寻求帮助有点太快了。我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-11-22 18:18:12

尸检。

PyWin32 当前URL[GitHub]:mhammond/ pywin32 - pywin32

关于CTypes替代方案:

信息em>PyWin32 实现,考虑到它不包含错误(我没有检查):

  • [MS.Docs]:GetNextWindow 宏 (winuser.h )(如 URL 文本所述),#defineGetWindow,而不是函数, 事实这是在提问时已知的(因为 CTypes 实现正在使用后者),因此可以使用 win32gui.GetWindow (至少在最新的 PyWin32版本)

  • 我刚刚提交了[GitHub]: mhammond/pywin32 - win32gui 中的更多窗口函数(在 220822 上合并到 main)。检查[SO]:如何使用 python & 更改打印队列中作业的用户名win32print(@CristiFati 的回答) 了解有关如何从此类补丁中受益的详细信息。
    本地构建示例:

    <块引用>

    [cfati@CFATI-5510-0:e:\Work\Dev\GitHub\CristiFati\pywin32\src\build\lib.win-amd64-3.9\win32]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" -c "导入 os;os.add_dll_directory(os.path.join(os.path.dirname(os.getcwd())), ' pywin32_system32'));import win32gui as wgui;print(wgui);print('顶部窗口句柄: 0x{:016X}'.format(wgui.GetTopWindow(无)))"
    <来自'e:\\Work\\Dev\\GitHub\\CristiFati\\pywin32\\src\\build\\lib.win-amd64-3.9\\win32\\win32gui.pyd'的模块'win32gui'> ;
    顶部窗口句柄:0x0000000000020480
    

Necroposting.

PyWin32's current URL: [GitHub]: mhammond/pywin32 - pywin32.

Regarding the CTypes alternative:

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:

    [cfati@CFATI-5510-0:e:\Work\Dev\GitHub\CristiFati\pywin32\src\build\lib.win-amd64-3.9\win32]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" -c "import os;os.add_dll_directory(os.path.join(os.path.dirname(os.getcwd()), 'pywin32_system32'));import win32gui as wgui;print(wgui);print('Top window handle: 0x{:016X}'.format(wgui.GetTopWindow(None)))"
    <module 'win32gui' from 'e:\\Work\\Dev\\GitHub\\CristiFati\\pywin32\\src\\build\\lib.win-amd64-3.9\\win32\\win32gui.pyd'>
    Top window handle: 0x0000000000020480
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文