From 1dd949dcc5823229bf2227050bbf3cbdd4193893 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 23 Jan 2024 14:17:59 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIDigitalLabel:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BB=98=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UIDigitalLabel.cs | 42 ++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/SunnyUI/Controls/UIDigitalLabel.cs b/SunnyUI/Controls/UIDigitalLabel.cs index fa0916f2..f95fca45 100644 --- a/SunnyUI/Controls/UIDigitalLabel.cs +++ b/SunnyUI/Controls/UIDigitalLabel.cs @@ -17,6 +17,7 @@ * 创建日期: 2023-12-01 * * 2023-12-01: V3.6.1 增加文件说明 + * 2024-01-23: V3.6.3 更新绘制 ******************************************************************************/ /****************************************************************************** @@ -44,7 +45,7 @@ namespace Sunny.UI { SetStyleFlags(); Size = new Size(208, 42); - TextAlign = ContentAlignment.MiddleRight; + TextAlign = HorizontalAlignment.Right; ShowText = ShowRect = ShowFill = false; ForeColor = Color.Lime; BackColor = Color.Black; @@ -101,12 +102,43 @@ namespace Sunny.UI /// 绘图参数 protected override void OnPaint(PaintEventArgs e) { - //e.Graphics.Clear(FillColor); + using Font font = DigitalFont.Instance.GetFont(DigitalSize); + using Brush br = new SolidBrush(ForeColor); - using (Font font = DigitalFont.Instance.GetFont(DigitalSize)) + string text = Value.ToString("F" + DecimalPlaces); + SizeF sf = e.Graphics.MeasureString(text, font); + float y = (Height - sf.Height) / 2.0f + 1 + Padding.Top; + float x = Padding.Left; + switch (TextAlign) { - string text = Value.ToString("F" + DecimalPlaces); - e.Graphics.DrawString(text, font, ForeColor, new Rectangle(0, 0, Width, Height), TextAlign, TextOffset.X, TextOffset.Y); + case HorizontalAlignment.Right: + x = Width - sf.Width - Padding.Right; + break; + case HorizontalAlignment.Center: + x = (Width - sf.Width) / 2.0f; + break; + } + + e.Graphics.DrawString(text, font, br, x, y); + } + + private HorizontalAlignment textAlign = HorizontalAlignment.Right; + + /// + /// 文字对齐方向 + /// + [Description("文字对齐方向"), Category("SunnyUI")] + [DefaultValue(HorizontalAlignment.Right)] + public new HorizontalAlignment TextAlign + { + get => textAlign; + set + { + if (textAlign != value) + { + textAlign = value; + Invalidate(); + } } }