* 重构主题

This commit is contained in:
Sunny 2023-11-05 22:43:04 +08:00
parent bd87f7f6ac
commit 42ade126ad
8 changed files with 123 additions and 156 deletions

View File

@ -24,6 +24,7 @@
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
* 2023-02-03: V3.3.1 WIN10系统响应触摸屏的按下和弹起事件 * 2023-02-03: V3.3.1 WIN10系统响应触摸屏的按下和弹起事件
* 2023-05-12: V3.3.6 DrawString函数 * 2023-05-12: V3.3.6 DrawString函数
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -268,12 +269,12 @@ namespace Sunny.UI
get; get;
} }
protected UIStyle _style = UIStyle.Blue; protected UIStyle _style = UIStyle.Inherited;
/// <summary> /// <summary>
/// 主题样式 /// 主题样式
/// </summary> /// </summary>
[DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")] [DefaultValue(UIStyle.Inherited), Description("主题样式"), Category("SunnyUI")]
public UIStyle Style public UIStyle Style
{ {
get => _style; get => _style;
@ -282,7 +283,6 @@ namespace Sunny.UI
protected void SetStyleCustom(bool needRefresh = true) protected void SetStyleCustom(bool needRefresh = true)
{ {
_style = UIStyle.Custom;
if (needRefresh) Invalidate(); if (needRefresh) Invalidate();
} }
@ -298,7 +298,13 @@ namespace Sunny.UI
Invalidate(); 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; protected bool styleCustomMode = false;

View File

@ -28,6 +28,7 @@
* 2022-11-25: V3.2.9 Get方法以获取控件 * 2022-11-25: V3.2.9 Get方法以获取控件
* 2023-01-11: V3.3.1 AutoScroll属性 * 2023-01-11: V3.3.1 AutoScroll属性
* 2023-01-11: V3.3.1 * 2023-01-11: V3.3.1
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -216,7 +217,7 @@ namespace Sunny.UI
} }
} }
if (Panel != null && !IsDesignMode) if (Panel != null && !DesignMode)
{ {
Add(e.Control); Add(e.Control);
} }

View File

@ -20,6 +20,7 @@
* 2022-04-02: V3.1.2 AutoScaleMode为None * 2022-04-02: V3.1.2 AutoScaleMode为None
* 2023-05-12: V3.3.6 DrawString函数 * 2023-05-12: V3.3.6 DrawString函数
* 2023-07-02: V3.3.9 * 2023-07-02: V3.3.9
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -150,22 +151,22 @@ namespace Sunny.UI
} }
} }
protected bool IsDesignMode //protected bool IsDesignMode
{ //{
get // get
{ // {
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) // if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{ // {
return true; // return true;
} // }
else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") // else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
{ // {
return true; // return true;
} // }
//
return false; // return false;
} // }
} //}
private ToolStripStatusLabelBorderSides _rectSides = ToolStripStatusLabelBorderSides.All; private ToolStripStatusLabelBorderSides _rectSides = ToolStripStatusLabelBorderSides.All;

View File

@ -49,6 +49,7 @@
* 2023-07-24: V3.4.1 UIPage未执行Final事件的问题 * 2023-07-24: V3.4.1 UIPage未执行Final事件的问题
* 2023-07-27: V3.4.1 TopMost为true * 2023-07-27: V3.4.1 TopMost为true
* 2023-10-09: V3.5.0 * 2023-10-09: V3.5.0
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -71,13 +72,9 @@ namespace Sunny.UI
base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;//设置最大化尺寸 base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;//设置最大化尺寸
InitializeComponent(); InitializeComponent();
if (this.Register()) this.Register();
{
SetStyle(UIStyles.Style);
}
SetStyle( SetStyle(ControlStyles.UserPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer | ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer | ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint | ControlStyles.AllPaintingInWmPaint |
@ -94,7 +91,7 @@ namespace Sunny.UI
controlBoxFillHoverColor = UIStyles.Blue.FormControlBoxFillHoverColor; controlBoxFillHoverColor = UIStyles.Blue.FormControlBoxFillHoverColor;
ControlBoxCloseFillHoverColor = UIStyles.Blue.FormControlBoxCloseFillHoverColor; ControlBoxCloseFillHoverColor = UIStyles.Blue.FormControlBoxCloseFillHoverColor;
rectColor = UIStyles.Blue.FormRectColor; rectColor = UIStyles.Blue.FormRectColor;
foreColor = UIStyles.Blue.FormForeColor; ForeColor = UIStyles.Blue.FormForeColor;
BackColor = UIStyles.Blue.FormBackColor; BackColor = UIStyles.Blue.FormBackColor;
titleColor = UIStyles.Blue.FormTitleColor; titleColor = UIStyles.Blue.FormTitleColor;
titleForeColor = UIStyles.Blue.FormTitleForeColor; 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) protected override void OnControlAdded(ControlEventArgs e)
{ {
base.OnControlAdded(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) if (ShowTitle && !AllowAddControlOnTitle && e.Control.Top < TitleHeight)
{ {
e.Control.Top = Padding.Top; e.Control.Top = Padding.Top;
@ -514,7 +487,7 @@ namespace Sunny.UI
if (titleColor != value) if (titleColor != value)
{ {
titleColor = value; titleColor = value;
SetStyleCustom(); Invalidate();
} }
} }
} }
@ -536,7 +509,7 @@ namespace Sunny.UI
if (titleForeColor != value) if (titleForeColor != value)
{ {
titleForeColor = value; titleForeColor = value;
SetStyleCustom(); Invalidate();
} }
} }
} }
@ -632,29 +605,8 @@ namespace Sunny.UI
} }
} }
protected Color foreColor;
protected Color rectColor; protected Color rectColor;
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("背景颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "48, 48, 48")]
public override Color ForeColor
{
get => foreColor;
set
{
if (foreColor != value)
{
foreColor = value;
AfterSetForeColor(ForeColor);
SetStyleCustom();
}
}
}
/// <summary> /// <summary>
/// 边框颜色 /// 边框颜色
/// </summary> /// </summary>
@ -668,7 +620,7 @@ namespace Sunny.UI
rectColor = value; rectColor = value;
AfterSetRectColor(value); AfterSetRectColor(value);
RectColorChanged?.Invoke(this, EventArgs.Empty); RectColorChanged?.Invoke(this, EventArgs.Empty);
SetStyleCustom(); Invalidate();
} }
} }
@ -687,17 +639,6 @@ namespace Sunny.UI
InControlBox = false; InControlBox = false;
Close(); Close();
} }
//else
//{
// if (ControlBox && WindowState == FormWindowState.Maximized)
// {
// if (MousePosition.X > ControlBoxRect.X)
// {
// InControlBox = false;
// Close();
// }
// }
//}
if (InMinBox) if (InMinBox)
{ {
@ -1267,19 +1208,53 @@ namespace Sunny.UI
Invalidate(); Invalidate();
} }
protected UIStyle _style = UIStyle.Blue; protected UIStyle _style = UIStyle.Inherited;
/// <summary> /// <summary>
/// 配色主题 /// 配色主题
/// </summary> /// </summary>
[Description("配色主题"), Category("SunnyUI")] [Description("配色主题"), Category("SunnyUI")]
[DefaultValue(UIStyle.Blue)] [DefaultValue(UIStyle.Inherited)]
public UIStyle Style public UIStyle Style
{ {
get => _style; get => _style;
set => SetStyle(value); 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")] [Description("自定义主题模式(开启后全局主题更改将对当前窗体无效)"), Category("SunnyUI")]
[DefaultValue(false)] [DefaultValue(false)]
public bool StyleCustomMode public bool StyleCustomMode
@ -1300,7 +1275,6 @@ namespace Sunny.UI
if (controlBoxForeColor != value) if (controlBoxForeColor != value)
{ {
controlBoxForeColor = value; controlBoxForeColor = value;
_style = UIStyle.Custom;
Invalidate(); Invalidate();
} }
} }
@ -1319,7 +1293,6 @@ namespace Sunny.UI
if (ControlBoxFillHoverColor != value) if (ControlBoxFillHoverColor != value)
{ {
controlBoxFillHoverColor = value; controlBoxFillHoverColor = value;
_style = UIStyle.Custom;
Invalidate(); 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 event EventHandler UIStyleChanged;
public virtual void SetStyleColor(UIBaseStyle uiColor) public virtual void SetStyleColor(UIBaseStyle uiColor)
@ -1367,18 +1324,12 @@ namespace Sunny.UI
controlBoxFillHoverColor = uiColor.FormControlBoxFillHoverColor; controlBoxFillHoverColor = uiColor.FormControlBoxFillHoverColor;
ControlBoxCloseFillHoverColor = uiColor.FormControlBoxCloseFillHoverColor; ControlBoxCloseFillHoverColor = uiColor.FormControlBoxCloseFillHoverColor;
rectColor = uiColor.FormRectColor; rectColor = uiColor.FormRectColor;
foreColor = uiColor.FormForeColor; ForeColor = uiColor.FormForeColor;
BackColor = uiColor.FormBackColor; BackColor = uiColor.FormBackColor;
titleColor = uiColor.FormTitleColor; titleColor = uiColor.FormTitleColor;
titleForeColor = uiColor.FormTitleForeColor; titleForeColor = uiColor.FormTitleForeColor;
} }
protected void SetStyleCustom(bool needRefresh = true)
{
_style = UIStyle.Custom;
if (needRefresh) Invalidate();
}
protected override void OnLocationChanged(EventArgs e) protected override void OnLocationChanged(EventArgs e)
{ {
base.OnLocationChanged(e); base.OnLocationChanged(e);
@ -1423,11 +1374,19 @@ namespace Sunny.UI
private System.Windows.Forms.Timer AfterShownTimer; private System.Windows.Forms.Timer AfterShownTimer;
public event EventHandler AfterShown; public event EventHandler AfterShown;
public void Render()
{
if (!DesignMode && UIStyles.Style.IsValid())
{
Style = UIStyles.Style;
}
}
protected override void OnShown(EventArgs e) protected override void OnShown(EventArgs e)
{ {
base.OnShown(e); base.OnShown(e);
if (AutoScaleMode == AutoScaleMode.Font) AutoScaleMode = AutoScaleMode.None; if (AutoScaleMode == AutoScaleMode.Font) AutoScaleMode = AutoScaleMode.None;
Render();
CalcSystemBoxPos(); CalcSystemBoxPos();
SetRadius(); SetRadius();
IsShown = true; IsShown = true;

View File

@ -21,6 +21,7 @@
* 2021-07-18: V3.0.5 * 2021-07-18: V3.0.5
* 2021-09-24: V3.0.7 GdiCharSet * 2021-09-24: V3.0.7 GdiCharSet
* 2021-10-16: V3.0.8 DPI缩放自适应 * 2021-10-16: V3.0.8 DPI缩放自适应
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System.Collections.Generic; using System.Collections.Generic;
@ -52,6 +53,12 @@ namespace Sunny.UI
/// </summary> /// </summary>
public enum UIStyle public enum UIStyle
{ {
/// <summary>
/// 继承的全局主题
/// </summary>
[DisplayText("继承的全局主题")]
Inherited = -1,
/// <summary> /// <summary>
/// 自定义 /// 自定义
/// </summary> /// </summary>
@ -385,17 +392,7 @@ namespace Sunny.UI
public static bool IsValid(this UIStyle style) public static bool IsValid(this UIStyle style)
{ {
return !style.IsCustom(); return (int)style > 0;
}
public static bool IsCustom(this UIBaseStyle style)
{
return style.Name.IsCustom();
}
public static bool IsValid(this UIBaseStyle style)
{
return !style.IsCustom();
} }
public static void SetChildUIStyle(Control ctrl, UIStyle style) public static void SetChildUIStyle(Control ctrl, UIStyle style)
@ -405,9 +402,17 @@ namespace Sunny.UI
{ {
if (control is IStyleInterface item) 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") if (info.FieldType.Name == "UIContextMenuStrip")
{ {
UIContextMenuStrip context = (UIContextMenuStrip)info.GetValue(ctrl); UIContextMenuStrip context = (UIContextMenuStrip)info.GetValue(ctrl);
if (context != null && !context.StyleCustomMode) if (context != null && context.Style == UIStyle.Inherited)
{ {
context.SetStyle(style); context.SetStyle(style);
} }

View File

@ -19,6 +19,7 @@
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2020-04-25: V2.2.4 * 2020-04-25: V2.2.4
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System.Drawing; using System.Drawing;
@ -450,6 +451,14 @@ namespace Sunny.UI
public override UIStyle Name => UIStyle.Custom; 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 class UIBlueStyle : UIBaseStyle
{ {
public UIBlueStyle() public UIBlueStyle()

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2021-10-16: V3.0.8 DPI缩放自适应 * 2021-10-16: V3.0.8 DPI缩放自适应
* 2023-11-05: V3.5.2
******************************************************************************/ ******************************************************************************/
using System.ComponentModel; using System.ComponentModel;
@ -30,30 +31,6 @@ namespace Sunny.UI
/// </summary> /// </summary>
public class UIStyleManager : Component public class UIStyleManager : Component
{ {
/// <summary>
/// 主题样式
/// </summary>
[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);
}
}
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
/// </summary> /// </summary>

View File

@ -21,7 +21,8 @@
* 2021-07-18: V3.0.5 * 2021-07-18: V3.0.5
* 2021-09-24: V3.0.7 GdiCharSet * 2021-09-24: V3.0.7 GdiCharSet
* 2021-10-16: V3.0.8 DPI缩放自适应 * 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; using System;
@ -145,6 +146,8 @@ namespace Sunny.UI
return styles; return styles;
} }
public static readonly UIBaseStyle Inherited = new UIInheritedStyle();
/// <summary> /// <summary>
/// 自定义 /// 自定义
/// </summary> /// </summary>
@ -213,6 +216,7 @@ namespace Sunny.UI
static UIStyles() static UIStyles()
{ {
AddStyle(Inherited);
AddStyle(Custom); AddStyle(Custom);
AddStyle(Blue); AddStyle(Blue);
AddStyle(Orange); AddStyle(Orange);
@ -382,7 +386,7 @@ namespace Sunny.UI
/// <summary> /// <summary>
/// 主题样式 /// 主题样式
/// </summary> /// </summary>
public static UIStyle Style { get; private set; } = UIStyle.Blue; public static UIStyle Style { get; private set; } = UIStyle.Inherited;
/// <summary> /// <summary>
/// 设置主题样式 /// 设置主题样式
@ -394,7 +398,7 @@ namespace Sunny.UI
foreach (var form in Forms.Values) foreach (var form in Forms.Values)
{ {
form.Style = style; form.SetInheritedStyle(style);
} }
foreach (var page in Pages.Values) foreach (var page in Pages.Values)
@ -403,6 +407,11 @@ namespace Sunny.UI
} }
} }
public static void Render()
{
SetStyle(Style);
}
public static void SetDPIScale() public static void SetDPIScale()
{ {
foreach (var form in Forms.Values) foreach (var form in Forms.Values)