My goal is to write a script in python that shows how much percent of the desktop is hidden behind all visible windows on top of it, even those not created by the script.
I wrote
import win32api, win32gui
def get_visible_area():
# Get the dimensions of the desktop
desktop_width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
desktop_height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
# Get the list of all open windows
windows = win32gui.GetDesktopWindow()
(child_windows, _) = win32gui.EnumChildWindows(windows)
# Initialize variables to keep track of the visible area
visible_area = desktop_width * desktop_height
total_area = desktop_width * desktop_height
# Iterate through all open windows and subtract their visible area from the total visible area
for window in child_windows:
if win32gui.IsWindowVisible(window):
left, top, right, bottom = win32gui.GetWindowRect(window)
window_width = right - left
window_height = bottom - top
window_area = window_width * window_height
visible_area -= window_area
# Calculate the percentage of the desktop that is hidden behind windows
percentage_hidden = (total_area - visible_area) / total_area * 100
return percentage_hidden
print("Percentage of desktop hidden behind windows:", get_visible_area(), "%")
But it does not take into account that windows overlap each other and it includes all windows somewhere, not only those that cover the desktop and are visible. How to include only those and have a 0-100% value.