Files
community/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs
T

95 lines
3.6 KiB
C#
Raw Normal View History

2025-08-31 09:54:13 +08:00
using System;
2025-05-25 09:29:48 +08:00
using System.Windows;
using System.Windows.Media;
2025-08-31 09:54:13 +08:00
using iNKORE.UI.WPF.Modern;
using Microsoft.Win32;
2025-05-25 09:29:48 +08:00
using Application = System.Windows.Application;
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
2025-05-25 09:29:48 +08:00
private Color FloatBarForegroundColor = Color.FromRgb(102, 102, 102);
2025-08-03 16:46:33 +08:00
private void SetTheme(string theme)
{
if (theme == "Light")
{
2025-07-28 14:40:44 +08:00
var rd1 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/Styles/Light.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd1);
2025-07-28 14:40:44 +08:00
var rd2 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd2);
2025-07-28 14:40:44 +08:00
var rd3 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd3);
2025-07-28 14:40:44 +08:00
var rd4 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd4);
ThemeManager.SetRequestedTheme(window, ElementTheme.Light);
FloatBarForegroundColor = (Color)Application.Current.FindResource("FloatBarForegroundColor");
}
2025-08-03 16:46:33 +08:00
else if (theme == "Dark")
{
2025-07-28 14:40:44 +08:00
var rd1 = new ResourceDictionary { Source = new Uri("Resources/Styles/Dark.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd1);
2025-07-28 14:40:44 +08:00
var rd2 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd2);
2025-07-28 14:40:44 +08:00
var rd3 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd3);
2025-07-28 14:40:44 +08:00
var rd4 = new ResourceDictionary
2025-08-03 16:46:33 +08:00
{ Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) };
2025-05-25 09:29:48 +08:00
Application.Current.Resources.MergedDictionaries.Add(rd4);
ThemeManager.SetRequestedTheme(window, ElementTheme.Dark);
FloatBarForegroundColor = (Color)Application.Current.FindResource("FloatBarForegroundColor");
}
}
2025-08-03 16:46:33 +08:00
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
switch (Settings.Appearance.Theme)
{
2025-05-25 09:29:48 +08:00
case 0:
SetTheme("Light");
break;
case 1:
SetTheme("Dark");
break;
case 2:
if (IsSystemThemeLight()) SetTheme("Light");
else SetTheme("Dark");
break;
}
}
2025-08-03 16:46:33 +08:00
private bool IsSystemThemeLight()
{
2025-05-25 09:29:48 +08:00
var light = false;
2025-08-03 16:46:33 +08:00
try
{
2025-05-25 09:29:48 +08:00
var registryKey = Registry.CurrentUser;
var themeKey =
registryKey.OpenSubKey("software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
var keyValue = 0;
if (themeKey != null) keyValue = (int)themeKey.GetValue("SystemUsesLightTheme");
if (keyValue == 1) light = true;
}
catch { }
return light;
}
}
}