* UIStyle: 修改DPI自适应缩放

This commit is contained in:
Sunny 2021-11-17 23:46:56 +08:00
parent 45fad139bc
commit ebfef9c105
5 changed files with 26 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@ -329,6 +329,12 @@ namespace Sunny.UI
control.Font.Style, control.Font.Unit, control.Font.GdiCharSet); control.Font.Style, control.Font.Unit, control.Font.GdiCharSet);
} }
public static Font DPIScaleFont(this Control control, Font font)
{
return new Font(font.FontFamily, font.Size / control.DPIScale(),
font.Style, font.Unit, font.GdiCharSet);
}
public static void SetDPIScaleFont(this Control control) public static void SetDPIScaleFont(this Control control)
{ {
if (!control.DPIScale().Equals(1)) if (!control.DPIScale().Equals(1))
@ -490,7 +496,6 @@ namespace Sunny.UI
private static bool GetChildNodeCheckedState(TreeNode node) private static bool GetChildNodeCheckedState(TreeNode node)
{ {
if (node.Nodes.Count > 0) if (node.Nodes.Count > 0)
{ {
var count = node.Nodes.Cast<TreeNode>().Where(n => n.Checked).ToList().Count; var count = node.Nodes.Cast<TreeNode>().Where(n => n.Checked).ToList().Count;
@ -505,8 +510,6 @@ namespace Sunny.UI
return GetChildNodeCheckedState(nd); return GetChildNodeCheckedState(nd);
} }
} }
} }
return false; return false;
} }

View File

@ -79,6 +79,12 @@ namespace Sunny.UI
if (!IsScaled && UIStyles.DPIScale) if (!IsScaled && UIStyles.DPIScale)
{ {
this.SetDPIScaleFont(); this.SetDPIScaleFont();
if (!this.DPIScale().Equals(1))
{
this.TitleFont = this.DPIScaleFont(this.TitleFont);
}
foreach (Control control in this.GetAllDPIScaleControls()) foreach (Control control in this.GetAllDPIScaleControls())
{ {
control.SetDPIScaleFont(); control.SetDPIScaleFont();
@ -444,7 +450,6 @@ namespace Sunny.UI
} }
} }
private Rectangle ControlBoxRect; private Rectangle ControlBoxRect;
private Rectangle MaximizeBoxRect; private Rectangle MaximizeBoxRect;
@ -1509,12 +1514,14 @@ namespace Sunny.UI
case Win32.User.WM_ERASEBKGND: case Win32.User.WM_ERASEBKGND:
m.Result = IntPtr.Zero; m.Result = IntPtr.Zero;
break; break;
case Win32.User.WM_HOTKEY: case Win32.User.WM_HOTKEY:
int hotKeyId = (int)(m.WParam); int hotKeyId = (int)(m.WParam);
if (hotKeys.ContainsKey(hotKeyId)) if (hotKeys.ContainsKey(hotKeyId))
HotKeyEventHandler?.Invoke(this, new HotKeyEventArgs(hotKeys[hotKeyId], DateTime.Now)); HotKeyEventHandler?.Invoke(this, new HotKeyEventArgs(hotKeys[hotKeyId], DateTime.Now));
break; break;
default: default:
base.WndProc(ref m); base.WndProc(ref m);
break; break;
@ -1842,7 +1849,7 @@ namespace Sunny.UI
UINotifierHelper.ShowNotifier(desc, UINotifierType.ERROR, UILocalize.ErrorTitle, isDialog, timeout); UINotifierHelper.ShowNotifier(desc, UINotifierType.ERROR, UILocalize.ErrorTitle, isDialog, timeout);
} }
#endregion #endregion
#region IFrame实现 #region IFrame实现
@ -1889,7 +1896,6 @@ namespace Sunny.UI
public virtual void Feedback(object sender, int pageIndex, params object[] objects) public virtual void Feedback(object sender, int pageIndex, params object[] objects)
{ {
} }
#endregion IFrame实现 #endregion IFrame实现

View File

@ -30,6 +30,7 @@ using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Design; using System.Drawing.Design;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Sunny.UI namespace Sunny.UI
{ {
@ -76,6 +77,11 @@ namespace Sunny.UI
if (!IsScaled) if (!IsScaled)
{ {
this.SetDPIScaleFont(); this.SetDPIScaleFont();
if (!this.DPIScale().Equals(1))
{
this.TitleFont = this.DPIScaleFont(this.TitleFont);
}
foreach (Control control in this.GetAllDPIScaleControls()) foreach (Control control in this.GetAllDPIScaleControls())
{ {
control.SetDPIScaleFont(); control.SetDPIScaleFont();
@ -145,7 +151,6 @@ namespace Sunny.UI
protected virtual void SymbolChange() protected virtual void SymbolChange()
{ {
} }
[Browsable(false)] [Browsable(false)]
@ -239,6 +244,7 @@ namespace Sunny.UI
} }
public event EventHandler Initialize; public event EventHandler Initialize;
public event EventHandler Finalize; public event EventHandler Finalize;
protected override void OnControlAdded(ControlEventArgs e) protected override void OnControlAdded(ControlEventArgs e)
@ -533,7 +539,6 @@ namespace Sunny.UI
InControlBox = inControlBox; InControlBox = inControlBox;
Invalidate(); Invalidate();
} }
} }
} }
@ -574,7 +579,6 @@ namespace Sunny.UI
} }
} }
protected override void OnTextChanged(EventArgs e) protected override void OnTextChanged(EventArgs e)
{ {
base.OnTextChanged(e); base.OnTextChanged(e);
@ -584,12 +588,10 @@ namespace Sunny.UI
private void CalcSystemBoxPos() private void CalcSystemBoxPos()
{ {
ControlBoxRect = new Rectangle(Width - 6 - 28, titleHeight / 2 - 14, 28, 28); ControlBoxRect = new Rectangle(Width - 6 - 28, titleHeight / 2 - 14, 28, 28);
} }
private Rectangle ControlBoxRect; private Rectangle ControlBoxRect;
/// <summary> /// <summary>
/// 是否显示窗体的控制按钮 /// 是否显示窗体的控制按钮
/// </summary> /// </summary>
@ -894,6 +896,6 @@ namespace Sunny.UI
UINotifierHelper.ShowNotifier(desc, UINotifierType.ERROR, UILocalize.ErrorTitle, isDialog, timeout); UINotifierHelper.ShowNotifier(desc, UINotifierType.ERROR, UILocalize.ErrorTitle, isDialog, timeout);
} }
#endregion #endregion
} }
} }