重构窗体缩放

This commit is contained in:
Sunny 2022-04-11 13:18:21 +08:00
parent 8bee360a99
commit b50a56da5a
25 changed files with 312 additions and 387 deletions

View File

@ -8,7 +8,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
[ToolboxItem(false)] [ToolboxItem(false)]
public sealed class UIColorWheel : Control, IStyleInterface public sealed class UIColorWheel : Control, IStyleInterface, IZoomScale
{ {
public event EventHandler SelectedColorChanged; public event EventHandler SelectedColorChanged;
@ -34,10 +34,15 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
public void SetZoomScale(float scale)
{
}
[Browsable(false)] [Browsable(false)]
public bool IsScaled { get; private set; } public bool IsScaled { get; private set; }

View File

@ -5,17 +5,22 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
[ToolboxItem(false)] [ToolboxItem(false)]
public class LabelRotate : Control, IStyleInterface public class LabelRotate : Control, IStyleInterface, IZoomScale
{ {
private float m_textAngle = 0; private float m_textAngle = 0;
private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter; private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter;
private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft; private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft;
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)]
public Rectangle ZoomScaleRect { get; set; }
public virtual void SetZoomScale(float scale)
{
}
[Browsable(false)] [Browsable(false)]
public bool IsScaled { get; private set; } public bool IsScaled { get; private set; }

View File

@ -35,7 +35,7 @@ namespace Sunny.UI
[DefaultEvent("Click")] [DefaultEvent("Click")]
[DefaultProperty("Symbol")] [DefaultProperty("Symbol")]
[ToolboxItem(true)] [ToolboxItem(true)]
public sealed class UIAvatar : UIControl, ISymbol, IControlScale public sealed class UIAvatar : UIControl, ISymbol, IZoomScale
{ {
/// <summary> /// <summary>
/// 头像图标类型 /// 头像图标类型
@ -356,10 +356,11 @@ namespace Sunny.UI
PaintAgain?.Invoke(this, e); PaintAgain?.Invoke(this, e);
} }
public void SetControlScale(float scale) public override void SetZoomScale(float scale)
{ {
avatarSize = UIDPIScale.Calc(baseAvatorSize, scale); base.SetZoomScale(scale);
symbolSize = UIDPIScale.Calc(baseSymbolSize, scale); avatarSize = UIZoomScale.Calc(baseAvatorSize, scale);
symbolSize = UIZoomScale.Calc(baseSymbolSize, scale);
Invalidate(); Invalidate();
} }
} }

View File

@ -27,7 +27,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public sealed class UIContextMenuStrip : ContextMenuStrip, IStyleInterface public sealed class UIContextMenuStrip : ContextMenuStrip, IStyleInterface, IZoomScale
{ {
public ContextMenuColorTable ColorTable = new ContextMenuColorTable(); public ContextMenuColorTable ColorTable = new ContextMenuColorTable();
@ -43,10 +43,15 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
public void SetZoomScale(float scale)
{
}
[Browsable(false), DefaultValue(false)] [Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; } public bool IsScaled { get; set; }

View File

@ -36,7 +36,7 @@ namespace Sunny.UI
/// 控件基类 /// 控件基类
/// </summary> /// </summary>
[ToolboxItem(false)] [ToolboxItem(false)]
public class UIControl : Control, IStyleInterface public class UIControl : Control, IStyleInterface, IZoomScale
{ {
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
@ -49,25 +49,16 @@ namespace Sunny.UI
base.MinimumSize = new Size(1, 1); base.MinimumSize = new Size(1, 1);
} }
[Browsable(false)]
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("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)]
public Rectangle ZoomScaleRect { get; set; }
public virtual void SetZoomScale(float scale)
{
}
protected bool selected; protected bool selected;

View File

@ -38,7 +38,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public class UIDataGridView : DataGridView, IStyleInterface public class UIDataGridView : DataGridView, IStyleInterface, IZoomScale
{ {
private readonly UIScrollBar VBar = new UIScrollBar(); private readonly UIScrollBar VBar = new UIScrollBar();
private readonly UIHorScrollBarEx HBar = new UIHorScrollBarEx(); private readonly UIHorScrollBarEx HBar = new UIHorScrollBarEx();
@ -101,23 +101,14 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public virtual void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Browsable(false)] [Browsable(false)]

View File

@ -30,7 +30,7 @@ namespace Sunny.UI
/// <summary> /// <summary>
/// 图像按钮 /// 图像按钮
/// </summary> /// </summary>
public sealed class UIImageButton : PictureBox, IStyleInterface public sealed class UIImageButton : PictureBox, IStyleInterface, IZoomScale
{ {
private bool IsPress; private bool IsPress;
private bool IsHover; private bool IsHover;
@ -44,24 +44,15 @@ namespace Sunny.UI
private ContentAlignment textAlign = ContentAlignment.MiddleCenter; private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
private Color foreColor = UIFontColor.Primary; private Color foreColor = UIFontColor.Primary;
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
protected override void OnVisibleChanged(EventArgs e) [Browsable(false)]
{ public Rectangle ZoomScaleRect { get; set; }
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize() public void SetZoomScale(float scale)
{ {
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
/// <summary> /// <summary>

View File

@ -338,16 +338,10 @@ namespace Sunny.UI
} }
[ToolboxItem(false)] [ToolboxItem(false)]
private sealed class ImageListBox : ListBox, IStyleInterface private sealed class ImageListBox : ListBox
{ {
private UIScrollBar bar; private UIScrollBar bar;
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
/// <summary> /// <summary>
/// Tag字符串 /// Tag字符串
/// </summary> /// </summary>

View File

@ -23,7 +23,6 @@
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
******************************************************************************/ ******************************************************************************/
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
@ -33,7 +32,7 @@ namespace Sunny.UI
[ToolboxItem(true)] [ToolboxItem(true)]
[DefaultEvent("Click")] [DefaultEvent("Click")]
[DefaultProperty("Text")] [DefaultProperty("Text")]
public class UILabel : Label, IStyleInterface public class UILabel : Label, IStyleInterface, IZoomScale
{ {
public UILabel() public UILabel()
{ {
@ -44,23 +43,14 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public virtual void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
private int angle; private int angle;
@ -173,7 +163,7 @@ namespace Sunny.UI
[ToolboxItem(true)] [ToolboxItem(true)]
[DefaultEvent("Click")] [DefaultEvent("Click")]
[DefaultProperty("Text")] [DefaultProperty("Text")]
public sealed class UILinkLabel : LinkLabel, IStyleInterface public sealed class UILinkLabel : LinkLabel, IStyleInterface, IZoomScale
{ {
public UILinkLabel() public UILinkLabel()
{ {
@ -189,23 +179,14 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Browsable(false)] [Browsable(false)]

View File

@ -605,16 +605,10 @@ namespace Sunny.UI
/// ListBox /// ListBox
/// </summary> /// </summary>
[ToolboxItem(false)] [ToolboxItem(false)]
public sealed class ListBoxEx : ListBox, IStyleInterface public sealed class ListBoxEx : ListBox
{ {
private UIScrollBar bar; private UIScrollBar bar;
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
/// <summary> /// <summary>
/// Tag字符串 /// Tag字符串
/// </summary> /// </summary>

View File

@ -34,7 +34,7 @@ namespace Sunny.UI
[DefaultEvent("MenuItemClick")] [DefaultEvent("MenuItemClick")]
[DefaultProperty("Nodes")] [DefaultProperty("Nodes")]
[Designer("System.Windows.Forms.Design.TreeViewDesigner, " + AssemblyRefEx.SystemDesign)] [Designer("System.Windows.Forms.Design.TreeViewDesigner, " + AssemblyRefEx.SystemDesign)]
public sealed partial class UINavBar : ScrollableControl, IStyleInterface public sealed partial class UINavBar : ScrollableControl, IStyleInterface, IZoomScale
{ {
public readonly TreeView Menu = new TreeView(); public readonly TreeView Menu = new TreeView();
@ -65,23 +65,16 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e); _nodeInterval = UIZoomScale.Calc(baseNodeInterval, scale);
SetDesignedSize(); nodeSize = UIZoomScale.Calc(baseNodeSize, scale);
} Invalidate();
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Browsable(false)] [Browsable(false)]
@ -562,6 +555,7 @@ namespace Sunny.UI
} }
private int _nodeInterval = 100; private int _nodeInterval = 100;
private int baseNodeInterval = 100;
[DefaultValue(100)] [DefaultValue(100)]
[Description("显示菜单边距"), Category("SunnyUI")] [Description("显示菜单边距"), Category("SunnyUI")]
@ -572,13 +566,14 @@ namespace Sunny.UI
{ {
if (_nodeInterval != value) if (_nodeInterval != value)
{ {
_nodeInterval = value; baseNodeInterval = _nodeInterval = value;
Invalidate(); Invalidate();
} }
} }
} }
private Size nodeSize = new Size(130, 45); private Size nodeSize = new Size(130, 45);
private Size baseNodeSize = new Size(130, 45);
[DefaultValue(typeof(Size), "130, 45")] [DefaultValue(typeof(Size), "130, 45")]
[Description("显示菜单大小"), Category("SunnyUI")] [Description("显示菜单大小"), Category("SunnyUI")]
@ -589,7 +584,7 @@ namespace Sunny.UI
{ {
if (nodeSize != value) if (nodeSize != value)
{ {
nodeSize = value; baseNodeSize = nodeSize = value;
Invalidate(); Invalidate();
} }
} }

View File

@ -39,7 +39,7 @@ namespace Sunny.UI
{ {
[DefaultEvent("MenuItemClick")] [DefaultEvent("MenuItemClick")]
[DefaultProperty("Nodes")] [DefaultProperty("Nodes")]
public sealed class UINavMenu : TreeView, IStyleInterface public sealed class UINavMenu : TreeView, IStyleInterface, IZoomScale
{ {
public delegate void OnMenuItemClick(TreeNode node, NavMenuItem item, int pageIndex); public delegate void OnMenuItemClick(TreeNode node, NavMenuItem item, int pageIndex);
@ -47,9 +47,6 @@ namespace Sunny.UI
private readonly UIScrollBar Bar = new UIScrollBar(); private readonly UIScrollBar Bar = new UIScrollBar();
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public UINavMenu() public UINavMenu()
{ {
SetStyle(ControlStyles.DoubleBuffer | SetStyle(ControlStyles.DoubleBuffer |
@ -90,21 +87,15 @@ namespace Sunny.UI
selectedHighColor = UIStyles.Blue.NavMenuMenuSelectedColor; selectedHighColor = UIStyles.Blue.NavMenuMenuSelectedColor;
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Description("滚动条填充颜色"), Category("SunnyUI")] [Description("滚动条填充颜色"), Category("SunnyUI")]

View File

@ -32,7 +32,7 @@ namespace Sunny.UI
[ToolboxItem(true)] [ToolboxItem(true)]
[DefaultEvent("Click")] [DefaultEvent("Click")]
[DefaultProperty("Text")] [DefaultProperty("Text")]
public sealed class UISmoothLabel : Label, IStyleInterface public sealed class UISmoothLabel : Label, IStyleInterface, IZoomScale
{ {
public UISmoothLabel() public UISmoothLabel()
{ {
@ -49,7 +49,15 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)]
public Rectangle ZoomScaleRect { get; set; }
public void SetZoomScale(float scale)
{
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
@ -63,23 +71,6 @@ namespace Sunny.UI
base.Dispose(disposing); base.Dispose(disposing);
} }
[Browsable(false)]
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 PointF point;
private SizeF drawSize; private SizeF drawSize;
private Pen drawPen; private Pen drawPen;

View File

@ -26,7 +26,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public sealed class UISplitContainer : SplitContainer, IStyleInterface public sealed class UISplitContainer : SplitContainer, IStyleInterface, IZoomScale
{ {
private enum UIMouseType private enum UIMouseType
{ {
@ -68,9 +68,6 @@ namespace Sunny.UI
private UIMouseType _uiMouseType; private UIMouseType _uiMouseType;
private readonly object EventCollapseClick = new object(); private readonly object EventCollapseClick = new object();
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public UISplitContainer() public UISplitContainer()
{ {
SetStyle(ControlStyles.UserPaint | SetStyle(ControlStyles.UserPaint |
@ -82,21 +79,15 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
private void SetStyleCustom(bool needRefresh = true) private void SetStyleCustom(bool needRefresh = true)

View File

@ -38,7 +38,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public sealed class UITabControl : TabControl, IStyleInterface public sealed class UITabControl : TabControl, IStyleInterface, IZoomScale
{ {
private readonly UITabControlHelper Helper; private readonly UITabControlHelper Helper;
private int DrawedIndex = -1; private int DrawedIndex = -1;
@ -73,23 +73,14 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public void SetZoomScale(float scale)
{ {
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>(); private ConcurrentDictionary<TabPage, string> TipsTexts = new ConcurrentDictionary<TabPage, string>();

View File

@ -27,7 +27,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public sealed class UITabControlMenu : TabControl, IStyleInterface public sealed class UITabControlMenu : TabControl, IStyleInterface, IZoomScale
{ {
public UITabControlMenu() public UITabControlMenu()
{ {
@ -50,24 +50,15 @@ namespace Sunny.UI
_fillColor = UIStyles.Blue.TabControlBackColor; _fillColor = UIStyles.Blue.TabControlBackColor;
} }
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
protected override void OnVisibleChanged(EventArgs e) [Browsable(false)]
{ public Rectangle ZoomScaleRect { get; set; }
base.OnVisibleChanged(e);
SetDesignedSize();
}
private void SetDesignedSize() public void SetZoomScale(float scale)
{ {
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Browsable(false)] [Browsable(false)]

View File

@ -21,7 +21,6 @@
******************************************************************************/ ******************************************************************************/
using System.ComponentModel; using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
@ -33,15 +32,9 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[Browsable(false)] [Browsable(false)]
public bool IsScaled { get; private set; } public bool IsScaled { get; private set; }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (!IsScaled)

View File

@ -31,7 +31,7 @@ namespace Sunny.UI
{ {
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))] [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
[DefaultEvent("Click"), DefaultProperty("Text")] [DefaultEvent("Click"), DefaultProperty("Text")]
public partial class UIUserControl : UserControl, IStyleInterface public partial class UIUserControl : UserControl, IStyleInterface, IZoomScale
{ {
private int radius = 5; private int radius = 5;
protected Color rectColor = UIStyles.Blue.PanelRectColor; protected Color rectColor = UIStyles.Blue.PanelRectColor;
@ -51,7 +51,15 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)]
public Rectangle ZoomScaleRect { get; set; }
public virtual void SetZoomScale(float scale)
{
}
[Browsable(false), DefaultValue(false)] [Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; } public bool IsScaled { get; set; }
@ -345,9 +353,6 @@ namespace Sunny.UI
} }
} }
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
protected override void OnVisibleChanged(EventArgs e) protected override void OnVisibleChanged(EventArgs e)
{ {
base.OnVisibleChanged(e); base.OnVisibleChanged(e);
@ -355,16 +360,6 @@ namespace Sunny.UI
{ {
AutoScaleMode = AutoScaleMode.None; 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) protected override void OnPaint(PaintEventArgs e)

View File

@ -83,7 +83,7 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public bool IsScaled { get; private set; } public bool IsScaled { get; private set; }
@ -115,52 +115,52 @@ namespace Sunny.UI
} }
} }
private void SetDesignedSize() private void SetZoomScaleRect()
{ {
if (DesignedRect.Width == 0 && DesignedRect.Height == 0) if (ZoomScaleRect.Width == 0 && ZoomScaleRect.Height == 0)
{ {
DesignedRect = new ControlScaleInfo(DesignedSize.Width, DesignedSize.Height, 0, 0); ZoomScaleRect = new Rectangle(ZoomScaleSize.Width, ZoomScaleSize.Height, 0, 0);
} }
if (DesignedRect.Width == 0 && DesignedRect.Height == 0) if (ZoomScaleRect.Width == 0 && ZoomScaleRect.Height == 0)
{ {
DesignedRect = new ControlScaleInfo(this); ZoomScaleRect = new Rectangle(Left, Top, Width, Height);
} }
DesignSizeChanged?.Invoke(this, DesignedRect); ZoomScaleRectChanged?.Invoke(this, ZoomScaleRect);
} }
[DefaultValue(typeof(Size), "0, 0")] [DefaultValue(typeof(Size), "0, 0")]
[Description("设计界面大小"), Category("SunnyUI")] [Description("设计界面大小"), Category("SunnyUI")]
public Size DesignedSize public Size ZoomScaleSize
{ {
get; get;
set; set;
} }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
private void SetControlScale() private void SetZoomScale()
{ {
if (ForbidControlScale) return; if (ZoomScaleDisabled) return;
if (!UIStyles.DPIScale || !UIStyles.ControlScale) return; if (!UIStyles.DPIScale || !UIStyles.ZoomScale) return;
if (DesignedRect.Width == 0 || DesignedRect.Height == 0) return; if (ZoomScaleRect.Width == 0 || ZoomScaleRect.Height == 0) return;
if (Width == 0 || Height == 0) return; if (Width == 0 || Height == 0) return;
float scale = Math.Min(Width * 1.0f / DesignedRect.Width, Height * 1.0f / DesignedRect.Height); float scale = Math.Min(Width * 1.0f / ZoomScaleRect.Width, Height * 1.0f / ZoomScaleRect.Height);
if (scale.EqualsFloat(0)) return; if (scale.EqualsFloat(0)) return;
foreach (Control control in this.GetAllDPIScaleControls()) foreach (Control control in this.GetAllZoomScaleControls())
{ {
if (control is IStyleInterface) if (control is IZoomScale ctrl)
{ {
UIDPIScale.SetControlScale(control, scale); UIZoomScale.SetZoomScale(control, scale);
} }
} }
ControlScaleChanged?.Invoke(this, scale); ZoomScaleChanged?.Invoke(this, scale);
} }
public event OnControlScaleChanged ControlScaleChanged; public event OnZoomScaleChanged ZoomScaleChanged;
public void ResetDPIScale() public void ResetDPIScale()
{ {
@ -1318,7 +1318,7 @@ namespace Sunny.UI
protected override void OnSizeChanged(EventArgs e) protected override void OnSizeChanged(EventArgs e)
{ {
base.OnSizeChanged(e); base.OnSizeChanged(e);
SetControlScale(); SetZoomScale();
CalcSystemBoxPos(); CalcSystemBoxPos();
if (isShow) if (isShow)
@ -1352,10 +1352,10 @@ namespace Sunny.UI
SetRadius(); SetRadius();
isShow = true; isShow = true;
SetDPIScale(); SetDPIScale();
SetDesignedSize(); SetZoomScaleRect();
} }
public event OnDesignSizeChanged DesignSizeChanged; public event OnZoomScaleRectChanged ZoomScaleRectChanged;
/// <summary> /// <summary>
/// 是否显示圆角 /// 是否显示圆角

View File

@ -30,9 +30,9 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
public delegate void OnControlScaleChanged(object sender, float scale); public delegate void OnZoomScaleChanged(object sender, float scale);
public delegate void OnDesignSizeChanged(object sender, ControlScaleInfo info); public delegate void OnZoomScaleRectChanged(object sender, Rectangle info);
public static class UIMessageDialog public static class UIMessageDialog
{ {

View File

@ -37,7 +37,7 @@ using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
[DefaultEvent("Initialize")] [DefaultEvent("Initialize")]
public partial class UIPage : Form, IStyleInterface, ISymbol public partial class UIPage : Form, IStyleInterface, ISymbol, IZoomScale
{ {
public readonly Guid Guid = Guid.NewGuid(); public readonly Guid Guid = Guid.NewGuid();
private Color _rectColor = UIColor.Blue; private Color _rectColor = UIColor.Blue;
@ -77,23 +77,14 @@ namespace Sunny.UI
} }
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] [Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; } public Rectangle ZoomScaleRect { get; set; }
protected override void OnVisibleChanged(EventArgs e) public virtual void SetZoomScale(float scale)
{ {
base.OnVisibleChanged(e);
SetDesignedSize();
}
protected virtual void SetDesignedSize()
{
if (DesignedRect.Width == 0 && DesignedRect.Height == 0)
{
DesignedRect = new ControlScaleInfo(this);
}
} }
[Browsable(false)] [Browsable(false)]

View File

@ -95,7 +95,6 @@ namespace Sunny.UI
if (con is UIPagination) continue; if (con is UIPagination) continue;
if (con is UIRichTextBox) continue; if (con is UIRichTextBox) continue;
if (con is UITreeView) continue; if (con is UITreeView) continue;
if (con is UINavBar) continue;
if (con.Controls.Count > 0) if (con.Controls.Count > 0)
{ {
@ -106,86 +105,29 @@ namespace Sunny.UI
return list; return list;
} }
internal static int Calc(int size, float scale) internal static List<Control> GetAllZoomScaleControls(this Control control)
{ {
return (int)(size * scale + 0.5); var list = new List<Control>();
} foreach (Control con in control.Controls)
internal static void SetControlScale(Control control, float scale)
{
if (scale.EqualsFloat(0)) return;
if (control is IStyleInterface ctrl)
{ {
if (ctrl.ForbidControlScale) if (con is IZoomScale)
list.Add(con);
if (con is UITextBox) continue;
if (con is UIDropControl) continue;
if (con is UIListBox) continue;
if (con is UIImageListBox) continue;
if (con is UIPagination) continue;
if (con is UIRichTextBox) continue;
if (con is UITreeView) continue;
if (con.Controls.Count > 0)
{ {
return; list.AddRange(GetAllZoomScaleControls(con));
}
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;
} }
} }
return list;
} }
} }
} }

View File

@ -59,61 +59,6 @@ namespace Sunny.UI
bool IsScaled { get; } bool IsScaled { get; }
void SetDPIScale(); 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(int width, int height, int xInterval, int yInterval)
{
Width = width;
Height = height;
XInterval = xInterval;
YInterval = yInterval;
}
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> /// <summary>

View File

@ -38,7 +38,7 @@ namespace Sunny.UI
{ {
public static bool DPIScale { get; set; } public static bool DPIScale { get; set; }
public static bool ControlScale { get; set; } public static bool ZoomScale { get; set; }
public static float FontSize { get; set; } = 12; public static float FontSize { get; set; } = 12;

View File

@ -0,0 +1,151 @@
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
public interface IZoomScale
{
void SetZoomScale(float scale);
Rectangle ZoomScaleRect { get; set; }
bool ZoomScaleDisabled { get; set; }
}
internal static class UIZoomScale
{
public static Rectangle Create(Control control)
{
if (control is IZoomScale ctrl)
{
if (ctrl.ZoomScaleRect.Width > 0 || ctrl.ZoomScaleRect.Height > 0)
{
return ctrl.ZoomScaleRect;
}
else
{
int Width = control.Width;
int Height = control.Height;
int XInterval = 0;
int 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;
}
}
return new Rectangle(XInterval, YInterval, Width, Height);
}
}
else
{
return new Rectangle(control.Left, control.Top, control.Width, control.Height);
}
}
internal static int Calc(int size, float scale)
{
return (int)(size * scale + 0.5);
}
internal static Size Calc(Size size, float scale)
{
return new Size(Calc(size.Width, scale), Calc(size.Height, scale));
}
internal static void SetZoomScale(Control control, float scale)
{
if (scale.EqualsFloat(0)) return;
if (control is IZoomScale ctrl)
{
if (ctrl.ZoomScaleDisabled)
{
return;
}
//设置控件的缩放参数
ctrl.ZoomScaleRect = UIZoomScale.Create(control);
//设置自定义的缩放例如UIAvatar
ctrl.SetZoomScale(scale);
if (control.Dock == DockStyle.Fill)
{
return;
}
var rect = ctrl.ZoomScaleRect;
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.Left, scale);
}
if ((control.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
{
int right = Calc(rect.Left, 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.Top - form.TitleHeight, scale) + form.TitleHeight;
else
control.Top = Calc(rect.Top, scale);
}
if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
{
int bottom = Calc(rect.Top, 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;
}
}
}
}
}