-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindows_utils.py
351 lines (295 loc) · 10.9 KB
/
windows_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import abc
import ctypes
import functools
import queue
import threading
import typing
import weakref
import timer # type: ignore
import win32api # type: ignore
import win32con # type: ignore
import win32gui # type: ignore
import win_magnification as mag
# make_partial_screen & make_fullscreen
# Inspired by:
# https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Magnification/cpp/Windowed/MagnifierSample.cpp
def make_partial_screen(hwnd, rectangle: mag.types.Rectangle):
win32gui.SetWindowLong(
hwnd,
win32con.GWL_EXSTYLE,
win32con.WS_EX_TOPMOST |
win32con.WS_EX_LAYERED
)
win32gui.SetWindowLong(
hwnd,
win32con.GWL_STYLE,
win32con.WS_SIZEBOX |
win32con.WS_SYSMENU |
win32con.WS_CLIPCHILDREN |
win32con.WS_CAPTION |
win32con.WS_MAXIMIZEBOX
)
win32gui.SetWindowPos(
hwnd,
win32con.HWND_TOPMOST,
*rectangle,
win32con.SWP_SHOWWINDOW |
win32con.SWP_NOZORDER |
win32con.SWP_NOACTIVATE
)
def get_fullscreen_size() -> mag.types.Rectangle:
# Calculate the span of the display area.
max_x = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
max_y = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
return 0, 0, max_x, max_y
# Alternative
# return win32gui.GetClientRect(win32gui.GetDesktopWindow())
# noinspection SpellCheckingInspection
def make_fullscreen(hwnd, rectangle: mag.types.Rectangle):
# The window must be styled as layered for proper rendering.
# It is styled as transparent so that it does not capture mouse clicks.
# For draw on top of TaskManager/System menus we need to use win10 zBand
# Most relevant is ZBID_UIACCESS, read blog below to know more
# https://blog.adeltax.com/window-z-order-in-windows-10/
win32gui.SetWindowLong(
hwnd, win32con.GWL_EXSTYLE,
win32con.WS_EX_TOPMOST |
win32con.WS_EX_LAYERED |
win32con.WS_EX_TOOLWINDOW |
win32con.WS_EX_TRANSPARENT
)
win32gui.SetWindowLong(
hwnd, win32con.GWL_STYLE,
win32con.WS_CHILD
)
win32gui.SetWindowPos(
hwnd,
win32con.HWND_TOPMOST,
*rectangle,
win32con.SWP_SHOWWINDOW |
win32con.SWP_NOZORDER |
win32con.SWP_NOACTIVATE
)
WinEventHandler = typing.Callable[['AbstractWindow'], None]
class AbstractWindow(metaclass=abc.ABCMeta):
window_class: int = 0
window_class_name = "Py_MyAbstractWindowClass"
windows: typing.Dict[int, 'AbstractWindow'] = weakref.WeakValueDictionary() # type: ignore
events: typing.Dict[int, typing.Callable[[int, int, int, int], None]] = dict()
# noinspection SpellCheckingInspection
hinst = win32api.GetModuleHandle()
def __init__(self):
self._thread: typing.Optional[threading.Thread] = None
self._thread_id: typing.Optional[int] = None
self._window_started_event = threading.Event()
self._command = queue.Queue()
self._result = queue.Queue()
self.hwnd: typing.Optional[int] = None
self.position = (0, 0)
self.size = (400, 400)
# noinspection PyTypeChecker
self.rectangle: mag.types.Rectangle = (*self.position, *self.size)
self._fullscreen_rectangle: mag.types.Rectangle = (0, 0, 200, 200)
self._fullscreen_mode = False
@property
def rectangle(self) -> mag.types.Rectangle:
# noinspection PyTypeChecker
return *self.position, *self.size # type: ignore
@rectangle.setter
def rectangle(self, value):
self.position = value[:2]
self.size = value[2:]
@abc.abstractmethod
def _create_window(self):
pass
def create_window(self):
if self.is_alive:
raise RuntimeError("Magnification window is already running")
self._thread_id = win32api.GetCurrentThreadId()
# Note: Without Dpi setting magnifier image quality is low and creepy
ctypes.windll.shcore.SetProcessDpiAwareness(2)
self._register_window_class()
self._create_window()
self.__class__.windows[self.hwnd] = self
self._fullscreen_rectangle = get_fullscreen_size()
if self._fullscreen_mode:
make_fullscreen(self.hwnd, self._fullscreen_rectangle)
else:
make_partial_screen(self.hwnd, self.rectangle)
self._window_started_event.set()
@classmethod
def _register_window_class(cls):
if cls.window_class:
return
cls.window_class = win32gui.GetClassLong(win32gui.FindWindow(cls.window_class_name, None), win32con.GCW_ATOM)
if cls.window_class:
return
window_class = win32gui.WNDCLASS()
window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW
# noinspection SpellCheckingInspection
window_class.lpfnWndProc = cls.events
window_class.hInstance = cls.hinst
window_class.hCursor = win32api.LoadCursor(0, win32con.IDC_ARROW)
# noinspection SpellCheckingInspection
window_class.lpszClassName = cls.window_class_name
cls.window_class = win32gui.RegisterClass(window_class)
@property
def is_alive(self):
return self._window_started_event.is_set()
def _close(self):
# Implements close logic
if not self.is_alive:
raise RuntimeError("Window not started yet")
self._window_started_event.clear()
del self.__class__.windows[self.hwnd]
self.hwnd = None
win32api.PostQuitMessage(0)
@property
def current_rectangle(self):
if self._fullscreen_mode:
return self._fullscreen_rectangle
return self.rectangle
def win_event(message: int):
def _wrapper(fun: WinEventHandler):
# noinspection PyUnusedLocal
def on_event(hwnd: int, message_: int, wparam: int, lparam: int):
self = AbstractWindow.windows.get(hwnd, None)
if self:
return fun(self)
@functools.wraps(fun)
def run_event(self: AbstractWindow = None):
if self:
win32gui.PostMessage(
self.hwnd, message, 0, 0,
)
AbstractWindow.events[message] = on_event
return run_event
return _wrapper
class BasicWindow(AbstractWindow, abc.ABC):
@win_event(win32con.WM_CLOSE)
def close(self):
win32gui.DestroyWindow(self.hwnd)
@win_event(win32con.WM_DESTROY)
def _on_destroy(self):
self._close()
def wait_window_start(self, timeout: float = None):
self._window_started_event.wait(timeout)
def wait_window_stop(self, timeout: float = None):
if self._thread:
self._thread.join(timeout)
@property
def fullscreen_mode(self):
return self._fullscreen_mode
@fullscreen_mode.setter
def fullscreen_mode(self, value):
self._fullscreen_mode = value
if self.is_alive:
if value:
self._fullscreen_rectangle = get_fullscreen_size()
self._execute(make_fullscreen, self.hwnd, self._fullscreen_rectangle)
else:
self._execute(make_partial_screen, self.hwnd, self.rectangle)
def _execute(self, command: typing.Callable, *args, **kwargs):
if self._thread_id == win32api.GetCurrentThreadId():
return command(*args, **kwargs)
else:
element = (command, args, kwargs)
self._command.put(element)
self._on_command_get()
return self._result.get()
@win_event(win32con.WM_USER)
def _on_command_get(self):
command, args, kwargs = self._command.get()
result = command(*args, **kwargs)
self._result.put(result)
def run(self):
def inner():
win32gui.InitCommonControls()
self.create_window()
win32gui.PumpMessages()
self._after_close()
self._thread = threading.Thread(target=inner)
self._thread.start()
self.wait_window_start()
def _after_close(self):
pass
class MagnifierWindow(BasicWindow):
window_class_name = "Py_CustomMagnifierWindowHost"
def __init__(self):
super().__init__()
self.magnifier_hwnd: typing.Optional[int] = None
self.__magnifier: typing.Optional[mag.WinMagnificationAPI] = None
def create_window(self):
self.__magnifier = mag.WinMagnificationAPI()
super().create_window()
def _create_window(self):
self._create_host_window()
self._create_magnifier_window()
self.__magnifier.window.hwnd = self.magnifier_hwnd
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)
self._on_move()
def _create_magnifier_window(self):
# noinspection SpellCheckingInspection
self.magnifier_hwnd = win32gui.CreateWindow(
mag.const.WC_MAGNIFIER,
"Custom Magnifier Window",
win32con.WS_CHILD |
# mag.const.MS_SHOWMAGNIFIEDCURSOR |
win32con.WS_VISIBLE,
*win32gui.GetClientRect(self.hwnd),
self.hwnd, 0,
self.hinst,
None
)
def _create_host_window(self):
self.hwnd = win32gui.CreateWindowEx(
win32con.WS_EX_TOPMOST |
win32con.WS_EX_LAYERED |
win32con.WS_EX_TRANSPARENT,
self.window_class_name,
"Custom Magnifier Host Window",
win32con.WS_CLIPCHILDREN |
win32con.WS_CAPTION,
0, 0, 0, 0,
0, 0,
self.hinst,
None
)
win32gui.SetLayeredWindowAttributes(self.hwnd, 0, 255, win32con.LWA_ALPHA)
def _close(self):
super()._close()
self.magnifier_hwnd = None
def _after_close(self):
"""Call this method after PumpMessages"""
self.__magnifier.dispose()
@property
def controller(self):
return self.__magnifier.window
@win_event(win32con.WM_SIZE)
def _on_resize(self):
if not self.fullscreen_mode:
self.size = win32gui.GetClientRect(self.hwnd)[2:]
win32gui.SetWindowPos(self.magnifier_hwnd, 0, *win32gui.GetClientRect(self.hwnd), 0)
@win_event(win32con.WM_MOVE)
def _on_move(self):
if self.fullscreen_mode:
return
self.position = list(win32gui.GetWindowRect(self.hwnd)[:2])
self.position[0] += 2*win32api.GetSystemMetrics(win32con.SM_CXFRAME)
self.position[1] += 2*win32api.GetSystemMetrics(win32con.SM_CYFRAME)
self.position[1] += win32api.GetSystemMetrics(win32con.SM_CYCAPTION)
@win_event(win32con.WM_PAINT)
def _draw(self):
if not self.is_alive:
return
self.controller.source.raw = self.current_rectangle
if self.hwnd is None:
return
win32gui.SetWindowPos(
self.hwnd,
win32con.HWND_TOPMOST,
0, 0, 0, 0,
win32con.SWP_NOACTIVATE |
win32con.SWP_NOMOVE |
win32con.SWP_NOSIZE
)