重构窗体缩放

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

View File

@ -5,17 +5,22 @@ using System.Windows.Forms;
namespace Sunny.UI
{
[ToolboxItem(false)]
public class LabelRotate : Control, IStyleInterface
public class LabelRotate : Control, IStyleInterface, IZoomScale
{
private float m_textAngle = 0;
private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter;
private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft;
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
[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)]
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, IControlScale
public sealed class UIAvatar : UIControl, ISymbol, IZoomScale
{
/// <summary>
/// 头像图标类型
@ -356,10 +356,11 @@ namespace Sunny.UI
PaintAgain?.Invoke(this, e);
}
public void SetControlScale(float scale)
public override void SetZoomScale(float scale)
{
avatarSize = UIDPIScale.Calc(baseAvatorSize, scale);
symbolSize = UIDPIScale.Calc(baseSymbolSize, scale);
base.SetZoomScale(scale);
avatarSize = UIZoomScale.Calc(baseAvatorSize, scale);
symbolSize = UIZoomScale.Calc(baseSymbolSize, scale);
Invalidate();
}
}

View File

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

View File

@ -36,7 +36,7 @@ namespace Sunny.UI
/// 控件基类
/// </summary>
[ToolboxItem(false)]
public class UIControl : Control, IStyleInterface
public class UIControl : Control, IStyleInterface, IZoomScale
{
/// <summary>
/// 构造函数
@ -49,25 +49,16 @@ namespace Sunny.UI
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("禁止控件跟随窗体缩放")]
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;

View File

@ -38,7 +38,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
public class UIDataGridView : DataGridView, IStyleInterface
public class UIDataGridView : DataGridView, IStyleInterface, IZoomScale
{
private readonly UIScrollBar VBar = new UIScrollBar();
private readonly UIHorScrollBarEx HBar = new UIHorScrollBarEx();
@ -101,23 +101,14 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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)]

View File

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

View File

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

View File

@ -23,7 +23,6 @@
* 2022-03-19: V3.1.1
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
@ -33,7 +32,7 @@ namespace Sunny.UI
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public class UILabel : Label, IStyleInterface
public class UILabel : Label, IStyleInterface, IZoomScale
{
public UILabel()
{
@ -44,23 +43,14 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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;
@ -173,7 +163,7 @@ namespace Sunny.UI
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public sealed class UILinkLabel : LinkLabel, IStyleInterface
public sealed class UILinkLabel : LinkLabel, IStyleInterface, IZoomScale
{
public UILinkLabel()
{
@ -189,23 +179,14 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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)]

View File

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

View File

@ -34,7 +34,7 @@ namespace Sunny.UI
[DefaultEvent("MenuItemClick")]
[DefaultProperty("Nodes")]
[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();
@ -65,23 +65,16 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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);
}
_nodeInterval = UIZoomScale.Calc(baseNodeInterval, scale);
nodeSize = UIZoomScale.Calc(baseNodeSize, scale);
Invalidate();
}
[Browsable(false)]
@ -562,6 +555,7 @@ namespace Sunny.UI
}
private int _nodeInterval = 100;
private int baseNodeInterval = 100;
[DefaultValue(100)]
[Description("显示菜单边距"), Category("SunnyUI")]
@ -572,13 +566,14 @@ namespace Sunny.UI
{
if (_nodeInterval != value)
{
_nodeInterval = value;
baseNodeInterval = _nodeInterval = value;
Invalidate();
}
}
}
private Size nodeSize = new Size(130, 45);
private Size baseNodeSize = new Size(130, 45);
[DefaultValue(typeof(Size), "130, 45")]
[Description("显示菜单大小"), Category("SunnyUI")]
@ -589,7 +584,7 @@ namespace Sunny.UI
{
if (nodeSize != value)
{
nodeSize = value;
baseNodeSize = nodeSize = value;
Invalidate();
}
}

View File

@ -39,7 +39,7 @@ namespace Sunny.UI
{
[DefaultEvent("MenuItemClick")]
[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);
@ -47,9 +47,6 @@ namespace Sunny.UI
private readonly UIScrollBar Bar = new UIScrollBar();
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public UINavMenu()
{
SetStyle(ControlStyles.DoubleBuffer |
@ -90,21 +87,15 @@ namespace Sunny.UI
selectedHighColor = UIStyles.Blue.NavMenuMenuSelectedColor;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ZoomScaleDisabled { get; set; }
[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")]

View File

@ -32,7 +32,7 @@ namespace Sunny.UI
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public sealed class UISmoothLabel : Label, IStyleInterface
public sealed class UISmoothLabel : Label, IStyleInterface, IZoomScale
{
public UISmoothLabel()
{
@ -49,7 +49,15 @@ namespace Sunny.UI
}
[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)
{
@ -63,23 +71,6 @@ namespace Sunny.UI
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 SizeF drawSize;
private Pen drawPen;

View File

@ -26,7 +26,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
public sealed class UISplitContainer : SplitContainer, IStyleInterface
public sealed class UISplitContainer : SplitContainer, IStyleInterface, IZoomScale
{
private enum UIMouseType
{
@ -68,9 +68,6 @@ 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 |
@ -82,21 +79,15 @@ namespace Sunny.UI
Version = UIGlobal.Version;
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ZoomScaleDisabled { get; set; }
[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)

View File

@ -38,7 +38,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
public sealed class UITabControl : TabControl, IStyleInterface
public sealed class UITabControl : TabControl, IStyleInterface, IZoomScale
{
private readonly UITabControlHelper Helper;
private int DrawedIndex = -1;
@ -73,23 +73,14 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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>();

View File

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

View File

@ -21,7 +21,6 @@
******************************************************************************/
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
@ -33,15 +32,9 @@ namespace Sunny.UI
Version = UIGlobal.Version;
}
[Browsable(false)]
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)

View File

@ -31,7 +31,7 @@ namespace Sunny.UI
{
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
[DefaultEvent("Click"), DefaultProperty("Text")]
public partial class UIUserControl : UserControl, IStyleInterface
public partial class UIUserControl : UserControl, IStyleInterface, IZoomScale
{
private int radius = 5;
protected Color rectColor = UIStyles.Blue.PanelRectColor;
@ -51,7 +51,15 @@ namespace Sunny.UI
}
[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)]
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)
{
base.OnVisibleChanged(e);
@ -355,16 +360,6 @@ namespace Sunny.UI
{
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

@ -83,7 +83,7 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[Browsable(false)]
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")]
[Description("设计界面大小"), Category("SunnyUI")]
public Size DesignedSize
public Size ZoomScaleSize
{
get;
set;
}
[Browsable(false)]
public ControlScaleInfo DesignedRect { get; private set; }
public Rectangle ZoomScaleRect { get; set; }
private void SetControlScale()
private void SetZoomScale()
{
if (ForbidControlScale) return;
if (!UIStyles.DPIScale || !UIStyles.ControlScale) return;
if (DesignedRect.Width == 0 || DesignedRect.Height == 0) return;
if (ZoomScaleDisabled) return;
if (!UIStyles.DPIScale || !UIStyles.ZoomScale) return;
if (ZoomScaleRect.Width == 0 || ZoomScaleRect.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;
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()
{
@ -1318,7 +1318,7 @@ namespace Sunny.UI
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
SetControlScale();
SetZoomScale();
CalcSystemBoxPos();
if (isShow)
@ -1352,10 +1352,10 @@ namespace Sunny.UI
SetRadius();
isShow = true;
SetDPIScale();
SetDesignedSize();
SetZoomScaleRect();
}
public event OnDesignSizeChanged DesignSizeChanged;
public event OnZoomScaleRectChanged ZoomScaleRectChanged;
/// <summary>
/// 是否显示圆角

View File

@ -30,9 +30,9 @@ using System.Windows.Forms;
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
{

View File

@ -37,7 +37,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultEvent("Initialize")]
public partial class UIPage : Form, IStyleInterface, ISymbol
public partial class UIPage : Form, IStyleInterface, ISymbol, IZoomScale
{
public readonly Guid Guid = Guid.NewGuid();
private Color _rectColor = UIColor.Blue;
@ -77,23 +77,14 @@ namespace Sunny.UI
}
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ForbidControlScale { get; set; }
public bool ZoomScaleDisabled { get; set; }
[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)]

View File

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

View File

@ -59,61 +59,6 @@ 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(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>

View File

@ -38,7 +38,7 @@ namespace Sunny.UI
{
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;

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;
}
}
}
}
}