* UIStyle: 增加系统DPI缩放自适应

* UIStyleManager: 增加系统DPI缩放自适应
This commit is contained in:
Sunny 2021-10-16 20:39:12 +08:00
parent 030db76dd8
commit 4c8d722ce6
21 changed files with 298 additions and 7 deletions

Binary file not shown.

View File

@ -303,6 +303,57 @@ namespace Sunny.UI
return values; return values;
} }
public static List<Control> GetAllControls(this Control control)
{
var list = new List<Control>();
foreach (Control con in control.Controls)
{
list.Add(con);
if (con.Controls.Count > 0)
{
list.AddRange(GetAllControls(con));
}
}
return list;
}
public static float DPIScale(this Control control)
{
return control.CreateGraphics().DpiX / 96.0f;
}
public static Font DPIScaleFont(this Control control)
{
return new Font(control.Font.FontFamily, control.Font.Size / control.DPIScale(),
control.Font.Style, control.Font.Unit, control.Font.GdiCharSet);
}
public static void SetDPIScaleFont(this Control control)
{
if (!control.DPIScale().Equals(1))
{
control.Font = control.DPIScaleFont();
}
}
public static List<Control> GetAllDPIScaleControls(this Control control)
{
var list = new List<Control>();
foreach (Control con in control.Controls)
{
list.Add(con);
if (con is IToolTip) continue;
if (con.Controls.Count > 0)
{
list.AddRange(GetAllControls(con));
}
}
return list;
}
/// <summary> /// <summary>
/// 查找包含接口名称的控件列表 /// 查找包含接口名称的控件列表
/// </summary> /// </summary>
@ -401,10 +452,10 @@ namespace Sunny.UI
object obj = f1.GetValue(button); object obj = f1.GetValue(button);
PropertyInfo pi = button.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance); PropertyInfo pi = button.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
if (pi != null) if (pi != null && obj != null)
{ {
EventHandlerList list = (EventHandlerList)pi.GetValue(button, null); EventHandlerList list = (EventHandlerList)pi.GetValue(button, null);
list.RemoveHandler(obj, list[obj]); list?.RemoveHandler(obj, list[obj]);
} }
} }

View File

@ -33,6 +33,17 @@ namespace Sunny.UI
} }
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
public void SetLightness(double lightness) public void SetLightness(double lightness)
{ {
m_selectedColor.Lightness = lightness; m_selectedColor.Lightness = lightness;

View File

@ -11,6 +11,17 @@ namespace Sunny.UI
private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter; private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter;
private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft; private ContentAlignment m_textAlignment = ContentAlignment.MiddleLeft;
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
public new string Text public new string Text
{ {
get { return base.Text; } get { return base.Text; }

View File

@ -49,6 +49,17 @@ namespace Sunny.UI
Width = 150; Width = 150;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
private UIStyle _style = UIStyle.Blue; private UIStyle _style = UIStyle.Blue;
protected override void OnBackColorChanged(EventArgs e) protected override void OnBackColorChanged(EventArgs e)

View File

@ -38,6 +38,17 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
/// <summary> /// <summary>
/// 自定义主题风格 /// 自定义主题风格
/// </summary> /// </summary>

View File

@ -45,6 +45,17 @@ namespace Sunny.UI
base.MinimumSize = new Size(1, 1); base.MinimumSize = new Size(1, 1);
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false) protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false)
{ {
SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true);

View File

@ -98,6 +98,17 @@ namespace Sunny.UI
HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged; HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
private readonly Dictionary<string, CellStyle> CellStyles = new Dictionary<string, CellStyle>(); private readonly Dictionary<string, CellStyle> CellStyles = new Dictionary<string, CellStyle>();
public class CellStyle public class CellStyle

View File

@ -136,7 +136,7 @@ namespace Sunny.UI
{ {
listbox.SetScrollInfo(); listbox.SetScrollInfo();
LastCount = Items.Count; LastCount = Items.Count;
ItemsCountChange?.Invoke(this, null); ItemsCountChange?.Invoke(this, EventArgs.Empty);
} }
} }
@ -368,6 +368,17 @@ namespace Sunny.UI
} }
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
//protected override void WndProc(ref Message m) //protected override void WndProc(ref Message m)
//{ //{
// if (IsDisposed || Disposing) return; // if (IsDisposed || Disposing) return;

View File

@ -42,6 +42,17 @@ namespace Sunny.UI
ForeColorChanged += UILabel_ForeColorChanged; ForeColorChanged += UILabel_ForeColorChanged;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
private void UILabel_ForeColorChanged(object sender, EventArgs e) private void UILabel_ForeColorChanged(object sender, EventArgs e)
{ {
_style = UIStyle.Custom; _style = UIStyle.Custom;
@ -128,6 +139,17 @@ namespace Sunny.UI
LinkColor = UIColor.Blue; LinkColor = UIColor.Blue;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
/// <summary> /// <summary>
/// Tag字符串 /// Tag字符串
/// </summary> /// </summary>

View File

@ -605,6 +605,17 @@ namespace Sunny.UI
[Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")] [Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")]
public string TagString { get; set; } public string TagString { get; set; }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
public UIScrollBar Bar public UIScrollBar Bar
{ {
get => bar; get => bar;

View File

@ -60,6 +60,17 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
private int radius; private int radius;
[DefaultValue(0)] [DefaultValue(0)]

View File

@ -80,6 +80,17 @@ namespace Sunny.UI
SetScrollInfo(); SetScrollInfo();
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
[DefaultValue(false)] [DefaultValue(false)]
[Description("只显示一个打开的节点"), Category("SunnyUI")] [Description("只显示一个打开的节点"), Category("SunnyUI")]
public bool ShowOneNode { get; set; } public bool ShowOneNode { get; set; }

View File

@ -52,6 +52,17 @@ namespace Sunny.UI
SetStyleFlags(true, false); SetStyleFlags(true, false);
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false) protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false)
{ {
SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true);

View File

@ -62,6 +62,17 @@ namespace Sunny.UI
timer.Tick += Timer_Tick; timer.Tick += Timer_Tick;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
base.Dispose(disposing); base.Dispose(disposing);

View File

@ -46,6 +46,17 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
/// <summary> /// <summary>
/// 自定义主题风格 /// 自定义主题风格
/// </summary> /// </summary>

View File

@ -32,6 +32,17 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
protected override void OnControlAdded(ControlEventArgs e) protected override void OnControlAdded(ControlEventArgs e)
{ {
base.OnControlAdded(e); base.OnControlAdded(e);

View File

@ -70,6 +70,22 @@ namespace Sunny.UI
showTitleIcon = false; showTitleIcon = false;
} }
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled && UIStyles.DPIScale)
{
this.SetDPIScaleFont();
foreach (Control control in this.GetAllDPIScaleControls())
{
control.SetDPIScaleFont();
}
IsScaled = true;
}
}
[DefaultValue(true)] [DefaultValue(true)]
[Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")] [Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")]
public bool Movable { get; set; } = true; public bool Movable { get; set; } = true;
@ -1183,6 +1199,7 @@ namespace Sunny.UI
CalcSystemBoxPos(); CalcSystemBoxPos();
SetRadius(); SetRadius();
isShow = true; isShow = true;
SetDPIScale();
} }
/// <summary> /// <summary>

View File

@ -65,6 +65,23 @@ namespace Sunny.UI
if (!IsDesignMode) base.Dock = DockStyle.Fill; if (!IsDesignMode) base.Dock = DockStyle.Fill;
Version = UIGlobal.Version; Version = UIGlobal.Version;
SetDPIScale();
}
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
foreach (Control control in this.GetAllDPIScaleControls())
{
control.SetDPIScaleFont();
}
IsScaled = true;
}
} }
public void Render() public void Render()

View File

@ -20,6 +20,7 @@
* 2021-07-12: V3.0.5 * 2021-07-12: V3.0.5
* 2021-07-18: V3.0.5 * 2021-07-18: V3.0.5
* 2021-09-24: V3.0.7 GdiCharSet * 2021-09-24: V3.0.7 GdiCharSet
* 2021-10-16: V3.0.8 DPI缩放自适应
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -56,6 +57,10 @@ namespace Sunny.UI
void SetStyleColor(UIBaseStyle uiColor); void SetStyleColor(UIBaseStyle uiColor);
void SetStyle(UIStyle style); void SetStyle(UIStyle style);
bool IsScaled { get; }
void SetDPIScale();
} }
/// <summary> /// <summary>
@ -189,6 +194,8 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static class UIStyles public static class UIStyles
{ {
public static bool DPIScale { get; set; }
public static List<UIStyle> PopularStyles() public static List<UIStyle> PopularStyles()
{ {
List<UIStyle> styles = new List<UIStyle>(); List<UIStyle> styles = new List<UIStyle>();
@ -503,6 +510,21 @@ namespace Sunny.UI
} }
} }
public static void SetDPIScale()
{
foreach (var form in Forms.Values)
{
if (!form.DPIScale().EqualsFloat(1))
form.SetDPIScale();
}
foreach (var page in Pages.Values)
{
if (!page.DPIScale().EqualsFloat(1))
page.SetDPIScale();
}
}
public static void Translate() public static void Translate()
{ {
foreach (var form in Forms.Values) foreach (var form in Forms.Values)

View File

@ -17,6 +17,7 @@
* : 2020-01-01 * : 2020-01-01
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2021-10-16: V3.0.8 DPI缩放自适应
******************************************************************************/ ******************************************************************************/
using System.ComponentModel; using System.ComponentModel;
@ -67,5 +68,12 @@ namespace Sunny.UI
{ {
container.Add(this); container.Add(this);
} }
[DefaultValue(false), Description("DPI缩放"), Category("SunnyUI")]
public bool DPIScale
{
get => UIStyles.DPIScale;
set => UIStyles.DPIScale = value;
}
} }
} }