* 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;
}
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>
@ -401,10 +452,10 @@ namespace Sunny.UI
object obj = f1.GetValue(button);
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);
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)
{
m_selectedColor.Lightness = lightness;

View File

@ -11,6 +11,17 @@ namespace Sunny.UI
private ContentAlignment m_rotatePointAlignment = ContentAlignment.MiddleCenter;
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
{
get { return base.Text; }

View File

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

View File

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

View File

@ -45,6 +45,17 @@ namespace Sunny.UI
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)
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);

View File

@ -98,6 +98,17 @@ namespace Sunny.UI
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>();
public class CellStyle

View File

@ -33,7 +33,7 @@ using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultEvent("ItemClick")]
public sealed partial class UIImageListBox : UIPanel,IToolTip
public sealed partial class UIImageListBox : UIPanel, IToolTip
{
private readonly ImageListBox listbox = new ImageListBox();
private readonly UIScrollBar bar = new UIScrollBar();
@ -136,7 +136,7 @@ namespace Sunny.UI
{
listbox.SetScrollInfo();
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)
//{
// if (IsDisposed || Disposing) return;

View File

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

View File

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

View File

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

View File

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

View File

@ -52,6 +52,17 @@ namespace Sunny.UI
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)
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);

View File

@ -62,6 +62,17 @@ namespace Sunny.UI
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)
{
base.Dispose(disposing);
@ -457,7 +468,7 @@ namespace Sunny.UI
Rectangle TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y - 2, ItemSize.Width, ItemSize.Height);
if (Alignment == TabAlignment.Bottom)
{
TabRect = new Rectangle(GetTabRect(index).Location.X- 2, GetTabRect(index).Location.Y + 2, ItemSize.Width, ItemSize.Height);
TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y + 2, ItemSize.Width, ItemSize.Height);
}
Bitmap bmp = new Bitmap(TabRect.Width, TabRect.Height);
Graphics g = Graphics.FromImage(bmp);
@ -479,10 +490,10 @@ namespace Sunny.UI
if (TabSelectedHighColorSize > 0)
g.FillRectangle(TabSelectedHighColor, 0, bmp.Height - TabSelectedHighColorSize, bmp.Width, TabSelectedHighColorSize);
}
if (Alignment == TabAlignment.Bottom)
{
g.DrawString(TabPages[index].Text, Font, index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor, textLeft, (TabRect.Height - sf.Height - TabSelectedHighColorSize) / 2.0f);
g.DrawString(TabPages[index].Text, Font, index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor, textLeft, (TabRect.Height - sf.Height - TabSelectedHighColorSize) / 2.0f);
}
else
{

View File

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

View File

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

View File

@ -70,6 +70,22 @@ namespace Sunny.UI
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)]
[Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")]
public bool Movable { get; set; } = true;
@ -1183,6 +1199,7 @@ namespace Sunny.UI
CalcSystemBoxPos();
SetRadius();
isShow = true;
SetDPIScale();
}
/// <summary>

View File

@ -65,6 +65,23 @@ namespace Sunny.UI
if (!IsDesignMode) base.Dock = DockStyle.Fill;
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()

View File

@ -20,6 +20,7 @@
* 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;
@ -56,6 +57,10 @@ namespace Sunny.UI
void SetStyleColor(UIBaseStyle uiColor);
void SetStyle(UIStyle style);
bool IsScaled { get; }
void SetDPIScale();
}
/// <summary>
@ -189,6 +194,8 @@ namespace Sunny.UI
/// </summary>
public static class UIStyles
{
public static bool DPIScale { get; set; }
public static List<UIStyle> PopularStyles()
{
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()
{
foreach (var form in Forms.Values)

View File

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