Get Window Z-Order with Python Windows Extensions
Asked Answered
I

1

7

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
Itinerate answered 17/6, 2011 at 4:2 Comment(0)
L
0

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
    
Lab answered 15/8, 2022 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.