Python win32api get "stack" of windows
Question: I'm looking for a way to find out what order windows are open on my desktop in
order to tell what parts of what windows are visible to the user.

Say, in order, I open up a maximized chrome window, a maximized notepad++
window, and then a command prompt that only covers a small portion of the
screen. Is there a way using the win32api (or possibly other library) that can
tell me the stack of windows open so I can take the window dimensions and find
out what is visible? I already know how to get which window has focus and the
top-level window, but I'm looking for more info than that.

In the example I mentioned above, I'd return that the full command prompt is
visible but in the places it isn't, the notepad++ window is visible for
example. No part of the chrome window would be visible.


Answer: This does not yet have any logic deciding if windows are overlaid but it does
return a dictionary of existing windows with info of their title, visibility,
minimization, size and the next window handle.

    
    
    import win32gui
    import win32con
    
    
    def enum_handler(hwnd, results):
        results[hwnd] = {
            "title":win32gui.GetWindowText(hwnd),
            "visible":win32gui.IsWindowVisible(hwnd),
            "minimized":win32gui.IsIconic(hwnd),
            "rectangle":win32gui.GetWindowRect(hwnd), #(left, top, right, bottom)
            "next":win32gui.GetWindow(hwnd, win32con.GW_HWNDNEXT) # Window handle to below window
        }
    
    
    def get_windows():
        enumerated_windows = {}
        win32gui.EnumWindows(enum_handler, enumerated_windows)
        return enumerated_windows
    
    
    if __name__ == "__main__":
        windows = get_windows()
    
        for window_handle in windows:
            if windows[window_handle]["title"] is not "":
                print "{}, {}, {}, {}".format(windows[window_handle]["minimized"],
                                              windows[window_handle]["rectangle"],
                                              windows[window_handle]["next"],
                                              windows[window_handle]["title"])
    

Microsoft MSDN has good artice on zorder info with GetWindow() and GW_HWNDNEXT
<https://msdn.microsoft.com/en-
us/library/windows/desktop/ms633515(v=vs.85).aspx>

