using WPFluent.Appearance; using WPFluent.Interop; using System.Runtime.InteropServices; // ReSharper disable once CheckNamespace namespace WPFluent.Controls; /// /// Applies the chosen backdrop effect to the selected window. /// public static class WindowBackdrop { private static bool ApplyDwmwWindowAttrubute(IntPtr hWnd, Dwmapi.DWMSBT dwmSbt) { if(hWnd == IntPtr.Zero) { return false; } if(!User32.IsWindow(hWnd)) { return false; } int backdropPvAttribute = (int)dwmSbt; var dwmApiResult = Dwmapi.DwmSetWindowAttribute( hWnd, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropPvAttribute, Marshal.SizeOf(typeof(int))); return dwmApiResult == HRESULT.S_OK; } private static bool ApplyLegacyMicaBackdrop(IntPtr hWnd) { var backdropPvAttribute = 1; // Enable // TODO: Validate HRESULT var dwmApiResult = Dwmapi.DwmSetWindowAttribute( hWnd, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_MICA_EFFECT, ref backdropPvAttribute, Marshal.SizeOf(typeof(int))); return dwmApiResult == HRESULT.S_OK; } private static SolidColorBrush GetFallbackBackgroundBrush() { return ApplicationThemeManager.GetAppTheme() switch { ApplicationTheme.HighContrast => ApplicationThemeManager.GetSystemTheme() switch { SystemTheme.HC1 => new SolidColorBrush(Color.FromArgb(0xFF, 0x2D, 0x32, 0x36)), SystemTheme.HC2 => new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00)), SystemTheme.HCBlack => new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x20, 0x20)), SystemTheme.HCWhite => new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x20, 0x20)), _ => new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFA, 0xEF)), }, ApplicationTheme.Dark => new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x20, 0x20)), ApplicationTheme.Light => new SolidColorBrush(Color.FromArgb(0xFF, 0xFA, 0xFA, 0xFA)), _ => new SolidColorBrush(Color.FromArgb(0xFF, 0xFA, 0xFA, 0xFA)), }; } /*private static bool ApplyLegacyAcrylicBackdrop(IntPtr hWnd) { throw new NotImplementedException(); }*/ private static bool RestoreContentBackground(IntPtr hWnd) { if(hWnd == IntPtr.Zero) { return false; } if(!User32.IsWindow(hWnd)) { return false; } var windowSource = HwndSource.FromHwnd(hWnd); // Restore client area background if(windowSource?.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null) { windowSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor; } if(windowSource?.RootVisual is System.Windows.Window window) { var backgroundBrush = window.Resources["ApplicationBackgroundBrush"]; // Manual fallback if(backgroundBrush is not SolidColorBrush) { backgroundBrush = GetFallbackBackgroundBrush(); } window.Background = (SolidColorBrush)backgroundBrush; } return true; } /// /// Applies a backdrop effect to the selected . /// /// The window to which the backdrop effect will be applied. /// The type of backdrop effect to apply. Determines the visual appearance of the window's backdrop. /// if the operation was successful; otherwise, . public static bool ApplyBackdrop(System.Windows.Window? window, WindowBackdropType backdropType) { if(window is null) { return false; } if(window.IsLoaded) { IntPtr windowHandle = new WindowInteropHelper(window).Handle; if(windowHandle == IntPtr.Zero) { return false; } return ApplyBackdrop(windowHandle, backdropType); } window.Loaded += (sender, _1) => { IntPtr windowHandle = new WindowInteropHelper(sender as System.Windows.Window ?? null)?.Handle ?? IntPtr.Zero; if(windowHandle == IntPtr.Zero) { return; } _ = ApplyBackdrop(windowHandle, backdropType); }; return true; } /// /// Applies backdrop effect to the selected window handle based on the specified backdrop type. /// /// The window handle to which the backdrop effect will be applied. /// The type of backdrop effect to apply. This determines the visual appearance of the window's backdrop. /// if the operation was successful; otherwise, . public static bool ApplyBackdrop(IntPtr hWnd, WindowBackdropType backdropType) { if(hWnd == IntPtr.Zero) { return false; } if(!User32.IsWindow(hWnd)) { return false; } if(ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Dark) { _ = UnsafeNativeMethods.ApplyWindowDarkMode(hWnd); } else { _ = UnsafeNativeMethods.RemoveWindowDarkMode(hWnd); } _ = UnsafeNativeMethods.RemoveWindowCaption(hWnd); // 22H1 if(!Win32.Utilities.IsOSWindows11Insider1OrNewer) { if(backdropType != WindowBackdropType.None) { return ApplyLegacyMicaBackdrop(hWnd); } return false; } return backdropType switch { WindowBackdropType.Auto => ApplyDwmwWindowAttrubute(hWnd, Dwmapi.DWMSBT.DWMSBT_AUTO), WindowBackdropType.Mica => ApplyDwmwWindowAttrubute(hWnd, Dwmapi.DWMSBT.DWMSBT_MAINWINDOW), WindowBackdropType.Acrylic => ApplyDwmwWindowAttrubute(hWnd, Dwmapi.DWMSBT.DWMSBT_TRANSIENTWINDOW), WindowBackdropType.Tabbed => ApplyDwmwWindowAttrubute(hWnd, Dwmapi.DWMSBT.DWMSBT_TABBEDWINDOW), _ => ApplyDwmwWindowAttrubute(hWnd, Dwmapi.DWMSBT.DWMSBT_DISABLE), }; } /// /// Checks whether the selected backdrop type is supported on current platform. /// /// if the selected backdrop type is supported on current platform. public static bool IsSupported(WindowBackdropType backdropType) { return backdropType switch { WindowBackdropType.Auto => Win32.Utilities.IsOSWindows11Insider1OrNewer, WindowBackdropType.Tabbed => Win32.Utilities.IsOSWindows11Insider1OrNewer, WindowBackdropType.Mica => Win32.Utilities.IsOSWindows11OrNewer, WindowBackdropType.Acrylic => Win32.Utilities.IsOSWindows7OrNewer, WindowBackdropType.None => true, _ => false, }; } /// /// Tries to remove backdrop effects if they have been applied to the . /// /// The window from which the effect should be removed. public static bool RemoveBackdrop(System.Windows.Window? window) { if(window is null) { return false; } IntPtr windowHandle = new WindowInteropHelper(window).Handle; return RemoveBackdrop(windowHandle); } /// /// Tries to remove all effects if they have been applied to the hWnd. /// /// Pointer to the window handle. public static bool RemoveBackdrop(IntPtr hWnd) { if(hWnd == IntPtr.Zero) { return false; } _ = RestoreContentBackground(hWnd); if(hWnd == IntPtr.Zero) { return false; } if(!User32.IsWindow(hWnd)) { return false; } var pvAttribute = 0; // Disable var backdropPvAttribute = (int)Dwmapi.DWMSBT.DWMSBT_DISABLE; _ = Dwmapi.DwmSetWindowAttribute( hWnd, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_MICA_EFFECT, ref pvAttribute, Marshal.SizeOf(typeof(int))); _ = Dwmapi.DwmSetWindowAttribute( hWnd, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropPvAttribute, Marshal.SizeOf(typeof(int))); return true; } /// /// Tries to remove background from and it's composition area. /// /// Window to manipulate. /// if operation was successful. public static bool RemoveBackground(System.Windows.Window? window) { if(window is null) { return false; } // Remove background from visual root window.SetCurrentValue(System.Windows.Controls.Control.BackgroundProperty, Brushes.Transparent); IntPtr windowHandle = new WindowInteropHelper(window).Handle; if(windowHandle == IntPtr.Zero) { return false; } var windowSource = HwndSource.FromHwnd(windowHandle); // Remove background from client area if(windowSource?.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null) { windowSource.CompositionTarget.BackgroundColor = Colors.Transparent; } return true; } public static bool RemoveTitlebarBackground(System.Windows.Window? window) { if(window is null) { return false; } IntPtr windowHandle = new WindowInteropHelper(window).Handle; if(windowHandle == IntPtr.Zero) { return false; } HwndSource? windowSource = HwndSource.FromHwnd(windowHandle); // Remove background from client area if(windowSource?.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null) { // NOTE: https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute // Specifying DWMWA_COLOR_DEFAULT (value 0xFFFFFFFF) for the color will reset the window back to using the system's default behavior for the caption color. uint titlebarPvAttribute = 0xFFFFFFFE; _ = Dwmapi.DwmSetWindowAttribute( windowSource.Handle, Dwmapi.DWMWINDOWATTRIBUTE.DWMWA_CAPTION_COLOR, ref titlebarPvAttribute, sizeof(uint)); } return true; } }