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