[英]Python win32api get “stack” of windows
我正在尋找一種方法來找出在我的桌面上打開了哪些訂單窗口,以便告訴用戶哪些窗口可見。
說,按順序,我打開一個最大化的chrome窗口,一個最大化的notepad ++窗口,然后打開一個僅覆蓋屏幕一小部分的命令提示符。 有沒有一種使用win32api(或可能的其他庫)的方法可以告訴我打開的窗口堆棧,以便我可以確定窗口尺寸並找出可見的內容? 我已經知道如何獲取具有焦點的窗口和頂級窗口,但是我正在尋找更多的信息。
在上面提到的示例中,我將返回完整的命令提示符可見,但在不可見的地方,例如,notepad ++窗口可見。 chrome窗口的任何部分都不可見。
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使用GetWindow()和GW_HWNDNEXT https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms633515(v=vs.85).aspx在zorder信息方面有很好的技巧
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.