import ctypes, sys
from ctypes import wintypes
import win32gui

sys.stdout.reconfigure(encoding='utf-8')
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk: user32.SetThreadDesktop(hdesk)

windows = []
def cb(h, _):
    if user32.IsWindowVisible(h):
        txt_len = user32.GetWindowTextLengthW(h) + 1
        buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, buf, txt_len)
        rect = win32gui.GetWindowRect(h)
        if rect[2] > rect[0] and rect[3] > rect[1]:
            pid = wintypes.DWORD()
            user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
            windows.append((h, pid.value, buf.value, rect))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)

for h, pid, title, rect in windows:
    if title:
        print(f'HWND: {hex(h)}, PID: {pid}, Rect: {rect}, Title: "{title}"')
