2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using WPFluent.Appearance;
|
|
|
|
|
|
using WPFluent.Interop;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
|
|
namespace WPFluent.Controls;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Applies the chosen backdrop effect to the selected window.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class WindowBackdrop
|
|
|
|
|
|
{
|
|
|
|
|
|
private static SolidColorBrush GetFallbackBackgroundBrush()
|
|
|
|
|
|
{
|
2025-05-05 17:04:06 +08:00
|
|
|
|
return ThemeManager.GetAppTheme() switch
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
2025-05-05 17:04:06 +08:00
|
|
|
|
ThemeType.Unknow => new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFA, 0xEF)),
|
|
|
|
|
|
ThemeType.Light => new SolidColorBrush(Color.FromArgb(0xFF, 0xFA, 0xFA, 0xFA)),
|
|
|
|
|
|
ThemeType.Dark => new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x20, 0x20)),
|
2025-02-10 20:53:40 +08:00
|
|
|
|
_ => new SolidColorBrush(Color.FromArgb(0xFF, 0xFA, 0xFA, 0xFA)),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*private static bool ApplyLegacyAcrylicBackdrop(IntPtr hWnd)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
private static bool RestoreContentBackground(IntPtr hWnd)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (hWnd == IntPtr.Zero)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (!User32.IsWindow(hWnd))
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var windowSource = HwndSource.FromHwnd(hWnd);
|
|
|
|
|
|
|
|
|
|
|
|
// Restore client area background
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (windowSource?.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
windowSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (windowSource?.RootVisual is System.Windows.Window window)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
var backgroundBrush = window.Resources["ApplicationBackgroundBrush"];
|
|
|
|
|
|
|
|
|
|
|
|
// Manual fallback
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (backgroundBrush is not SolidColorBrush)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
backgroundBrush = GetFallbackBackgroundBrush();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.Background = (SolidColorBrush)backgroundBrush;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Tries to remove background from <see cref="Window"/> and it's composition area.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="window">Window to manipulate.</param>
|
|
|
|
|
|
/// <returns><see langword="true"/> if operation was successful.</returns>
|
|
|
|
|
|
public static bool RemoveBackground(System.Windows.Window? window)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (window is null)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove background from visual root
|
|
|
|
|
|
window.SetCurrentValue(System.Windows.Controls.Control.BackgroundProperty, Brushes.Transparent);
|
|
|
|
|
|
|
|
|
|
|
|
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (windowHandle == IntPtr.Zero)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var windowSource = HwndSource.FromHwnd(windowHandle);
|
|
|
|
|
|
|
|
|
|
|
|
// Remove background from client area
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (windowSource?.Handle != IntPtr.Zero && windowSource?.CompositionTarget != null)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
windowSource.CompositionTarget.BackgroundColor = Colors.Transparent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|