From 16972fcf48c55933d6b15b2b73a95d52610f5413 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 25 Oct 2023 17:19:20 +0800 Subject: [PATCH] =?UTF-8?q?*=20UITextBox:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E9=AB=98DPI=E4=B8=8B=EF=BC=8C=E6=96=87=E5=AD=97=E5=9E=82?= =?UTF-8?q?=E7=9B=B4=E4=B8=8D=E5=B1=85=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20*=20UITextBox:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=E6=9F=90?= =?UTF-8?q?=E4=BA=9B=E5=AD=97=E4=BD=93=E4=B8=8D=E6=98=BE=E7=A4=BA=E4=B8=8B?= =?UTF-8?q?=E5=88=92=E7=BA=BF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UITextBox.cs | 52 +++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/SunnyUI/Controls/UITextBox.cs b/SunnyUI/Controls/UITextBox.cs index 1610b294..d1e19197 100644 --- a/SunnyUI/Controls/UITextBox.cs +++ b/SunnyUI/Controls/UITextBox.cs @@ -51,6 +51,8 @@ * 2023-07-16: V3.4.0 修复了Enabled为false时,PasswordChar失效的问题 * 2023-08-17: V3.4.1 修复了Enabled为false时,字体大小调整后,文字显示位置的问题 * 2023-08-24: V3.4.2 修复了Enabled为false时,自定义颜色,文字不显示的问题 + * 2023-10-25: V3.5.1 修复在高DPI下,文字垂直不居中的问题 + * 2023-10-25: V3.5.1 修复在某些字体不显示下划线的问题 ******************************************************************************/ using System; @@ -79,10 +81,7 @@ namespace Sunny.UI ShowText = false; MinimumSize = new Size(1, 16); - Width = 150; - Height = 29; - - edit.AutoSize = false; + edit.AutoSize = true; edit.Top = (Height - edit.Height) / 2; edit.Left = 4; edit.Width = Width - 8; @@ -108,6 +107,10 @@ namespace Sunny.UI edit.SelectionChanged += Edit_SelectionChanged; edit.MouseClick += Edit_MouseClick; edit.MouseDoubleClick += Edit_MouseDoubleClick; + edit.SizeChanged += Edit_SizeChanged; + + Width = 150; + Height = 29; btn.Parent = this; btn.Visible = false; @@ -133,11 +136,21 @@ namespace Sunny.UI TextAlignment = ContentAlignment.MiddleLeft; SizeChange(); - + lastEditHeight = edit.Height; editCursor = Cursor; TextAlignmentChange += UITextBox_TextAlignmentChange; } + int lastEditHeight = -1; + private void Edit_SizeChanged(object sender, EventArgs e) + { + if (lastEditHeight != edit.Height) + { + lastEditHeight = edit.Height; + SizeChange(); + } + } + public override void SetDPIScale() { base.SetDPIScale(); @@ -660,7 +673,11 @@ namespace Sunny.UI { base.OnFontChanged(e); - if (DefaultFontSize < 0 && edit != null) edit.Font = this.Font; + if (DefaultFontSize < 0 && edit != null) + { + edit.Font = this.Font; + edit.Invalidate(); + } SizeChange(); Invalidate(); @@ -673,7 +690,11 @@ namespace Sunny.UI protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); - SizeChange(); + + if (!NoNeedChange) + { + SizeChange(); + } if (tipsBtn != null) { @@ -700,6 +721,8 @@ namespace Sunny.UI } } + private bool NoNeedChange = false; + private void SizeChange() { if (!InitializeComponentEnd) return; @@ -708,11 +731,18 @@ namespace Sunny.UI if (!multiline) { - if (Height < UIGlobal.EditorMinHeight) Height = UIGlobal.EditorMinHeight; - if (Height > UIGlobal.EditorMaxHeight) Height = UIGlobal.EditorMaxHeight; + if (Height < edit.Height + RectSize * 2 + 2) + { + NoNeedChange = true; + Height = edit.Height + RectSize * 2 + 2; + edit.Top = (Height - edit.Height) / 2; + NoNeedChange = false; + } - edit.Height = Math.Min(Height - RectSize * 2, edit.PreferredHeight); - edit.Top = (Height - edit.Height) / 2; + if (edit.Top != (Height - edit.Height) / 2) + { + edit.Top = (Height - edit.Height) / 2; + } if (icon == null && Symbol == 0) {