Skip to content

Commit 0c782bd

Browse files
committed
Fix Python crash when hiding the title bar of more windows
1 parent 4306f73 commit 0c782bd

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

hPyT/hPyT.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class NCCALCSIZE_PARAMS(ctypes.Structure):
9595
rnbbcs: List[int] = []
9696
accent_color_titlebars: List[int] = []
9797
accent_color_borders: List[int] = []
98+
old_wndprocs: Dict[int, ctypes.c_uint64] = {}
99+
new_wndprocs: Dict[int, ctypes.c_uint64] = {}
98100

99101
WINDOWS_VERSION = float(platform.version().split(".")[0])
100102

@@ -130,10 +132,11 @@ def handle(hwnd: int, msg: int, wp: int, lp: int) -> int:
130132

131133
# Default processing for other messages
132134
return call_window_proc(
133-
*map(ctypes.c_uint64, (globals()[old], hwnd, msg, wp, lp))
135+
*map(ctypes.c_uint64, (old_wndprocs[hwnd], hwnd, msg, wp, lp))
134136
)
135137

136-
old, new = "old_wndproc", "new_wndproc"
138+
hwnd: int = module_find(window)
139+
137140
prototype = ctypes.WINFUNCTYPE(
138141
ctypes.c_uint64,
139142
ctypes.c_uint64,
@@ -142,8 +145,6 @@ def handle(hwnd: int, msg: int, wp: int, lp: int) -> int:
142145
ctypes.c_uint64,
143146
)
144147

145-
hwnd: int = module_find(window)
146-
147148
# Get the window and client dimensions
148149
rect = RECT()
149150
client_rect = RECT()
@@ -160,13 +161,13 @@ def handle(hwnd: int, msg: int, wp: int, lp: int) -> int:
160161
title_bar_height: int = full_height - client_height - border_width
161162

162163
# Override the window procedure if not already done
163-
if globals().get(old) is None:
164-
globals()[old] = get_window_long(hwnd, GWL_WNDPROC)
164+
if old_wndprocs.get(hwnd, 0) == 0:
165+
old_wndprocs[hwnd] = get_window_long(hwnd, GWL_WNDPROC)
165166

166167
# Do not remove the top border when on windows 7 or lower
167168
if WINDOWS_VERSION >= 10.0: # Windows 10 is version 10.0
168-
globals()[new] = prototype(handle)
169-
set_window_long(hwnd, GWL_WNDPROC, globals()[new])
169+
new_wndprocs[hwnd] = prototype(handle)
170+
set_window_long(hwnd, GWL_WNDPROC, new_wndprocs[hwnd])
170171

171172
old_style = get_window_long(hwnd, GWL_STYLE)
172173
new_style = (old_style & ~WS_CAPTION) | WS_BORDER
@@ -206,9 +207,10 @@ def unhide(cls, window: Any) -> None:
206207

207208
hwnd: int = module_find(window)
208209

209-
if globals().get("old_wndproc") is not None:
210-
set_window_long(hwnd, GWL_WNDPROC, globals()["old_wndproc"])
211-
globals()["old_wndproc"] = None
210+
if old_wndprocs.get(hwnd, 0) != 0:
211+
set_window_long(hwnd, GWL_WNDPROC, old_wndprocs[hwnd])
212+
old_wndprocs.pop(hwnd)
213+
new_wndprocs.pop(hwnd)
212214

213215
# Restore the original height if no_span was used
214216
height_reduction: int = cls._height_reduction.pop(hwnd, 0)

0 commit comments

Comments
 (0)