diff --git a/SunnyUI/Controls/UIDigitalLabel.cs b/SunnyUI/Controls/UIDigitalLabel.cs new file mode 100644 index 00000000..fa0916f2 --- /dev/null +++ b/SunnyUI/Controls/UIDigitalLabel.cs @@ -0,0 +1,136 @@ +/****************************************************************************** + * SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。 + * CopyRight (C) 2012-2023 ShenYongHua(沈永华). + * QQ群:56829229 QQ:17612584 EMail:SunnyUI@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是一种固定宽度(web)字体,采用冷液晶显示(LCD)样式。 + * 依据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(); + } + } + + /// + /// 重载绘图 + /// + /// 绘图参数 + 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(); + } + } + + /// + /// 设置主题样式 + /// + /// 主题样式 + public override void SetStyleColor(UIBaseStyle uiColor) + { + + } + } +} diff --git a/SunnyUI/Font/UDigitalFont.cs b/SunnyUI/Font/UDigitalFont.cs new file mode 100644 index 00000000..904e6a54 --- /dev/null +++ b/SunnyUI/Font/UDigitalFont.cs @@ -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; + } + } +} diff --git a/SunnyUI/Font/sa-digital-number.ttf b/SunnyUI/Font/sa-digital-number.ttf new file mode 100644 index 00000000..25340c9b Binary files /dev/null and b/SunnyUI/Font/sa-digital-number.ttf differ diff --git a/SunnyUI/SunnyUI.csproj b/SunnyUI/SunnyUI.csproj index 94570d35..25a27ea5 100644 --- a/SunnyUI/SunnyUI.csproj +++ b/SunnyUI/SunnyUI.csproj @@ -41,6 +41,7 @@ + @@ -64,6 +65,7 @@ +