Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions pywinstyles/py_win_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from __future__ import annotations
from typing import Any, Union, Callable
import sys


try:
import winreg
Expand All @@ -18,6 +20,10 @@
raise ImportError("pywinstyles import errror: No windows environment detected!")


windows_version = sys.getwindowsversion()
is_windows_10 = windows_version.major == 10 and windows_version.build < 22000


class ACCENT_POLICY(Structure):
_fields_ = [
("AccentState", DWORD),
Expand Down Expand Up @@ -102,6 +108,17 @@ def __init__(self, window, style: str) -> None:
ChangeDWMAccent(self.HWND, 19, 0)
DisableFrameIntoClientArea(self.HWND)

# On Windows 10, changing the title bar's style to "light" or "dark" will not automatically make
# the title bar redraw (unlike on Windows 11), so we'll have to force this by toggling the window's
# nonclient area focus state
if style in ["light", "dark", "normal"] and is_windows_10:
is_window_focused = windll.user32.GetForegroundWindow() == self.HWND

# 0x0086 is WM_NCACTIVATE
windll.user32.SendMessageW(self.HWND, 0x0086, not is_window_focused, 0)
windll.user32.SendMessageW(self.HWND, 0x0086, is_window_focused, 0)


class change_header_color():
"""change the titlebar background color"""

Expand Down Expand Up @@ -269,8 +286,16 @@ def get_accent_color() -> str:
def detect(window: Any):
"""detect the type of UI library and return HWND"""
try: # tkinter
window.update()
return windll.user32.GetParent(window.winfo_id())
hwnd = windll.user32.GetParent(window.winfo_id())

if hwnd == 0:
# The window was not realized yet
# We need to call `update` to make sure it gets realized before trying to get its hwnd

window.update()
hwnd = windll.user32.GetParent(window.winfo_id())

return hwnd
except:
pass
try: # pyqt/pyside
Expand Down