diff --git a/SunnyUI/Controls/UIControl.cs b/SunnyUI/Controls/UIControl.cs index 0a12f266..ec2c43c6 100644 --- a/SunnyUI/Controls/UIControl.cs +++ b/SunnyUI/Controls/UIControl.cs @@ -24,6 +24,7 @@ * 2022-03-19: V3.1.1 重构主题配色 * 2023-02-03: V3.3.1 增加WIN10系统响应触摸屏的按下和弹起事件 * 2023-05-12: V3.3.6 重构DrawString函数 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System; @@ -268,12 +269,12 @@ namespace Sunny.UI get; } - protected UIStyle _style = UIStyle.Blue; + protected UIStyle _style = UIStyle.Inherited; /// /// 主题样式 /// - [DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")] + [DefaultValue(UIStyle.Inherited), Description("主题样式"), Category("SunnyUI")] public UIStyle Style { get => _style; @@ -282,7 +283,6 @@ namespace Sunny.UI protected void SetStyleCustom(bool needRefresh = true) { - _style = UIStyle.Custom; if (needRefresh) Invalidate(); } @@ -298,7 +298,13 @@ namespace Sunny.UI Invalidate(); } - _style = style; + _style = style == UIStyle.Inherited ? UIStyle.Inherited : UIStyle.Custom; + } + + public void SetInheritedStyle(UIStyle style) + { + SetStyle(style); + _style = UIStyle.Inherited; } protected bool styleCustomMode = false; diff --git a/SunnyUI/Controls/UIFlowLayoutPanel.cs b/SunnyUI/Controls/UIFlowLayoutPanel.cs index b2a5e84e..efcdf67d 100644 --- a/SunnyUI/Controls/UIFlowLayoutPanel.cs +++ b/SunnyUI/Controls/UIFlowLayoutPanel.cs @@ -28,6 +28,7 @@ * 2022-11-25: V3.2.9 增加Get方法以获取控件 * 2023-01-11: V3.3.1 增加AutoScroll属性 * 2023-01-11: V3.3.1 修复只显示水平滚动条时,鼠标滚轮滚动水平滚动条不动的问题 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System; @@ -216,7 +217,7 @@ namespace Sunny.UI } } - if (Panel != null && !IsDesignMode) + if (Panel != null && !DesignMode) { Add(e.Control); } diff --git a/SunnyUI/Controls/UIUserControl.cs b/SunnyUI/Controls/UIUserControl.cs index 39db4806..c6f73a28 100644 --- a/SunnyUI/Controls/UIUserControl.cs +++ b/SunnyUI/Controls/UIUserControl.cs @@ -20,6 +20,7 @@ * 2022-04-02: V3.1.2 默认设置AutoScaleMode为None * 2023-05-12: V3.3.6 重构DrawString函数 * 2023-07-02: V3.3.9 渐变色增加方向选择 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System; @@ -150,22 +151,22 @@ namespace Sunny.UI } } - protected bool IsDesignMode - { - get - { - if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) - { - return true; - } - else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") - { - return true; - } - - return false; - } - } + //protected bool IsDesignMode + //{ + // get + // { + // if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) + // { + // return true; + // } + // else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") + // { + // return true; + // } + // + // return false; + // } + //} private ToolStripStatusLabelBorderSides _rectSides = ToolStripStatusLabelBorderSides.All; diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index 11622de2..603ef6f9 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -49,6 +49,7 @@ * 2023-07-24: V3.4.1 修复页面切换时,第一个UIPage未执行Final事件的问题 * 2023-07-27: V3.4.1 默认提示弹窗TopMost为true * 2023-10-09: V3.5.0 增加一个在窗体显示后延时执行的事件 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System; @@ -71,13 +72,9 @@ namespace Sunny.UI base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;//设置最大化尺寸 InitializeComponent(); - if (this.Register()) - { - SetStyle(UIStyles.Style); - } + this.Register(); - SetStyle( - ControlStyles.UserPaint | + SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | @@ -94,7 +91,7 @@ namespace Sunny.UI controlBoxFillHoverColor = UIStyles.Blue.FormControlBoxFillHoverColor; ControlBoxCloseFillHoverColor = UIStyles.Blue.FormControlBoxCloseFillHoverColor; rectColor = UIStyles.Blue.FormRectColor; - foreColor = UIStyles.Blue.FormForeColor; + ForeColor = UIStyles.Blue.FormForeColor; BackColor = UIStyles.Blue.FormBackColor; titleColor = UIStyles.Blue.FormTitleColor; titleForeColor = UIStyles.Blue.FormTitleForeColor; @@ -310,33 +307,9 @@ namespace Sunny.UI } } - public void Render() - { - SetStyle(UIStyles.Style); - } - - protected override void OnBackColorChanged(EventArgs e) - { - base.OnBackColorChanged(e); - AfterSetFillColor(BackColor); - _style = UIStyle.Custom; - } - - protected virtual void AfterSetFillColor(Color color) - { - } - protected override void OnControlAdded(ControlEventArgs e) { base.OnControlAdded(e); - - if (e.Control is IStyleInterface ctrl) - { - if (!ctrl.StyleCustomMode) ctrl.Style = Style; - } - - UIStyleHelper.SetRawControlStyle(e, Style); - if (ShowTitle && !AllowAddControlOnTitle && e.Control.Top < TitleHeight) { e.Control.Top = Padding.Top; @@ -514,7 +487,7 @@ namespace Sunny.UI if (titleColor != value) { titleColor = value; - SetStyleCustom(); + Invalidate(); } } } @@ -536,7 +509,7 @@ namespace Sunny.UI if (titleForeColor != value) { titleForeColor = value; - SetStyleCustom(); + Invalidate(); } } } @@ -632,29 +605,8 @@ namespace Sunny.UI } } - protected Color foreColor; - protected Color rectColor; - /// - /// 填充颜色,当值为背景色或透明色或空值则不填充 - /// - [Description("背景颜色"), Category("SunnyUI")] - [DefaultValue(typeof(Color), "48, 48, 48")] - public override Color ForeColor - { - get => foreColor; - set - { - if (foreColor != value) - { - foreColor = value; - AfterSetForeColor(ForeColor); - SetStyleCustom(); - } - } - } - /// /// 边框颜色 /// @@ -668,7 +620,7 @@ namespace Sunny.UI rectColor = value; AfterSetRectColor(value); RectColorChanged?.Invoke(this, EventArgs.Empty); - SetStyleCustom(); + Invalidate(); } } @@ -687,17 +639,6 @@ namespace Sunny.UI InControlBox = false; Close(); } - //else - //{ - // if (ControlBox && WindowState == FormWindowState.Maximized) - // { - // if (MousePosition.X > ControlBoxRect.X) - // { - // InControlBox = false; - // Close(); - // } - // } - //} if (InMinBox) { @@ -1267,19 +1208,53 @@ namespace Sunny.UI Invalidate(); } - protected UIStyle _style = UIStyle.Blue; + protected UIStyle _style = UIStyle.Inherited; /// /// 配色主题 /// [Description("配色主题"), Category("SunnyUI")] - [DefaultValue(UIStyle.Blue)] + [DefaultValue(UIStyle.Inherited)] public UIStyle Style { get => _style; set => SetStyle(value); } + public void SetInheritedStyle(UIStyle style) + { + if (!DesignMode) + { + this.SuspendLayout(); + UIStyleHelper.SetChildUIStyle(this, style); + + if (_style == UIStyle.Inherited && style.IsValid()) + { + SetStyleColor(style.Colors()); + Invalidate(); + _style = UIStyle.Inherited; + } + + UIStyleChanged?.Invoke(this, new EventArgs()); + this.ResumeLayout(); + } + } + + public void SetStyle(UIStyle style) + { + this.SuspendLayout(); + + if (!style.IsCustom()) + { + SetStyleColor(style.Colors()); + Invalidate(); + } + + _style = style == UIStyle.Inherited ? UIStyle.Inherited : UIStyle.Custom; + UIStyleChanged?.Invoke(this, new EventArgs()); + this.ResumeLayout(); + } + [Description("自定义主题模式(开启后全局主题更改将对当前窗体无效)"), Category("SunnyUI")] [DefaultValue(false)] public bool StyleCustomMode @@ -1300,7 +1275,6 @@ namespace Sunny.UI if (controlBoxForeColor != value) { controlBoxForeColor = value; - _style = UIStyle.Custom; Invalidate(); } } @@ -1319,7 +1293,6 @@ namespace Sunny.UI if (ControlBoxFillHoverColor != value) { controlBoxFillHoverColor = value; - _style = UIStyle.Custom; Invalidate(); } } @@ -1343,22 +1316,6 @@ namespace Sunny.UI } } - public void SetStyle(UIStyle style) - { - this.SuspendLayout(); - UIStyleHelper.SetChildUIStyle(this, style); - - if (!style.IsCustom()) - { - SetStyleColor(style.Colors()); - Invalidate(); - } - - _style = style; - UIStyleChanged?.Invoke(this, new EventArgs()); - this.ResumeLayout(); - } - public event EventHandler UIStyleChanged; public virtual void SetStyleColor(UIBaseStyle uiColor) @@ -1367,18 +1324,12 @@ namespace Sunny.UI controlBoxFillHoverColor = uiColor.FormControlBoxFillHoverColor; ControlBoxCloseFillHoverColor = uiColor.FormControlBoxCloseFillHoverColor; rectColor = uiColor.FormRectColor; - foreColor = uiColor.FormForeColor; + ForeColor = uiColor.FormForeColor; BackColor = uiColor.FormBackColor; titleColor = uiColor.FormTitleColor; titleForeColor = uiColor.FormTitleForeColor; } - protected void SetStyleCustom(bool needRefresh = true) - { - _style = UIStyle.Custom; - if (needRefresh) Invalidate(); - } - protected override void OnLocationChanged(EventArgs e) { base.OnLocationChanged(e); @@ -1423,11 +1374,19 @@ namespace Sunny.UI private System.Windows.Forms.Timer AfterShownTimer; public event EventHandler AfterShown; + public void Render() + { + if (!DesignMode && UIStyles.Style.IsValid()) + { + Style = UIStyles.Style; + } + } + protected override void OnShown(EventArgs e) { base.OnShown(e); if (AutoScaleMode == AutoScaleMode.Font) AutoScaleMode = AutoScaleMode.None; - + Render(); CalcSystemBoxPos(); SetRadius(); IsShown = true; diff --git a/SunnyUI/Style/UIStyle.cs b/SunnyUI/Style/UIStyle.cs index d1766db3..19bef74a 100644 --- a/SunnyUI/Style/UIStyle.cs +++ b/SunnyUI/Style/UIStyle.cs @@ -21,6 +21,7 @@ * 2021-07-18: V3.0.5 增加多彩主题,以颜色深色,文字白色为主 * 2021-09-24: V3.0.7 修改默认字体的GdiCharSet * 2021-10-16: V3.0.8 增加系统DPI缩放自适应 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System.Collections.Generic; @@ -52,6 +53,12 @@ namespace Sunny.UI /// public enum UIStyle { + /// + /// 继承的全局主题 + /// + [DisplayText("继承的全局主题")] + Inherited = -1, + /// /// 自定义 /// @@ -385,17 +392,7 @@ namespace Sunny.UI public static bool IsValid(this UIStyle style) { - return !style.IsCustom(); - } - - public static bool IsCustom(this UIBaseStyle style) - { - return style.Name.IsCustom(); - } - - public static bool IsValid(this UIBaseStyle style) - { - return !style.IsCustom(); + return (int)style > 0; } public static void SetChildUIStyle(Control ctrl, UIStyle style) @@ -405,9 +402,17 @@ namespace Sunny.UI { if (control is IStyleInterface item) { - if (!item.StyleCustomMode) + if (item.Style == UIStyle.Inherited) { - item.Style = style; + if (item is UIButton ctrl1) + { + ctrl1.SetInheritedStyle(style); + } + else + { + item.Style = style; + item.Style = UIStyle.Inherited; + } } } } @@ -418,7 +423,7 @@ namespace Sunny.UI if (info.FieldType.Name == "UIContextMenuStrip") { UIContextMenuStrip context = (UIContextMenuStrip)info.GetValue(ctrl); - if (context != null && !context.StyleCustomMode) + if (context != null && context.Style == UIStyle.Inherited) { context.SetStyle(style); } diff --git a/SunnyUI/Style/UIStyleColor.cs b/SunnyUI/Style/UIStyleColor.cs index d9b61430..9ad64a64 100644 --- a/SunnyUI/Style/UIStyleColor.cs +++ b/SunnyUI/Style/UIStyleColor.cs @@ -19,6 +19,7 @@ * 2020-01-01: V2.2.0 增加文件说明 * 2020-04-25: V2.2.4 更新主题配置类 * 2022-03-19: V3.1.1 重构主题配色 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System.Drawing; @@ -450,6 +451,14 @@ namespace Sunny.UI public override UIStyle Name => UIStyle.Custom; } + public class UIInheritedStyle : UIBaseStyle + { + public UIInheritedStyle() + { + base.Init(UIColor.Blue, UIStyle.Inherited, Color.White, UIFontColor.Primary); + } + } + public class UIBlueStyle : UIBaseStyle { public UIBlueStyle() diff --git a/SunnyUI/Style/UIStyleManager.cs b/SunnyUI/Style/UIStyleManager.cs index 05c0a38a..1ddabfcd 100644 --- a/SunnyUI/Style/UIStyleManager.cs +++ b/SunnyUI/Style/UIStyleManager.cs @@ -18,6 +18,7 @@ * * 2020-01-01: V2.2.0 增加文件说明 * 2021-10-16: V3.0.8 增加系统DPI缩放自适应 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System.ComponentModel; @@ -30,30 +31,6 @@ namespace Sunny.UI /// public class UIStyleManager : Component { - /// - /// 主题样式 - /// - [DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")] - public UIStyle Style - { - get => UIStyles.Style; - set - { - if (UIStyles.Style != value && value != UIStyle.Custom) - { - UIStyles.SetStyle(value); - } - } - } - - public void Render() - { - if (Style != UIStyle.Custom) - { - UIStyles.SetStyle(Style); - } - } - /// /// 构造函数 /// diff --git a/SunnyUI/Style/UIStyles.cs b/SunnyUI/Style/UIStyles.cs index 260785ed..e69cdc82 100644 --- a/SunnyUI/Style/UIStyles.cs +++ b/SunnyUI/Style/UIStyles.cs @@ -21,7 +21,8 @@ * 2021-07-18: V3.0.5 增加多彩主题,以颜色深色,文字白色为主 * 2021-09-24: V3.0.7 修改默认字体的GdiCharSet * 2021-10-16: V3.0.8 增加系统DPI缩放自适应 - * 2023-08-28: V3.4.2 修改全局字体为系统默认:System.Drawing.SystemFonts.DefaultFont + * 2023-08-28: V3.4.2 修改全局字体为系统默认字体 + * 2023-11-05: V3.5.2 重构主题 ******************************************************************************/ using System; @@ -145,6 +146,8 @@ namespace Sunny.UI return styles; } + public static readonly UIBaseStyle Inherited = new UIInheritedStyle(); + /// /// 自定义 /// @@ -213,6 +216,7 @@ namespace Sunny.UI static UIStyles() { + AddStyle(Inherited); AddStyle(Custom); AddStyle(Blue); AddStyle(Orange); @@ -382,7 +386,7 @@ namespace Sunny.UI /// /// 主题样式 /// - public static UIStyle Style { get; private set; } = UIStyle.Blue; + public static UIStyle Style { get; private set; } = UIStyle.Inherited; /// /// 设置主题样式 @@ -394,7 +398,7 @@ namespace Sunny.UI foreach (var form in Forms.Values) { - form.Style = style; + form.SetInheritedStyle(style); } foreach (var page in Pages.Values) @@ -403,6 +407,11 @@ namespace Sunny.UI } } + public static void Render() + { + SetStyle(Style); + } + public static void SetDPIScale() { foreach (var form in Forms.Values)