Skip to content

Commit 3f015a2

Browse files
committed
Add win32 support for listing windows
1 parent 79b144f commit 3f015a2

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
if sys.platform.startswith("win"):
4+
from .windows_list_win import WindowsList
5+
elif sys.platform.startswith("linux"):
6+
from .windows_list_x11 import WindowsList
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import ctypes
2+
from ctypes import wintypes
3+
4+
user32 = ctypes.windll.user32
5+
6+
7+
class WindowsList:
8+
def __init__(self):
9+
self.windows = {}
10+
11+
def __call__(self) -> dict[int, tuple[str, bool]]:
12+
WNDENUMPROC = ctypes.WINFUNCTYPE(
13+
wintypes.BOOL,
14+
wintypes.HWND,
15+
wintypes.LPARAM
16+
)
17+
cb_worker = WNDENUMPROC(self._enum_windows_cb)
18+
user32.EnumWindows(cb_worker, None)
19+
return self.windows
20+
21+
def _enum_windows_cb(self, hwnd: wintypes.HWND, _: wintypes.LPARAM):
22+
"""Callback for EnumWindows.
23+
24+
See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633498(v=vs.85)
25+
"""
26+
length = user32.GetWindowTextLengthW(hwnd) + 1
27+
buffer = ctypes.create_unicode_buffer(length)
28+
user32.GetWindowTextW(hwnd, buffer, length)
29+
if user32.IsWindowVisible(hwnd) and len(buffer.value) > 0:
30+
self.windows[hwnd] = (buffer.value, True)
31+
return True
File renamed without changes.

0 commit comments

Comments
 (0)