简体   繁体   中英

Python win32api get “stack” of windows

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.

import win32gui
import win32con

def get_windows():
    def sort_windows(windows):
        sorted_windows = []

        # Find the first entry
        for window in windows:
            if window["hwnd_above"] == 0:
                sorted_windows.append(window)
                break
        else:
            raise(IndexError("Could not find first entry"))

        # Follow the trail
        while True:
            for window in windows:
                if sorted_windows[-1]["hwnd"] == window["hwnd_above"]:
                    sorted_windows.append(window)
                    break
            else:
                break

        # Remove hwnd_above
        for window in windows:
            del(window["hwnd_above"])

        return sorted_windows

    def enum_handler(hwnd, results):
        window_placement = win32gui.GetWindowPlacement(hwnd)
        results.append({
            "hwnd":hwnd,
            "hwnd_above":win32gui.GetWindow(hwnd, win32con.GW_HWNDPREV), # Window handle to above window
            "title":win32gui.GetWindowText(hwnd),
            "visible":win32gui.IsWindowVisible(hwnd) == 1,
            "minimized":window_placement[1] == win32con.SW_SHOWMINIMIZED,
            "maximized":window_placement[1] == win32con.SW_SHOWMAXIMIZED,
            "rectangle":win32gui.GetWindowRect(hwnd) #(left, top, right, bottom)
        })

    enumerated_windows = []
    win32gui.EnumWindows(enum_handler, enumerated_windows)
    return sort_windows(enumerated_windows)

if __name__ == "__main__":
    windows = get_windows()

    for window in windows:
        print(window)
    print()

    # Pretty print
    for window in windows:
        if window["title"] == "" or not window["visible"]:
            continue
        print(window)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM