+ UIDigitalLabel: 增加冷液晶显示LCD标签

This commit is contained in:
Sunny 2023-12-01 22:30:01 +08:00
parent 7c7cfb592d
commit 5ca057ab6c
4 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,136 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2023 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIDigitalLabel.cs
* : LCD标签
* : V3.6.1
* : 2023-12-01
*
* 2023-12-01: V3.6.1
******************************************************************************/
/******************************************************************************
* sa-digital-number.ttf
* Digital Numbers Fonts是一种固定宽度webLCD
* SIL Open Font License 1.1
* https://github.com/s-a/digital-numbers-font
* Copyright (c) 2015, Stephan Ahlf (stephan.ahlf@googlemail.com)
* This Font Software is licensed under the SIL Open Font License, Version 1.1.
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultEvent("ValueChanged")]
[DefaultProperty("Value")]
[ToolboxItem(true)]
public class UIDigitalLabel : UIControl
{
public UIDigitalLabel()
{
SetStyleFlags();
Size = new Size(208, 42);
TextAlign = ContentAlignment.MiddleRight;
ShowText = ShowRect = ShowFill = false;
ForeColor = Color.Lime;
BackColor = Color.Black;
}
private double digitalValue;
[Description("浮点数"), Category("SunnyUI")]
[DefaultValue(typeof(double), "0")]
public double Value
{
get => digitalValue;
set
{
digitalValue = value;
ValueChanged?.Invoke(this, EventArgs.Empty);
Invalidate();
}
}
public event EventHandler ValueChanged;
private int digitalSize = 24;
[Description("LCD字体大小"), Category("SunnyUI")]
[DefaultValue(24)]
public int DigitalSize
{
get => digitalSize;
set
{
digitalSize = Math.Max(9, value);
Invalidate();
}
}
private int decimalPlaces = 2;
[Description("浮点数,显示文字小数位数"), Category("SunnyUI")]
[DefaultValue(2)]
public int DecimalPlaces
{
get => decimalPlaces;
set
{
decimalPlaces = Math.Max(0, value);
Invalidate();
}
}
/// <summary>
/// 重载绘图
/// </summary>
/// <param name="e">绘图参数</param>
protected override void OnPaint(PaintEventArgs e)
{
//e.Graphics.Clear(FillColor);
using (Font font = DigitalFont.Instance.GetFont(DigitalSize))
{
string text = Value.ToString("F" + DecimalPlaces);
e.Graphics.DrawString(text, font, ForeColor, new Rectangle(0, 0, Width, Height), TextAlign, TextOffset.X, TextOffset.Y);
}
}
private Point textOffset = new Point(0, 0);
[Description("文字偏移"), Category("SunnyUI")]
[DefaultValue(typeof(Point), "0, 0")]
public Point TextOffset
{
get => textOffset;
set
{
textOffset = value;
Invalidate();
}
}
/// <summary>
/// 设置主题样式
/// </summary>
/// <param name="uiColor">主题样式</param>
public override void SetStyleColor(UIBaseStyle uiColor)
{
}
}
}

View File

@ -0,0 +1,51 @@
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace Sunny.UI
{
public class DigitalFont
{
private static DigitalFont _instance = null;
public static DigitalFont Instance
{
get
{
if (_instance == null) _instance = new DigitalFont();
return _instance;
}
}
private readonly PrivateFontCollection ImageFont;
public DigitalFont()
{
byte[] buffer = ReadFontFileFromResource("Sunny.UI.Font.sa-digital-number.ttf");
ImageFont = new PrivateFontCollection();
var memoryFont = Marshal.AllocCoTaskMem(buffer.Length);
Marshal.Copy(buffer, 0, memoryFont, buffer.Length);
ImageFont.AddMemoryFont(memoryFont, buffer.Length);
}
public Font GetFont(float size)
{
return new Font(ImageFont.Families[0], size, FontStyle.Regular, GraphicsUnit.Point);
}
private byte[] ReadFontFileFromResource(string name)
{
byte[] buffer = null;
Stream fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
if (fontStream != null)
{
buffer = new byte[fontStream.Length];
fontStream.Read(buffer, 0, (int)fontStream.Length);
fontStream.Close();
}
return buffer;
}
}
}

Binary file not shown.

View File

@ -41,6 +41,7 @@
<None Remove="Common\FastLZx64.dat" />
<None Remove="Common\FastLZx86.dat" />
<None Remove="Font\MaterialIcons-Regular.ttf" />
<None Remove="Font\sa-digital-number.ttf" />
</ItemGroup>
<ItemGroup>
@ -64,6 +65,7 @@
<EmbeddedResource Include="Font\fa-solid-900.ttf" />
<EmbeddedResource Include="Font\FontAwesome.ttf" />
<EmbeddedResource Include="Font\MaterialIcons-Regular.ttf" />
<EmbeddedResource Include="Font\sa-digital-number.ttf" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">