-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_effects.py
165 lines (132 loc) · 5.57 KB
/
window_effects.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
from ctypes import Structure, POINTER, c_int, sizeof, pointer, cdll, c_bool, \
byref
from ctypes.wintypes import DWORD, ULONG, BOOL, HRGN, LPCVOID, LONG
from enum import Enum
from sys import getwindowsversion
import win32con
import win32gui
class WINDOWCOMPOSITIONATTRIB(Enum):
WCA_ACCENT_POLICY = 19
WCA_USEDARKMODECOLORS = 26
class ACCENT_STATE(Enum):
ACCENT_DISABLED = 0
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4 # Acrylic effect
ACCENT_ENABLE_HOSTBACKDROP = 5 # Mica effect
class DWMWINDOWATTRIBUTE(Enum):
DWMWA_NCRENDERING_POLICY = 2
class DWMNCRENDERINGPOLICY(Enum):
DWMNCRP_DISABLED = 1
class ACCENT_POLICY(Structure):
_fields_ = [
('AccentState', DWORD),
('AccentFlags', DWORD),
('GradientColor', DWORD),
('AnimationId', DWORD)
]
class WINDOWCOMPOSITIONATTRIBDATA(Structure):
_fields_ = [
('Attribute', DWORD),
('Data', POINTER(ACCENT_POLICY)),
('SizeOfData', ULONG)
]
class MARGINS(Structure):
_fields_ = [
('cxLeftWidth', c_int),
('cxRightWidth', c_int),
('cyTopHeight', c_int),
('cyBottomHeight', c_int)
]
class DWM_BLURBEHIND(Structure):
_fields_ = [
('dwFlags', DWORD),
('fEnable', BOOL),
('hRgnBlur', HRGN),
('fTransitionOnMaximized', BOOL)
]
class WindowsEffects:
""" Class for applying Windows effects """
def __init__(self):
# Initialize structure
self.accent_policy = ACCENT_POLICY()
self.win_comp_attr_data = WINDOWCOMPOSITIONATTRIBDATA()
self.win_comp_attr_data.Attribute = \
WINDOWCOMPOSITIONATTRIB.WCA_ACCENT_POLICY.value
self.win_comp_attr_data.SizeOfData = sizeof(self.accent_policy)
self.win_comp_attr_data.Data = pointer(self.accent_policy)
# Declare the function signature of the API
user_32 = cdll.LoadLibrary('user32')
self.set_win_comp_attr = user_32.SetWindowCompositionAttribute
dwm_api = cdll.LoadLibrary('dwmapi')
self.dwm_ext_frame_into_client_area = \
dwm_api.DwmExtendFrameIntoClientArea
self.dwm_enable_blur_behind_win = dwm_api.DwmEnableBlurBehindWindow
self.dwm_set_win_attr = dwm_api.DwmSetWindowAttribute
self.set_win_comp_attr.argtypes = [
c_int, POINTER(WINDOWCOMPOSITIONATTRIBDATA)]
self.dwm_ext_frame_into_client_area.argtypes = [
c_int, POINTER(MARGINS)]
self.dwm_enable_blur_behind_win.argtypes = [
c_int, POINTER(DWM_BLURBEHIND)]
self.dwm_set_win_attr.argtypes = [
c_int, DWORD, LPCVOID, DWORD]
self.set_win_comp_attr.restype = c_bool
self.dwm_ext_frame_into_client_area.restype = LONG
self.dwm_enable_blur_behind_win.restype = LONG
self.dwm_set_win_attr.restype = LONG
def add_acrylic_effect(self, h_wnd, gradient_color,
enable_shadow=True, animation_id=0):
gradient_color = ''.join(gradient_color[i:i + 2]
for i in range(6, -1, -2))
gradient_color = DWORD(int(gradient_color, base=16))
accent_flags = DWORD(0x20 | 0x40 | 0x80 | 0x100) \
if enable_shadow else DWORD(0)
animation_id = DWORD(animation_id)
self.accent_policy.AccentState = \
ACCENT_STATE.ACCENT_ENABLE_ACRYLICBLURBEHIND.value
self.accent_policy.AccentFlags = accent_flags
self.accent_policy.GradientColor = gradient_color
self.accent_policy.AnimationId = animation_id
self.set_win_comp_attr(int(h_wnd), pointer(self.win_comp_attr_data))
def add_mica_effect(self, h_wnd, dark_mode=False):
h_wnd = int(h_wnd)
self.accent_policy.AccentState = \
ACCENT_STATE.ACCENT_ENABLE_HOSTBACKDROP.value
self.set_win_comp_attr(h_wnd, pointer(self.win_comp_attr_data))
if dark_mode:
self.win_comp_attr_data.Attribute = \
WINDOWCOMPOSITIONATTRIB.WCA_USEDARKMODECOLORS.value
self.set_win_comp_attr(h_wnd, pointer(self.win_comp_attr_data))
self.win_comp_attr_data.Attribute = \
WINDOWCOMPOSITIONATTRIB.WCA_ACCENT_POLICY.value
if getwindowsversion().build >= 22523:
self.dwm_set_win_attr(h_wnd, 38, byref(c_int(2)), 4)
else:
self.dwm_set_win_attr(h_wnd, 1029, byref(c_int(1)), 4)
def remove_background_effect(self, h_wnd):
self.accent_policy.AccentState = ACCENT_STATE.ACCENT_DISABLED.value
self.set_win_comp_attr(int(h_wnd), pointer(self.win_comp_attr_data))
def add_shadow_effect(self, h_wnd):
margins = MARGINS(-1, -1, -1, -1)
self.dwm_ext_frame_into_client_area(int(h_wnd), byref(margins))
def remove_shadow_effect(self, h_wnd):
self.dwm_set_win_attr(
int(h_wnd),
DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_POLICY.value,
byref(c_int(DWMNCRENDERINGPOLICY.DWMNCRP_DISABLED.value)),
4
)
@staticmethod
def add_window_animation(h_wnd):
h_wnd = int(h_wnd)
style = win32gui.GetWindowLong(h_wnd, win32con.GWL_STYLE)
win32gui.SetWindowLong(
h_wnd, win32con.GWL_STYLE,
style
| win32con.WS_MINIMIZEBOX
| win32con.WS_MAXIMIZEBOX
| win32con.WS_CAPTION
| win32con.WS_THICKFRAME
)
def add_blur_behind_window(self, h_wnd):
blur_behind = DWM_BLURBEHIND(1, True, 0, False)
self.dwm_enable_blur_behind_win(int(h_wnd), byref(blur_behind))