系統換膚功能。
效果:
html
資源文件前端
各個顏色的依賴屬性git
屬性:定義各個顏色的默認顏色
做用:github
屬性:倉庫類型
方法:從倉庫取出顏色、把顏色保存到倉庫數據庫
定義倉庫的方法:取、存express
存取出主題顏色到 Property框架
存取出主題顏色到 XML 文件ide
取出資源中 FaceColor 對象的顏色,進行查看、修改、保存操做。
其中修改的即時性是實時,無需重啓。
保存的方式爲:保存到 Property 或 xml 文件或數據庫。this
存檔管理。spa
檔案。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:XX" xmlns:Helper="clr-namespace:XX.Helper.FaceColor"> <Helper:FaceColor x:Key="FaceColor"/> <SolidColorBrush x:Key="BackgroundColor" Color="{Binding BackgroundColor, Source={StaticResource FaceColor}}" /> <SolidColorBrush x:Key="TopBackgroundColor" Color="{Binding TopBackgroundColor, Source={StaticResource FaceColor}}" /> <SolidColorBrush x:Key="TopStrokeColor" Color="{Binding TopStrokeColor, Source={StaticResource FaceColor}}" /> <SolidColorBrush x:Key="UserControlStrokeColor" Color="{Binding UserControlStrokeColor, Source={StaticResource FaceColor}}" /> <SolidColorBrush x:Key="UserControlBackgroundColor" Color="{Binding UserControlBackgroundColor, Source={StaticResource FaceColor}}" /> </ResourceDictionary>
namespace XX.Helper.FaceColor { public class FaceColor : DependencyObject, ICloneable { /// <summary> /// 主背景顏色 /// </summary> public Color BackgroundColor { get { return (Color)GetValue(BackgroundColorProperty); } set { SetValue(BackgroundColorProperty, value); } } public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(FaceColor), new PropertyMetadata(DefaultColorForFace.BackgroundColor)); /// <summary> /// 頂部背景色 /// </summary> public Color TopBackgroundColor { get { return (Color)GetValue(TopBackgroundColorProperty); } set { SetValue(TopBackgroundColorProperty, value); } } public static readonly DependencyProperty TopBackgroundColorProperty = DependencyProperty.Register("TopBackgroundColor", typeof(Color), typeof(FaceColor), new PropertyMetadata(DefaultColorForFace.TopBackgroundColor)); /// <summary> /// 頂部邊框顏色 /// </summary> public Color TopStrokeColor { get { return (Color)GetValue(TopStrokeColorProperty); } set { SetValue(TopStrokeColorProperty, value); } } public static readonly DependencyProperty TopStrokeColorProperty = DependencyProperty.Register("TopStrokeColor", typeof(Color), typeof(FaceColor), new PropertyMetadata(DefaultColorForFace.TopStrokeColor)); /// <summary> /// 子控件的 4 個角、標題矩形的顏色 /// </summary> public Color UserControlStrokeColor { get { return (Color)GetValue(UserControlStrokeColorProperty); } set { SetValue(UserControlStrokeColorProperty, value); } } public static readonly DependencyProperty UserControlStrokeColorProperty = DependencyProperty.Register("UserControlStrokeColor", typeof(Color), typeof(FaceColor), new PropertyMetadata(DefaultColorForFace.UserControlStrokeColor)); /// <summary> /// 子控件的背景色 /// </summary> public Color UserControlBackgroundColor { get { return (Color)GetValue(UserControlBackgroundColorProperty); } set { SetValue(UserControlBackgroundColorProperty, value); } } public static readonly DependencyProperty UserControlBackgroundColorProperty = DependencyProperty.Register("UserControlBackgroundColor", typeof(Color), typeof(FaceColor), new PropertyMetadata(DefaultColorForFace.UserControlBackgroundColor)); public object Clone() { return (object)this.MemberwiseClone(); } } }
namespace XX.Helper.FaceColor { public class DefaultColorForFace { public static Color BackgroundColor = Color.FromRgb(11, 65, 92); public static Color TopBackgroundColor = Color.FromRgb(6, 79, 120); public static Color TopStrokeColor = Color.FromRgb(0, 191, 191); public static Color UserControlStrokeColor = Color.FromRgb(13, 253, 254); public static Color UserControlBackgroundColor = Color.FromRgb(10, 38, 50); } }
namespace XX.Helper.FaceColor { /// <summary> /// 皮膚的存取 /// 程序啓動時,取 /// 發生修改時,存 /// </summary> public class FaceColorHelper { private readonly static SaveFaceColor _saveFaceColor = new SaveFaceColorToProperties(); /// <summary> /// 4. 程序啓動,從倉庫中取出 5 種顏色,賦予給 ColorObj 的 5 個屬性; /// </summary> public static void InitColor() { _saveFaceColor.InitColor(); } /// <summary> /// 把 ColorObj 的 5 種顏色保存到倉庫中 /// </summary> public static void SaveColor() { _saveFaceColor.SaveColor(); } }
/// <summary> /// 保存主題顏色的方式:Property、xml 文件、數據庫 /// </summary> abstract class SaveFaceColor { public abstract void InitColor(); public abstract void SaveColor(); }
存取出主題顏色到 Property。
/// <summary> /// 從 Property 取出、保存主題顏色 /// </summary> class SaveFaceColorToProperties : SaveFaceColor { public override void InitColor() { FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (faceColor == null) return; try { Drawing.Color backColor = (Drawing.Color)Properties.Settings.Default["backgroundColor"]; faceColor.BackgroundColor = Media.Color.FromArgb(backColor.A, backColor.R, backColor.G, backColor.B); Drawing.Color topBackColor = (Drawing.Color)Properties.Settings.Default["topBackgroundColor"]; faceColor.TopBackgroundColor = Media.Color.FromArgb(topBackColor.A, topBackColor.R, topBackColor.G, topBackColor.B); Drawing.Color topSkColor = (Drawing.Color)Properties.Settings.Default["topStrokeColor"]; faceColor.TopStrokeColor = Media.Color.FromArgb(topSkColor.A, topSkColor.R, topSkColor.G, topSkColor.B); Drawing.Color ucSkColor = (Drawing.Color)Properties.Settings.Default["userControlStrokeColor"]; faceColor.UserControlStrokeColor = Media.Color.FromArgb(ucSkColor.A, ucSkColor.R, ucSkColor.G, ucSkColor.B); Drawing.Color ucBackColor = (Drawing.Color)Properties.Settings.Default["userControlBackgroundColor"]; faceColor.UserControlBackgroundColor = Media.Color.FromArgb(ucBackColor.A, ucBackColor.R, ucBackColor.G, ucBackColor.B); } catch (Exception ex) { // 從 Property 取出主題顏色失敗 } } /// <summary> /// 保存主題顏色到 App.Properties /// </summary> public override void SaveColor() { FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (faceColor == null) return; try { Properties.Settings.Default["backgroundColor"] = Drawing.Color.FromArgb(faceColor.BackgroundColor.A, faceColor.BackgroundColor.R, faceColor.BackgroundColor.G, faceColor.BackgroundColor.B); Properties.Settings.Default["topBackgroundColor"] = Drawing.Color.FromArgb(faceColor.TopBackgroundColor.A, faceColor.TopBackgroundColor.R, faceColor.TopBackgroundColor.G, faceColor.TopBackgroundColor.B); Properties.Settings.Default["topStrokeColor"] = Drawing.Color.FromArgb(faceColor.TopStrokeColor.A, faceColor.TopStrokeColor.R, faceColor.TopStrokeColor.G, faceColor.TopStrokeColor.B); Properties.Settings.Default["userControlStrokeColor"] = Drawing.Color.FromArgb(faceColor.UserControlStrokeColor.A, faceColor.UserControlStrokeColor.R, faceColor.UserControlStrokeColor.G, faceColor.UserControlStrokeColor.B); Properties.Settings.Default["userControlBackgroundColor"] = Drawing.Color.FromArgb(faceColor.UserControlBackgroundColor.A, faceColor.UserControlBackgroundColor.R, faceColor.UserControlBackgroundColor.G, faceColor.UserControlBackgroundColor.B); Properties.Settings.Default.Save(); } catch (Exception ex) { // 保存主題顏色到 App.Properties 失敗 } } }
存取出主題顏色到 XML 文件。
namespace XX.Helper.FaceColor { /// <summary> /// 取出資源中 FaceColor 對象的顏色,進行查看、修改、保存操做 /// 其中修改成 實時更新資源 /// 保存爲 保存到 Property 或 xml 文件或 數據庫 /// </summary> public class FaceColorPickerViewModel { public TItleAndColor BackColor { get; } = new TItleAndColor() { Title = "主界面背景色" }; public TItleAndColor TopBackColor { get; } = new TItleAndColor() { Title = "頂部區域背景色" }; public TItleAndColor TopStrokeColor { get; } = new TItleAndColor() { Title = "頂部區域的邊框色" }; public TItleAndColor UCBackColor { get; } = new TItleAndColor() { Title = "數據框背景色" }; public TItleAndColor UCStrokeColor { get; } = new TItleAndColor() { Title = "數據框邊角顏色" }; public FaceColorPickerViewModel() { InitColor(); } /// <summary> /// 初始化顏色 /// </summary> private void InitColor() { FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (faceColor == null) return; BackColor.Color = faceColor.BackgroundColor; TopBackColor.Color = faceColor.TopBackgroundColor; TopStrokeColor.Color = faceColor.TopStrokeColor; UCBackColor.Color = faceColor.UserControlBackgroundColor; UCStrokeColor.Color = faceColor.UserControlStrokeColor; } /// <summary> /// 將選擇好的顏色,更新到資源中的 CommonFaceColor 對象 /// </summary> public void RefreshColorOfResource() { FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (faceColor == null) return; // 將資源的原有顏色存檔,用於恢復 CareTaker.AddMemento( "FaceColor", new FaceColorMemento( faceColor.BackgroundColor, faceColor.TopBackgroundColor, faceColor.TopStrokeColor, faceColor.UserControlBackgroundColor, faceColor.UserControlStrokeColor) ); // 將最新修改的顏色更新帶資源顏色 faceColor.BackgroundColor = BackColor.Color; faceColor.TopBackgroundColor = TopBackColor.Color; faceColor.TopStrokeColor = TopStrokeColor.Color; faceColor.UserControlStrokeColor = UCStrokeColor.Color; faceColor.UserControlBackgroundColor = UCBackColor.Color; } /// <summary> /// 將出廠默認的顏色,更新到資源中的 CommonFaceColor 對象 /// </summary> public void RecoverFaceColorToDefault() { FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (faceColor == null) return; // 將資源的原有顏色存檔,用於恢復 CareTaker.AddMemento( "FaceColor", new FaceColorMemento( faceColor.BackgroundColor, faceColor.TopBackgroundColor, faceColor.TopStrokeColor, faceColor.UserControlBackgroundColor, faceColor.UserControlStrokeColor) ); // 將最新修改的顏色更新帶資源顏色 faceColor.BackgroundColor = DefaultColorForFace.BackgroundColor; faceColor.TopBackgroundColor = DefaultColorForFace.TopBackgroundColor; faceColor.TopStrokeColor = DefaultColorForFace.TopStrokeColor; faceColor.UserControlStrokeColor = DefaultColorForFace.UserControlStrokeColor; faceColor.UserControlBackgroundColor = DefaultColorForFace.UserControlBackgroundColor; } /// <summary> /// 將資源中的顏色恢復成存檔的顏色 /// </summary> public void RecoverFaceColor() { FaceColorMemento memento = (FaceColorMemento)CareTaker.GetMemento("FaceColor"); FaceColor faceColor = (FaceColor)App.Current.TryFindResource("FaceColor"); if (memento == null || faceColor == null) return; faceColor.BackgroundColor = memento.BackColor; faceColor.TopBackgroundColor = memento.TopBackColor; faceColor.TopStrokeColor = memento.TopStrokeColor; faceColor.UserControlStrokeColor = memento.UCStrokeColor; faceColor.UserControlBackgroundColor = memento.UCBackColor; } /// <summary> /// 將自定義的顏色永久保存到倉庫(property、xml 文件、數據庫等中的一個方式) /// </summary> public void SaveFaceColorToWareHouse() { // 將自定義的資源顏色保存起來 FaceColorHelper.SaveColor(); } } public class TItleAndColor : DependencyObject { /// <summary> /// 標題 /// </summary> public string Title { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TItleAndColor), new PropertyMetadata(string.Empty)); /// <summary> /// 顏色 /// </summary> public Color Color { get { return (Color)GetValue(ColorProperty); } set { SetValue(ColorProperty, value); } } public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(TItleAndColor), new PropertyMetadata(Colors.LightSkyBlue)); } }
<local:BaseWindow x:Class="XX.Helper.FaceColor.FaceColorPickerWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:XX" xmlns:colorPickerWpf="clr-namespace:ColorPickerWPF;assembly=ColorPickerWPF" mc:Ignorable="d" Title="FaceColorPickerWindow" Height="720" Width="721" WindowStartupLocation="CenterOwner"> <Grid PreviewMouseLeftButtonUp="Grid_PreviewMouseLeftButtonUp" Background="#FFCFCFCF"> <Grid.RowDefinitions> <RowDefinition Height="0"/> <RowDefinition Height="auto"/> <RowDefinition Height="10"/> <RowDefinition Height="auto"/> <RowDefinition Height="20"/> <RowDefinition Height="auto"/> <RowDefinition Height="20"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="10"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="10"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="1" Grid.Row="1" Text="自定義換膚設置" HorizontalAlignment="Center" Style="{StaticResource ThirdTextBlockStyle}" Foreground="Black"/> <StackPanel Grid.Row="3" Grid.Column="1"> <StackPanel.Resources> <ControlTemplate TargetType="ContentControl" x:Key="ColorPickerTemplate"> <Grid Margin="0 0 0 3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="10" /> <ColumnDefinition Width="100" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="10" /> <ColumnDefinition Width="65" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="1" Text="{Binding Title}" VerticalAlignment="Center"/> <Rectangle Grid.Column="3" x:Name="PickColorRectangle" Stroke="Black" Width="45" Height="20"> <Rectangle.Fill> <SolidColorBrush Color="{Binding Color}"/> </Rectangle.Fill> </Rectangle> <Button Grid.Column="5" x:Name="PickColorButton" Content="調色" FontSize="13" Foreground="#FFEFEFFF" Style="{StaticResource ChangeImageBackgroundLongButtonStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="28"/> </Grid> </ControlTemplate> </StackPanel.Resources> <ContentControl Template="{StaticResource ColorPickerTemplate}" DataContext="{Binding BackColor}"/> <ContentControl Template="{StaticResource ColorPickerTemplate}" DataContext="{Binding TopBackColor}"/> <ContentControl Template="{StaticResource ColorPickerTemplate}" DataContext="{Binding TopStrokeColor}"/> <ContentControl Template="{StaticResource ColorPickerTemplate}" DataContext="{Binding UCBackColor}"/> <ContentControl Template="{StaticResource ColorPickerTemplate}" DataContext="{Binding UCStrokeColor}"/> </StackPanel> <colorPickerWpf:ColorPickerControl x:Name="ColorPicker" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Stretch" MinHeight="372" Width="600" OnPickColor="ColorPicker_OnPickColor"> <colorPickerWpf:ColorPickerControl.Resources> <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}"> <Setter Property="FontSize" Value="13"/> </Style> </colorPickerWpf:ColorPickerControl.Resources> </colorPickerWpf:ColorPickerControl> <StackPanel Grid.Column="1" Grid.Row="7" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="應用" Click="Button_Click" Style="{StaticResource ChangeImageBackgroundButtonStyle}" Height="35" Width="90"/> <Button Content="取消" Click="Button_Click_Cancel" Style="{StaticResource ChangeImageBackgroundButtonStyle}" Height="35" Width="90" Margin="10 0 0 0"/> <Button Content="默認" Click="Button_Click_Default" Style="{StaticResource ChangeImageBackgroundButtonStyle}" Height="35" Width="90" Margin="10 0 0 0"/> </StackPanel> </Grid> </local:BaseWindow> using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using ColorPickerWPF; namespace XX.Helper.FaceColor { /// <summary> /// FaceColorPickerWindow.xaml 的交互邏輯 /// </summary> /// <summary> /// FaceColorWindow.xaml 的交互邏輯 /// </summary> public partial class FaceColorPickerWindow : BaseWindow { double _originalWidth; double _originalHeight; double _originalOpacity; double _reduceWidth = 200; double _reduceHeight = 200; double _reduceOpacity = 0.5; public FaceColorPickerWindow() { InitializeComponent(); _originalWidth = this.Width; _originalHeight = this.Height; _originalOpacity = this.Opacity; } ContentControl _selectedControl; /// <summary> /// 鼠標左鍵彈起事件 /// 1. 若是點擊的是 切換顏色 按鈕,則記錄該按鈕對應的 ContentControl 到 _selectedControl 屬性; /// 2. 若是點擊顏色選擇器 ColorPickerControl,則返回 /// 3. 若是點擊其餘部位,則取消 _selectedControl 的記錄。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { object source = e.Source; if (e.Source.GetType() == typeof(ColorPickerControl)) return; if ((e.OriginalSource.GetType() == typeof(Button) && (e.OriginalSource as Button).Name == "PickColorButton") || (e.OriginalSource.GetType() == typeof(Rectangle) && (e.OriginalSource as Rectangle).Name == "PickColorRectangle")) { _selectedControl = e.Source as ContentControl; } else { _selectedControl = null; } } /// <summary> /// 顏色選擇器選擇顏色時 /// </summary> /// <param name="color"></param> private void ColorPicker_OnPickColor(Color color) { if (_selectedControl == null) return; // 前端這裏扯到 DataContext 彷佛有一點壞味道 (_selectedControl.DataContext as TItleAndColor).Color = color; } private void Button_Click(object sender, RoutedEventArgs e) { ReduceWindow(); // 將選擇好的顏色,更新到資源中的 CommonFaceColor 對象 (this.DataContext as FaceColorPickerViewModel)?.RefreshColorOfResource(); UpdateOrRecover(); RecoverWindow(); } private void Button_Click_Cancel(object sender, RoutedEventArgs e) { this.Close(); } private void Button_Click_Default(object sender, RoutedEventArgs e) { ReduceWindow(); // 將選擇好的顏色,更新到資源中的 CommonFaceColor 對象 (this.DataContext as FaceColorPickerViewModel)?.RecoverFaceColorToDefault(); UpdateOrRecover(); RecoverWindow(); } private void ReduceWindow() { this.Width = _reduceWidth; this.Height = _reduceHeight; this.Opacity = _reduceOpacity; } private void UpdateOrRecover() { if (MessageWindow.ShowMsg("是否保留這些設置?") == true) { // 將資源顏色保存到倉庫 (this.DataContext as FaceColorPickerViewModel)?.SaveFaceColorToWareHouse(); } else { // 撤銷對資源顏色的修改 (this.DataContext as FaceColorPickerViewModel)?.RecoverFaceColor(); } } private void RecoverWindow() { this.Width = _originalWidth; this.Height = _originalHeight; this.Opacity = _originalOpacity; } } }
存檔管理
namespace XX.Helper.Memento { /// <summary> /// 存檔管理 /// </summary> public class CareTaker { static Dictionary<string, IMemento> MementoDic = new Dictionary<string, IMemento>(); public static IMemento GetMemento(string key) { IMemento result = null; if (MementoDic.ContainsKey(key)) { result = (IMemento)MementoDic[key]; } return result; } public static void AddMemento(string key, IMemento memento) { if (MementoDic.ContainsKey(key)) { MementoDic[key] = memento; } else { MementoDic.Add(key, memento); } } } }
檔案。
namespace XX.Helper.Memento { /// <summary> /// 顏色存檔 /// </summary> public class FaceColorMemento : IMemento { public Color BackColor { get; set; } public Color TopBackColor { get; set; } public Color TopStrokeColor { get; set; } public Color UCBackColor { get; set; } public Color UCStrokeColor { get; set; } public FaceColorMemento(Color backColor, Color topBackColor, Color topStrokeColor, Color uCBackColor, Color uCStrokeColor) { this.BackColor = backColor; this.TopBackColor = topBackColor; this.TopStrokeColor = topStrokeColor; this.UCBackColor = uCBackColor; this.UCStrokeColor = uCStrokeColor; } } }