diff --git a/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs b/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs index d7655fd2..0b859e93 100644 --- a/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs +++ b/BetterGenshinImpact/Helpers/Ui/WindowHelper.cs @@ -1,4 +1,6 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; using System.Windows.Media; using BetterGenshinImpact.Core.Config; using BetterGenshinImpact.GameTask; @@ -9,6 +11,8 @@ namespace BetterGenshinImpact.Helpers.Ui; public class WindowHelper { + private const uint DesktopCompositionDisabledHResult = 0x80263001; + public static void TryApplySystemBackdrop(System.Windows.Window window) { var themeType = TaskContext.Instance().Config.CommonConfig.CurrentThemeType; @@ -37,6 +41,22 @@ public class WindowHelper /// 要应用主题的窗口 /// 主题类型 public static void ApplyThemeToWindow(System.Windows.Window window, ThemeType themeType) + { + try + { + ApplyThemeCore(window, themeType); + } + catch (COMException ex) when ((uint)ex.HResult == DesktopCompositionDisabledHResult) + { + ApplyFallbackTheme(window, themeType); + } + catch + { + ApplyFallbackTheme(window, themeType); + } + } + + private static void ApplyThemeCore(System.Windows.Window window, ThemeType themeType) { switch (themeType) { @@ -76,4 +96,21 @@ public class WindowHelper break; } } -} \ No newline at end of file + + private static void ApplyFallbackTheme(System.Windows.Window window, ThemeType themeType) + { + window.Background = new SolidColorBrush(GetFallbackBackgroundColor(themeType)); + WindowBackdrop.ApplyBackdrop(window, WindowBackdropType.None); + } + + private static Color GetFallbackBackgroundColor(ThemeType themeType) + { + return themeType switch + { + ThemeType.LightNone => Color.FromArgb(255, 243, 243, 243), + ThemeType.LightMica => Color.FromArgb(255, 243, 243, 243), + ThemeType.LightAcrylic => Color.FromArgb(255, 243, 243, 243), + _ => Color.FromArgb(255, 32, 32, 32) + }; + } +}