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

239 lines
10 KiB
C#
Raw Normal View History

2025-08-31 11:43:52 +08:00
using iNKORE.UI.WPF.Modern;
using Microsoft.Win32;
using System;
2025-09-21 00:25:09 +08:00
using System.Collections.Generic;
2025-05-25 09:29:48 +08:00
using System.Windows;
using System.Windows.Media;
using Application = System.Windows.Application;
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
2025-09-21 00:25:09 +08:00
private Color FloatBarForegroundColor;
2025-05-25 09:29:48 +08:00
2025-08-03 16:46:33 +08:00
private void SetTheme(string theme)
{
2025-09-21 00:25:09 +08:00
// 清理现有的主题资源
var resourcesToRemove = new List<ResourceDictionary>();
foreach (var dict in Application.Current.Resources.MergedDictionaries)
{
if (dict.Source != null &&
(dict.Source.ToString().Contains("Light.xaml") ||
dict.Source.ToString().Contains("Dark.xaml")))
{
resourcesToRemove.Add(dict);
}
}
foreach (var dict in resourcesToRemove)
{
Application.Current.Resources.MergedDictionaries.Remove(dict);
}
2025-08-03 16:46:33 +08:00
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-09-21 02:05:49 +08:00
// 在主题资源之后添加其他资源
var rd2 = new ResourceDictionary
{ Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd2);
var rd3 = new ResourceDictionary
{ Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd3);
var rd4 = new ResourceDictionary
{ Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd4);
2025-05-25 09:29:48 +08:00
ThemeManager.SetRequestedTheme(window, ElementTheme.Light);
2025-09-21 00:25:09 +08:00
InitializeFloatBarForegroundColor();
2025-10-01 09:13:48 +08:00
// 刷新快速面板图标
RefreshQuickPanelIcons();
2025-09-21 00:25:09 +08:00
// 强制刷新UI
window.InvalidateVisual();
2025-05-25 09:29:48 +08:00
}
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-09-21 02:05:49 +08:00
// 在主题资源之后添加其他资源
var rd2 = new ResourceDictionary
{ Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd2);
var rd3 = new ResourceDictionary
{ Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd3);
var rd4 = new ResourceDictionary
{ Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd4);
2025-05-25 09:29:48 +08:00
ThemeManager.SetRequestedTheme(window, ElementTheme.Dark);
2025-09-21 00:25:09 +08:00
InitializeFloatBarForegroundColor();
2025-10-01 09:13:48 +08:00
// 刷新快速面板图标
RefreshQuickPanelIcons();
2025-09-21 00:25:09 +08:00
// 强制刷新UI
window.InvalidateVisual();
}
}
/// <summary>
/// 初始化FloatBarForegroundColor,从当前主题资源中加载颜色
/// </summary>
private void InitializeFloatBarForegroundColor()
{
try
{
2025-05-25 09:29:48 +08:00
FloatBarForegroundColor = (Color)Application.Current.FindResource("FloatBarForegroundColor");
2025-09-21 00:39:30 +08:00
// 强制刷新浮动工具栏按钮颜色
RefreshFloatingBarButtonColors();
2025-05-25 09:29:48 +08:00
}
2025-09-21 00:39:30 +08:00
catch (Exception)
2025-09-21 00:25:09 +08:00
{
// 如果无法从资源中加载,使用默认颜色
FloatBarForegroundColor = Color.FromRgb(0, 0, 0);
2025-09-21 00:39:30 +08:00
}
}
2025-10-01 09:13:48 +08:00
/// <summary>
/// 刷新快速面板图标
/// </summary>
private void RefreshQuickPanelIcons()
{
try
{
if (LeftUnFoldButtonQuickPanel != null)
{
LeftUnFoldButtonQuickPanel.InvalidateVisual();
}
if (RightUnFoldButtonQuickPanel != null)
{
RightUnFoldButtonQuickPanel.InvalidateVisual();
}
if (LeftSidePanel != null)
{
LeftSidePanel.InvalidateVisual();
}
if (RightSidePanel != null)
{
RightSidePanel.InvalidateVisual();
}
}
catch (Exception)
{
}
}
2025-09-21 00:39:30 +08:00
/// <summary>
/// 刷新浮动工具栏按钮颜色
/// </summary>
private void RefreshFloatingBarButtonColors()
{
try
{
// 选中状态的颜色(蓝底)
var selectedColor = Color.FromRgb(30, 58, 138);
// 根据当前模式设置按钮颜色
switch (_currentToolMode)
{
case "cursor":
CursorIconGeometry.Brush = new SolidColorBrush(selectedColor);
PenIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
break;
case "pen":
case "color":
CursorIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
PenIconGeometry.Brush = new SolidColorBrush(selectedColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
break;
case "eraser":
CursorIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
PenIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(selectedColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
break;
case "eraserByStrokes":
CursorIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
PenIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(selectedColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
break;
case "select":
CursorIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
PenIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(selectedColor);
break;
default:
// 默认情况,所有按钮都使用主题颜色
CursorIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
PenIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
StrokeEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
CircleEraserIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
LassoSelectIconGeometry.Brush = new SolidColorBrush(FloatBarForegroundColor);
break;
}
}
catch (Exception)
{
2025-09-21 00:25:09 +08:00
}
2025-05-25 09:29:48 +08:00
}
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;
}
}
}