* UIDigitalLabel: 更新绘制

This commit is contained in:
Sunny 2024-01-23 14:17:59 +08:00
parent ed559a1a21
commit 1dd949dcc5

View File

@ -17,6 +17,7 @@
* : 2023-12-01 * : 2023-12-01
* *
* 2023-12-01: V3.6.1 * 2023-12-01: V3.6.1
* 2024-01-23: V3.6.3
******************************************************************************/ ******************************************************************************/
/****************************************************************************** /******************************************************************************
@ -44,7 +45,7 @@ namespace Sunny.UI
{ {
SetStyleFlags(); SetStyleFlags();
Size = new Size(208, 42); Size = new Size(208, 42);
TextAlign = ContentAlignment.MiddleRight; TextAlign = HorizontalAlignment.Right;
ShowText = ShowRect = ShowFill = false; ShowText = ShowRect = ShowFill = false;
ForeColor = Color.Lime; ForeColor = Color.Lime;
BackColor = Color.Black; BackColor = Color.Black;
@ -101,12 +102,43 @@ namespace Sunny.UI
/// <param name="e">绘图参数</param> /// <param name="e">绘图参数</param>
protected override void OnPaint(PaintEventArgs e) 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); case HorizontalAlignment.Right:
e.Graphics.DrawString(text, font, ForeColor, new Rectangle(0, 0, Width, Height), TextAlign, TextOffset.X, TextOffset.Y); 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;
/// <summary>
/// 文字对齐方向
/// </summary>
[Description("文字对齐方向"), Category("SunnyUI")]
[DefaultValue(HorizontalAlignment.Right)]
public new HorizontalAlignment TextAlign
{
get => textAlign;
set
{
if (textAlign != value)
{
textAlign = value;
Invalidate();
}
} }
} }