* UIStyle: 增加属性以期控件跟随窗体缩放

This commit is contained in:
Sunny 2022-04-07 16:13:17 +08:00
parent d11e4d4a5e
commit 696c36c9e9
30 changed files with 897 additions and 328 deletions

Binary file not shown.

View File

@ -33,6 +33,12 @@ namespace Sunny.UI
}
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -11,6 +11,12 @@ namespace Sunny.UI
private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter;
private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft;
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -35,7 +35,7 @@ namespace Sunny.UI
[DefaultEvent("Click")]
[DefaultProperty("Symbol")]
[ToolboxItem(true)]
public sealed class UIAvatar : UIControl, ISymbol
public sealed class UIAvatar : UIControl, ISymbol, IControlScale
{
/// <summary>
/// 头像图标类型
@ -351,5 +351,10 @@ namespace Sunny.UI
PaintAgain?.Invoke(this, e);
}
public void SetControlScale(float scale)
{
throw new NotImplementedException();
}
}
}

View File

@ -42,6 +42,12 @@ namespace Sunny.UI
BackColor = UIStyles.Blue.ContextMenuColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }

View File

@ -49,6 +49,26 @@ namespace Sunny.UI
base.MinimumSize = new Size(1, 1);
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
protected bool selected;
[Browsable(false), DefaultValue(false)]

View File

@ -100,6 +100,26 @@ namespace Sunny.UI
HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -44,6 +44,26 @@ namespace Sunny.UI
private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
private Color foreColor = UIFontColor.Primary;
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
/// <summary>
/// 主题样式
/// </summary>

View File

@ -342,6 +342,12 @@ namespace Sunny.UI
{
private UIScrollBar bar;
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
/// <summary>
/// Tag字符串
/// </summary>

View File

@ -23,6 +23,7 @@
* 2022-03-19: V3.1.1
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
@ -42,6 +43,26 @@ namespace Sunny.UI
foreColor = UIStyles.Blue.LabelForeColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
private int angle;
[DefaultValue(0), Category("SunnyUI"), Description("居中时旋转角度")]
@ -167,6 +188,26 @@ namespace Sunny.UI
base.LinkColor = linkColor = UIColor.Blue;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -609,6 +609,12 @@ namespace Sunny.UI
{
private UIScrollBar bar;
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
/// <summary>
/// Tag字符串
/// </summary>

View File

@ -29,7 +29,7 @@ namespace Sunny.UI
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public class UIMarkLabel : UILabel
public sealed class UIMarkLabel : UILabel
{
public UIMarkLabel()
{

View File

@ -64,6 +64,26 @@ namespace Sunny.UI
selectedHighColor = UIStyles.Blue.NavBarMenuSelectedColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -47,6 +47,9 @@ namespace Sunny.UI
private readonly UIScrollBar Bar = new UIScrollBar();
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public UINavMenu()
{
SetStyle(ControlStyles.DoubleBuffer |
@ -87,6 +90,23 @@ namespace Sunny.UI
selectedHighColor = UIStyles.Blue.NavMenuMenuSelectedColor;
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Description("滚动条填充颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "56, 56, 56")]
public Color ScrollFillColor

View File

@ -334,30 +334,48 @@ namespace Sunny.UI
return CreateTabIfNotExists(new NavMenuItem(guid));
}
public void SelectPage(int pageIndex)
public bool SelectPage(int pageIndex)
{
if (pageIndex < 0) return;
if (pageIndex < 0)
{
return false;
}
foreach (var item in PageItems)
{
if (item.Value.PageIndex == pageIndex && item.Key != null)
{
if (tabControl.TabPages.Contains(item.Key))
{
tabControl.SelectTab(item.Key);
return true;
}
}
}
return false;
}
public void SelectPage(Guid guid)
public bool SelectPage(Guid guid)
{
if (guid == Guid.Empty) return;
if (guid == Guid.Empty)
{
return false;
}
foreach (var item in PageItems)
{
if (item.Value.PageGuid == guid && item.Key != null)
{
if (tabControl.TabPages.Contains(item.Key))
{
tabControl.SelectTab(item.Key);
return true;
}
}
}
return false;
}
public UIPage GetPage(int pageIndex)

View File

@ -32,7 +32,7 @@ namespace Sunny.UI
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public class UISmoothLabel : Label, IStyleInterface
public sealed class UISmoothLabel : Label, IStyleInterface
{
public UISmoothLabel()
{
@ -48,6 +48,9 @@ namespace Sunny.UI
Size = new Size(300, 60);
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
protected override void Dispose(bool disposing)
{
if (disposing)
@ -60,6 +63,23 @@ namespace Sunny.UI
base.Dispose(disposing);
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
private PointF point;
private SizeF drawSize;
private Pen drawPen;
@ -103,7 +123,7 @@ namespace Sunny.UI
}
}
protected void SetStyleCustom(bool needRefresh = true)
private void SetStyleCustom(bool needRefresh = true)
{
_style = UIStyle.Custom;
if (needRefresh) Invalidate();
@ -129,7 +149,7 @@ namespace Sunny.UI
[Description("获取或设置可以自定义主题风格"), Category("SunnyUI")]
public bool StyleCustomMode { get; set; }
public virtual void SetStyleColor(UIBaseStyle uiColor)
public void SetStyleColor(UIBaseStyle uiColor)
{
foreColor = uiColor.SmoothLabelForeColor;
rectColor = uiColor.SmoothLabelRectColor;
@ -163,7 +183,7 @@ namespace Sunny.UI
Invalidate();
}
protected Color rectColor;
private Color rectColor;
/// <summary>
/// 边框颜色

View File

@ -26,7 +26,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
public class UISplitContainer : SplitContainer, IStyleInterface
public sealed class UISplitContainer : SplitContainer, IStyleInterface
{
private enum UIMouseType
{
@ -68,6 +68,9 @@ namespace Sunny.UI
private UIMouseType _uiMouseType;
private readonly object EventCollapseClick = new object();
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public UISplitContainer()
{
SetStyle(ControlStyles.UserPaint |
@ -79,7 +82,24 @@ namespace Sunny.UI
Version = UIGlobal.Version;
}
protected void SetStyleCustom(bool needRefresh = true)
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
private void SetStyleCustom(bool needRefresh = true)
{
_style = UIStyle.Custom;
if (needRefresh) Invalidate();
@ -163,9 +183,9 @@ namespace Sunny.UI
}
}
protected virtual int DefaultCollapseWidth => 80;
private int DefaultCollapseWidth => 80;
protected virtual int DefaultArrowWidth => 24;
private int DefaultArrowWidth => 24;
private Rectangle CollapseRect
{
@ -227,7 +247,7 @@ namespace Sunny.UI
}
}
protected UIStyle _style = UIStyle.Blue;
private UIStyle _style = UIStyle.Blue;
/// <summary>
/// 主题样式
@ -317,7 +337,7 @@ namespace Sunny.UI
}
}
protected virtual void OnCollapseClick(EventArgs e)
private void OnCollapseClick(EventArgs e)
{
SplitPanelState = SplitPanelState == UISplitPanelState.Collapsed ?
UISplitPanelState.Expanded : UISplitPanelState.Collapsed;
@ -384,7 +404,7 @@ namespace Sunny.UI
}
}
protected Point[] GetHandlePoints()
private Point[] GetHandlePoints()
{
bool bCollapsed = SplitPanelState == UISplitPanelState.Collapsed;

View File

@ -72,6 +72,26 @@ namespace Sunny.UI
_fillColor = UIStyles.Blue.TabControlBackColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
private ConcurrentDictionary<TabPage, string> TipsTexts = new ConcurrentDictionary<TabPage, string>();
public void SetTipsText(TabPage tabPage, string tipsText)
@ -192,9 +212,9 @@ namespace Sunny.UI
[Description("是否禁用Ctrl+Tab"), Category("SunnyUI")]
public bool ForbidCtrlTab { get; set; } = true;
public void SelectPage(int pageIndex) => Helper.SelectPage(pageIndex);
public bool SelectPage(int pageIndex) => Helper.SelectPage(pageIndex);
public void SelectPage(Guid pageGuid) => Helper.SelectPage(pageGuid);
public bool SelectPage(Guid pageGuid) => Helper.SelectPage(pageGuid);
public void AddPage(UIPage page) => Helper.AddPage(page);

View File

@ -50,6 +50,26 @@ namespace Sunny.UI
_fillColor = UIStyles.Blue.TabControlBackColor;
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Browsable(false)]
public bool IsScaled { get; private set; }

View File

@ -21,20 +21,27 @@
******************************************************************************/
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
public class UITableLayoutPanel : TableLayoutPanel, IStyleInterface
public sealed class UITableLayoutPanel : TableLayoutPanel, IStyleInterface
{
public UITableLayoutPanel()
{
Version = UIGlobal.Version;
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
[Browsable(false)]
public bool IsScaled { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public void SetDPIScale()
{
if (!IsScaled)
@ -66,7 +73,7 @@ namespace Sunny.UI
get; set;
}
protected UIStyle _style = UIStyle.Blue;
private UIStyle _style = UIStyle.Blue;
/// <summary>
/// 主题样式

View File

@ -50,11 +50,15 @@ namespace Sunny.UI
SetStyleFlags(true, false);
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
public virtual void SetDPIScale()
{
if (DesignMode) return;
if (!IsScaled)
{
this.SetDPIScaleFont();
@ -341,10 +345,26 @@ namespace Sunny.UI
}
}
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (AutoScaleMode == AutoScaleMode.Font) AutoScaleMode = AutoScaleMode.None;
if (AutoScaleMode == AutoScaleMode.Font)
{
AutoScaleMode = AutoScaleMode.None;
}
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
protected override void OnPaint(PaintEventArgs e)

View File

@ -82,6 +82,9 @@ namespace Sunny.UI
titleForeColor = UIStyles.Blue.FormTitleForeColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false)]
public bool IsScaled { get; private set; }
@ -112,6 +115,27 @@ namespace Sunny.UI
}
}
[DefaultValue(typeof(Size), "0, 0")]
[Description("设计界面大小"), Category("SunnyUI")]
public ControlScaleInfo DesignedRect { get; private set; }
private void SetControlScale()
{
if (ForbidControlScale) return;
if (!UIStyles.DPIScale || !UIStyles.ControlScale) return;
if (DesignedRect.Width == 0 || DesignedRect.Height == 0) return;
if (Width == 0 || Height == 0) return;
float scale = Math.Min(Width * 1.0f / DesignedRect.Width, Height * 1.0f / DesignedRect.Height);
if (scale.EqualsFloat(0)) return;
foreach (Control control in this.GetAllDPIScaleControls())
{
if (control is IStyleInterface)
{
UIDPIScale.SetControlScale(control, scale);
}
}
}
public void ResetDPIScale()
{
IsScaled = false;
@ -1220,6 +1244,7 @@ namespace Sunny.UI
public void SetStyle(UIStyle style)
{
if (DesignMode) return;
this.SuspendLayout();
UIStyleHelper.SetChildUIStyle(this, style);
@ -1267,6 +1292,7 @@ namespace Sunny.UI
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
SetControlScale();
CalcSystemBoxPos();
if (isShow)
@ -1300,6 +1326,15 @@ namespace Sunny.UI
SetRadius();
isShow = true;
SetDPIScale();
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this); ;
}
}
/// <summary>
@ -1990,18 +2025,43 @@ namespace Sunny.UI
private UIForm SetDefaultTabControl()
{
if (MainTabControl == null)
List<UITabControl> ctrls = this.GetControls<UITabControl>();
if (ctrls.Count == 1)
{
List<UITabControl> ctrls = this.GetControls<UITabControl>();
if (ctrls.Count == 1) MainTabControl = ctrls[0];
if (MainTabControl == null)
{
MainTabControl = ctrls[0];
}
List<UINavMenu> Menus = this.GetControls<UINavMenu>();
if (Menus.Count == 1 && Menus[0].TabControl == null)
{
Menus[0].TabControl = ctrls[0];
}
List<UINavBar> Bars = this.GetControls<UINavBar>();
if (Bars.Count == 1 && Bars[0].TabControl == null)
{
Bars[0].TabControl = ctrls[0];
}
}
return this;
}
public virtual void SelectPage(int pageIndex) => SetDefaultTabControl().MainTabControl?.SelectPage(pageIndex);
public virtual bool SelectPage(int pageIndex)
{
SetDefaultTabControl();
if (MainTabControl == null) return false;
return MainTabControl.SelectPage(pageIndex);
}
public virtual void SelectPage(Guid guid) => SetDefaultTabControl().MainTabControl?.SelectPage(guid);
public virtual bool SelectPage(Guid guid)
{
SetDefaultTabControl();
if (MainTabControl == null) return false;
return MainTabControl.SelectPage(guid);
}
public bool RemovePage(int pageIndex) => MainTabControl?.RemovePage(pageIndex) ?? false;

View File

@ -34,9 +34,9 @@ namespace Sunny.UI
UIPage AddPage(UIPage page);
void SelectPage(int pageIndex);
bool SelectPage(int pageIndex);
void SelectPage(Guid guid);
bool SelectPage(Guid guid);
UIPage GetPage(int pageIndex);
@ -57,7 +57,9 @@ namespace Sunny.UI
bool SetParamToPage(int toPageIndex, int fromPageIndex, params object[] objects);
bool SetParamToPage(Guid toPageGuid, Guid fromPageGuid, params object[] objects);
void Init();
void Final();
T GetPage<T>() where T : UIPage;

View File

@ -35,11 +35,12 @@ namespace Sunny.UI
Aside.TabControl = MainTabControl;
}
public override void SelectPage(int pageIndex)
public override bool SelectPage(int pageIndex)
{
base.SelectPage(pageIndex);
bool result = base.SelectPage(pageIndex);
TreeNode node = Aside.GetTreeNode(pageIndex);
if (node != null) Aside.SelectedNode = node;
return result;
}
}
}

View File

@ -37,11 +37,12 @@ namespace Sunny.UI
Aside.TabControl = MainTabControl;
}
public override void SelectPage(int pageIndex)
public override bool SelectPage(int pageIndex)
{
base.SelectPage(pageIndex);
bool result = base.SelectPage(pageIndex);
TreeNode node = Aside.GetTreeNode(pageIndex);
if (node != null) Aside.SelectedNode = node;
return result;
}
}
}

View File

@ -76,11 +76,33 @@ namespace Sunny.UI
titleForeColor = UIStyles.Blue.PageTitleForeColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false), DefaultValue(typeof(Size), "0, 0")]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
}
[Browsable(false)]
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (DesignMode) return;
if (!IsScaled)
{
this.SetDPIScaleFont();

View File

@ -105,5 +105,87 @@ namespace Sunny.UI
return list;
}
private static int Calc(int size, float scale)
{
return (int)(size * scale + 0.5);
}
internal static void SetControlScale(Control control, float scale)
{
if (scale.EqualsFloat(0)) return;
if (control is IStyleInterface ctrl)
{
if (ctrl.ForbidControlScale)
{
return;
}
if (control is IControlScale sc)
{
sc.SetControlScale(scale);
}
if (control.Dock == DockStyle.Fill)
{
return;
}
var rect = ctrl.DesignedRect;
switch (control.Dock)
{
case DockStyle.None:
control.Height = Calc(rect.Height, scale);
control.Width = Calc(rect.Width, scale);
if (control.Parent != null)
{
if ((control.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
{
control.Left = Calc(rect.XInterval, scale);
}
if ((control.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
{
int right = Calc(rect.XInterval, scale);
control.Left = control.Parent.Width - right - control.Width;
}
if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
{
if (control.Parent is UIForm form && form.ShowTitle)
control.Top = Calc(rect.YInterval - form.TitleHeight, scale) + form.TitleHeight;
else
control.Top = Calc(rect.YInterval, scale);
}
if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
{
int bottom = Calc(rect.YInterval, scale);
control.Top = control.Parent.Height - bottom - control.Height;
}
}
break;
case DockStyle.Top:
control.Height = Calc(rect.Height, scale);
break;
case DockStyle.Bottom:
control.Height = Calc(rect.Height, scale);
break;
case DockStyle.Left:
control.Width = Calc(rect.Width, scale);
break;
case DockStyle.Right:
control.Width = Calc(rect.Width, scale);
break;
case DockStyle.Fill:
break;
default:
break;
}
}
}
}
}

View File

@ -23,8 +23,6 @@
* 2021-10-16: V3.0.8 DPI缩放自适应
******************************************************************************/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
@ -61,6 +59,53 @@ namespace Sunny.UI
bool IsScaled { get; }
void SetDPIScale();
ControlScaleInfo DesignedRect { get; }
bool ForbidControlScale { get; set; }
}
public interface IControlScale
{
void SetControlScale(float scale);
}
public struct ControlScaleInfo
{
public int XInterval { get; }
public int YInterval { get; }
public int Width { get; }
public int Height { get; }
public ControlScaleInfo(Control control)
{
Width = control.Width;
Height = control.Height;
XInterval = 0;
YInterval = 0;
if (control.Parent != null)
{
if ((control.Anchor & AnchorStyles.Left) == AnchorStyles.Left)
{
XInterval = control.Left;
}
if ((control.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
{
XInterval = control.Parent.Width - control.Right;
}
if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
{
YInterval = control.Top;
}
if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
{
YInterval = control.Parent.Height - control.Bottom;
}
}
}
}
/// <summary>
@ -147,299 +192,6 @@ namespace Sunny.UI
Colorful = 999
}
/// <summary>
/// 主题样式管理类
/// </summary>
public static class UIStyles
{
public static bool DPIScale { get; set; }
public static float FontSize { get; set; } = 12;
public static List<UIStyle> PopularStyles()
{
List<UIStyle> styles = new List<UIStyle>();
foreach (UIStyle style in Enum.GetValues(typeof(UIStyle)))
{
if (style.Value() >= UIStyle.Blue.Value() && style.Value() < UIStyle.Colorful.Value())
{
styles.Add(style);
}
}
return styles;
}
/// <summary>
/// 自定义
/// </summary>
private static readonly UIBaseStyle Custom = new UICustomStyle();
/// <summary>
/// 蓝
/// </summary>
public static readonly UIBaseStyle Blue = new UIBlueStyle();
/// <summary>
/// 橙
/// </summary>
public static readonly UIBaseStyle Orange = new UIOrangeStyle();
/// <summary>
/// 灰
/// </summary>
public static readonly UIBaseStyle Gray = new UIGrayStyle();
/// <summary>
/// 绿
/// </summary>
public static readonly UIBaseStyle Green = new UIGreenStyle();
/// <summary>
/// 红
/// </summary>
public static readonly UIBaseStyle Red = new UIRedStyle();
/// <summary>
/// 深蓝
/// </summary>
public static readonly UIBaseStyle DarkBlue = new UIDarkBlueStyle();
/// <summary>
/// 黑
/// </summary>
public static readonly UIBaseStyle Black = new UIBlackStyle();
/// <summary>
/// 紫
/// </summary>
public static readonly UIBaseStyle Purple = new UIPurpleStyle();
/// <summary>
/// 多彩
/// </summary>
private static readonly UIColorfulStyle Colorful = new UIColorfulStyle();
public static void InitColorful(Color styleColor, Color foreColor)
{
Colorful.Init(styleColor, foreColor);
Style = UIStyle.Colorful;
SetStyle(Style);
}
private static readonly ConcurrentDictionary<UIStyle, UIBaseStyle> Styles = new ConcurrentDictionary<UIStyle, UIBaseStyle>();
private static readonly ConcurrentDictionary<Guid, UIForm> Forms = new ConcurrentDictionary<Guid, UIForm>();
private static readonly ConcurrentDictionary<Guid, UIPage> Pages = new ConcurrentDictionary<Guid, UIPage>();
/// <summary>
/// 菜单颜色集合
/// </summary>
public static readonly ConcurrentDictionary<UIMenuStyle, UIMenuColor> MenuColors = new ConcurrentDictionary<UIMenuStyle, UIMenuColor>();
static UIStyles()
{
AddStyle(Custom);
AddStyle(Blue);
AddStyle(Orange);
AddStyle(Gray);
AddStyle(Green);
AddStyle(Red);
AddStyle(DarkBlue);
AddStyle(new UIBaseStyle().Init(UIColor.LayuiGreen, UIStyle.LayuiGreen, Color.White, UIFontColor.Primary));
AddStyle(new UIBaseStyle().Init(UIColor.LayuiRed, UIStyle.LayuiRed, Color.White, UIFontColor.Primary));
AddStyle(new UIBaseStyle().Init(UIColor.LayuiOrange, UIStyle.LayuiOrange, Color.White, UIFontColor.Primary));
AddStyle(Black);
AddStyle(Purple);
AddStyle(Colorful);
MenuColors.TryAdd(UIMenuStyle.Custom, new UIMenuCustomColor());
MenuColors.TryAdd(UIMenuStyle.Black, new UIMenuBlackColor());
MenuColors.TryAdd(UIMenuStyle.White, new UIMenuWhiteColor());
}
/// <summary>
/// 主题样式整数值
/// </summary>
/// <param name="style">主题样式</param>
/// <returns>整数值</returns>
public static int Value(this UIStyle style)
{
return (int)style;
}
/// <summary>
/// 注册窗体
/// </summary>
/// <param name="guid">GUID</param>
/// <param name="form">窗体</param>
public static bool Register(Guid guid, UIForm form)
{
if (!Forms.ContainsKey(guid))
{
Forms.TryAddOrUpdate(guid, form);
return true;
}
return false;
}
/// <summary>
/// 注册页面
/// </summary>
/// <param name="guid">GUID</param>
/// <param name="page">页面</param>
public static bool Register(Guid guid, UIPage page)
{
if (!Pages.ContainsKey(guid))
{
Pages.TryAddOrUpdate(guid, page);
return true;
}
return false;
}
/// <summary>
/// 注册窗体
/// </summary>
/// <param name="form">窗体</param>
public static bool Register(this UIForm form)
{
if (!Forms.ContainsKey(form.Guid))
{
Forms.TryAddOrUpdate(form.Guid, form);
return true;
}
return false;
}
/// <summary>
/// 注册页面
/// </summary>
/// <param name="page">页面</param>
public static bool Register(this UIPage page)
{
if (!Pages.ContainsKey(page.Guid))
{
Pages.TryAddOrUpdate(page.Guid, page);
return true;
}
return false;
}
/// <summary>
/// 反注册窗体
/// </summary>
/// <param name="form">窗体</param>
public static void UnRegister(this UIForm form)
{
Forms.TryRemove(form.Guid, out _);
}
/// <summary>
/// 反注册页面
/// </summary>
/// <param name="page">页面</param>
public static void UnRegister(this UIPage page)
{
Pages.TryRemove(page.Guid, out _);
}
/// <summary>
/// 反注册窗体、页面
/// </summary>
/// <param name="guid">GUID</param>
public static void UnRegister(Guid guid)
{
if (Forms.ContainsKey(guid))
Forms.TryRemove(guid, out _);
if (Pages.ContainsKey(guid))
Pages.TryRemove(guid, out _);
}
/// <summary>
/// 获取主题样式
/// </summary>
/// <param name="style">主题样式名称</param>
/// <returns>主题样式</returns>
public static UIBaseStyle GetStyleColor(UIStyle style)
{
if (Styles.ContainsKey(style))
{
return Styles[style];
}
Style = UIStyle.Blue;
return Styles[Style];
}
public static UIBaseStyle ActiveStyleColor => GetStyleColor(Style);
private static void AddStyle(UIBaseStyle uiColor)
{
if (Styles.ContainsKey(uiColor.Name))
{
MessageBox.Show(uiColor.Name + " is already exist.");
}
Styles.TryAdd(uiColor.Name, uiColor);
}
/// <summary>
/// 主题样式
/// </summary>
public static UIStyle Style { get; private set; } = UIStyle.Blue;
/// <summary>
/// 设置主题样式
/// </summary>
/// <param name="style">主题样式</param>
public static void SetStyle(UIStyle style)
{
Style = style;
foreach (var form in Forms.Values)
{
form.Style = style;
}
foreach (var page in Pages.Values)
{
page.Style = style;
}
}
public static void SetDPIScale()
{
foreach (var form in Forms.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
form.SetDPIScale();
}
foreach (var page in Pages.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
page.SetDPIScale();
}
}
public static void Translate()
{
foreach (var form in Forms.Values)
{
form.Translate();
}
}
}
/// <summary>
/// 主题颜色
/// </summary>

View File

@ -85,6 +85,12 @@ namespace Sunny.UI
set => UIStyles.FontSize = value;
}
public bool ControlScale
{
get => UIStyles.ControlScale;
set => UIStyles.ControlScale = value;
}
/// <summary>
/// 版本
/// </summary>

342
SunnyUI/Style/UIStyles.cs Normal file
View File

@ -0,0 +1,342 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2022 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIStyles.cs
* :
* : V3.1
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2021-07-12: V3.0.5
* 2021-07-18: V3.0.5
* 2021-09-24: V3.0.7 GdiCharSet
* 2021-10-16: V3.0.8 DPI缩放自适应
******************************************************************************/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
/// <summary>
/// 主题样式管理类
/// </summary>
public static class UIStyles
{
public static bool DPIScale { get; set; }
public static bool ControlScale { get; set; }
public static float FontSize { get; set; } = 12;
public static List<UIStyle> PopularStyles()
{
List<UIStyle> styles = new List<UIStyle>();
foreach (UIStyle style in Enum.GetValues(typeof(UIStyle)))
{
if (style.Value() >= UIStyle.Blue.Value() && style.Value() < UIStyle.Colorful.Value())
{
styles.Add(style);
}
}
return styles;
}
/// <summary>
/// 自定义
/// </summary>
private static readonly UIBaseStyle Custom = new UICustomStyle();
/// <summary>
/// 蓝
/// </summary>
public static readonly UIBaseStyle Blue = new UIBlueStyle();
/// <summary>
/// 橙
/// </summary>
public static readonly UIBaseStyle Orange = new UIOrangeStyle();
/// <summary>
/// 灰
/// </summary>
public static readonly UIBaseStyle Gray = new UIGrayStyle();
/// <summary>
/// 绿
/// </summary>
public static readonly UIBaseStyle Green = new UIGreenStyle();
/// <summary>
/// 红
/// </summary>
public static readonly UIBaseStyle Red = new UIRedStyle();
/// <summary>
/// 深蓝
/// </summary>
public static readonly UIBaseStyle DarkBlue = new UIDarkBlueStyle();
/// <summary>
/// 黑
/// </summary>
public static readonly UIBaseStyle Black = new UIBlackStyle();
/// <summary>
/// 紫
/// </summary>
public static readonly UIBaseStyle Purple = new UIPurpleStyle();
/// <summary>
/// 多彩
/// </summary>
private static readonly UIColorfulStyle Colorful = new UIColorfulStyle();
public static void InitColorful(Color styleColor, Color foreColor)
{
Colorful.Init(styleColor, foreColor);
Style = UIStyle.Colorful;
SetStyle(Style);
}
private static readonly ConcurrentDictionary<UIStyle, UIBaseStyle> Styles = new ConcurrentDictionary<UIStyle, UIBaseStyle>();
private static readonly ConcurrentDictionary<Guid, UIForm> Forms = new ConcurrentDictionary<Guid, UIForm>();
private static readonly ConcurrentDictionary<Guid, UIPage> Pages = new ConcurrentDictionary<Guid, UIPage>();
/// <summary>
/// 菜单颜色集合
/// </summary>
public static readonly ConcurrentDictionary<UIMenuStyle, UIMenuColor> MenuColors = new ConcurrentDictionary<UIMenuStyle, UIMenuColor>();
static UIStyles()
{
AddStyle(Custom);
AddStyle(Blue);
AddStyle(Orange);
AddStyle(Gray);
AddStyle(Green);
AddStyle(Red);
AddStyle(DarkBlue);
AddStyle(new UIBaseStyle().Init(UIColor.LayuiGreen, UIStyle.LayuiGreen, Color.White, UIFontColor.Primary));
AddStyle(new UIBaseStyle().Init(UIColor.LayuiRed, UIStyle.LayuiRed, Color.White, UIFontColor.Primary));
AddStyle(new UIBaseStyle().Init(UIColor.LayuiOrange, UIStyle.LayuiOrange, Color.White, UIFontColor.Primary));
AddStyle(Black);
AddStyle(Purple);
AddStyle(Colorful);
MenuColors.TryAdd(UIMenuStyle.Custom, new UIMenuCustomColor());
MenuColors.TryAdd(UIMenuStyle.Black, new UIMenuBlackColor());
MenuColors.TryAdd(UIMenuStyle.White, new UIMenuWhiteColor());
}
/// <summary>
/// 主题样式整数值
/// </summary>
/// <param name="style">主题样式</param>
/// <returns>整数值</returns>
public static int Value(this UIStyle style)
{
return (int)style;
}
/// <summary>
/// 注册窗体
/// </summary>
/// <param name="guid">GUID</param>
/// <param name="form">窗体</param>
public static bool Register(Guid guid, UIForm form)
{
if (!Forms.ContainsKey(guid))
{
Forms.TryAddOrUpdate(guid, form);
return true;
}
return false;
}
/// <summary>
/// 注册页面
/// </summary>
/// <param name="guid">GUID</param>
/// <param name="page">页面</param>
public static bool Register(Guid guid, UIPage page)
{
if (!Pages.ContainsKey(guid))
{
Pages.TryAddOrUpdate(guid, page);
return true;
}
return false;
}
/// <summary>
/// 注册窗体
/// </summary>
/// <param name="form">窗体</param>
public static bool Register(this UIForm form)
{
if (!Forms.ContainsKey(form.Guid))
{
Forms.TryAddOrUpdate(form.Guid, form);
return true;
}
return false;
}
/// <summary>
/// 注册页面
/// </summary>
/// <param name="page">页面</param>
public static bool Register(this UIPage page)
{
if (!Pages.ContainsKey(page.Guid))
{
Pages.TryAddOrUpdate(page.Guid, page);
return true;
}
return false;
}
public static List<T> GetPages<T>() where T : UIPage
{
List<T> result = new List<T>();
foreach (var page in Pages)
{
if (page is T pg)
result.Add(pg);
}
return result;
}
/// <summary>
/// 反注册窗体
/// </summary>
/// <param name="form">窗体</param>
public static void UnRegister(this UIForm form)
{
Forms.TryRemove(form.Guid, out _);
}
/// <summary>
/// 反注册页面
/// </summary>
/// <param name="page">页面</param>
public static void UnRegister(this UIPage page)
{
Pages.TryRemove(page.Guid, out _);
}
/// <summary>
/// 反注册窗体、页面
/// </summary>
/// <param name="guid">GUID</param>
public static void UnRegister(Guid guid)
{
if (Forms.ContainsKey(guid))
Forms.TryRemove(guid, out _);
if (Pages.ContainsKey(guid))
Pages.TryRemove(guid, out _);
}
/// <summary>
/// 获取主题样式
/// </summary>
/// <param name="style">主题样式名称</param>
/// <returns>主题样式</returns>
public static UIBaseStyle GetStyleColor(UIStyle style)
{
if (Styles.ContainsKey(style))
{
return Styles[style];
}
Style = UIStyle.Blue;
return Styles[Style];
}
public static UIBaseStyle ActiveStyleColor => GetStyleColor(Style);
private static void AddStyle(UIBaseStyle uiColor)
{
if (Styles.ContainsKey(uiColor.Name))
{
MessageBox.Show(uiColor.Name + " is already exist.");
}
Styles.TryAdd(uiColor.Name, uiColor);
}
/// <summary>
/// 主题样式
/// </summary>
public static UIStyle Style { get; private set; } = UIStyle.Blue;
/// <summary>
/// 设置主题样式
/// </summary>
/// <param name="style">主题样式</param>
public static void SetStyle(UIStyle style)
{
Style = style;
foreach (var form in Forms.Values)
{
form.Style = style;
}
foreach (var page in Pages.Values)
{
page.Style = style;
}
}
public static void SetDPIScale()
{
if (!DPIScale) return;
foreach (var form in Forms.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
form.SetDPIScale();
}
foreach (var page in Pages.Values)
{
if (!UIDPIScale.DPIScaleIsOne())
page.SetDPIScale();
}
}
public static void Translate()
{
foreach (var form in Forms.Values)
{
form.Translate();
}
}
}
}