@@ -168,6 +168,40 @@ public static void EnableBlur(Window window)
168168 Marshal . FreeHGlobal ( accentPtr ) ;
169169 }
170170
171+ public static void EnableAcrylicBlur ( Window window , Color tintColor )
172+ {
173+ window . Background = Brushes . Transparent ;
174+
175+ var hwnd = new WindowInteropHelper ( window ) . EnsureHandle ( ) ;
176+
177+ if ( ! window . AllowsTransparency && HwndSource . FromHwnd ( hwnd ) is HwndSource hwndSource )
178+ {
179+ hwndSource . CompositionTarget . BackgroundColor = Colors . Transparent ;
180+ }
181+
182+ var margins = new Dwmapi . Margins ( - 1 , - 1 , - 1 , - 1 ) ;
183+ Dwmapi . DwmExtendFrameIntoClientArea ( hwnd , ref margins ) ;
184+
185+ var accent = new AccentPolicy ( ) ;
186+ var accentStructSize = Marshal . SizeOf ( accent ) ;
187+ accent . AccentState = AccentState . AccentEnableAcrylicblurbehind ;
188+ accent . GradientColor = ToAbgr ( tintColor , 0.8 ) ;
189+
190+ var accentPtr = Marshal . AllocHGlobal ( accentStructSize ) ;
191+ Marshal . StructureToPtr ( accent , accentPtr , false ) ;
192+
193+ var data = new WindowCompositionAttributeData
194+ {
195+ Attribute = WindowCompositionAttribute . WcaAccentPolicy ,
196+ SizeOfData = accentStructSize ,
197+ Data = accentPtr
198+ } ;
199+
200+ User32 . SetWindowCompositionAttribute ( hwnd , ref data ) ;
201+
202+ Marshal . FreeHGlobal ( accentPtr ) ;
203+ }
204+
171205 public static void DisableDwmBlur ( Window window )
172206 {
173207 var accent = new AccentPolicy ( ) ;
@@ -202,22 +236,34 @@ public static void DisableDwmBlur(Window window)
202236 var micaEnabled = 0 ;
203237 Dwmapi . DwmSetWindowAttribute ( hwnd , ( uint ) Dwmapi . WindowAttribute . MicaEffect , ref micaEnabled , Marshal . SizeOf ( typeof ( int ) ) ) ;
204238 }
239+
240+ if ( Environment . OSVersion . Version >= new Version ( 10 , 0 , 22000 ) )
241+ {
242+ var captionColor = - 1 ;
243+ Dwmapi . DwmSetWindowAttribute ( hwnd , ( uint ) Dwmapi . WindowAttribute . CaptionColor , ref captionColor , Marshal . SizeOf ( typeof ( int ) ) ) ;
244+ }
205245 }
206246
207247 private static void EnableDwmBlur ( Window window , bool isDarkTheme , uint dwAttribute , int pvAttribute )
208248 {
209249 // Mica will handle the color
210250 window . Background = Brushes . Transparent ;
211251
212- var hwnd = new WindowInteropHelper ( window ) . Handle ;
252+ var hwnd = new WindowInteropHelper ( window ) . EnsureHandle ( ) ;
213253
214254 if ( ! window . AllowsTransparency && HwndSource . FromHwnd ( hwnd ) is HwndSource hwndSource )
215255 {
216256 hwndSource . CompositionTarget . BackgroundColor = Colors . Transparent ;
217257 }
218258
259+ if ( Environment . OSVersion . Version >= new Version ( 10 , 0 , 22000 ) )
260+ {
261+ var captionColor = - 2 ;
262+ Dwmapi . DwmSetWindowAttribute ( hwnd , ( uint ) Dwmapi . WindowAttribute . CaptionColor , ref captionColor , Marshal . SizeOf ( typeof ( int ) ) ) ;
263+ }
264+
219265 var isDarkThemeInt = isDarkTheme ? 1 : 0 ;
220- Dwmapi . DwmSetWindowAttribute ( hwnd , ( uint ) Dwmapi . WindowAttribute . UseImmersiveDarkMode , ref isDarkThemeInt , Marshal . SizeOf ( typeof ( bool ) ) ) ;
266+ Dwmapi . DwmSetWindowAttribute ( hwnd , ( uint ) Dwmapi . WindowAttribute . UseImmersiveDarkMode , ref isDarkThemeInt , Marshal . SizeOf ( typeof ( int ) ) ) ;
221267
222268 var margins = new Dwmapi . Margins ( - 1 , - 1 , - 1 , - 1 ) ;
223269 Dwmapi . DwmExtendFrameIntoClientArea ( hwnd , ref margins ) ;
@@ -260,7 +306,16 @@ private enum AccentState
260306 AccentEnableGradient = 1 ,
261307 AccentEnableTransparentgradient = 2 ,
262308 AccentEnableBlurbehind = 3 ,
263- AccentInvalidState = 4 ,
309+ AccentEnableAcrylicblurbehind = 4 ,
310+ AccentInvalidState = 5 ,
311+ }
312+
313+ private static uint ToAbgr ( Color color , double alphaScale )
314+ {
315+ return ( uint ) ( color . R << 0 |
316+ color . G << 8 |
317+ color . B << 16 |
318+ ( int ) ( color . A * alphaScale ) << 24 ) ;
264319 }
265320
266321 [ StructLayout ( LayoutKind . Sequential ) ]
0 commit comments