// ***********************************************************************
// Assembly : HZH_Controls
// Created : 09-02-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace HZH_Controls.Controls
{
/* 显示位置序号
* ****1***
* * *
* 6 2
* * *
* ****7***
* * *
* 5 3
* * *
* ****4***
*/
///
/// Class UCLEDNum.
/// Implements the
///
///
public class UCLEDNum : UserControl
{
///
/// The m draw rect
///
Rectangle m_drawRect = Rectangle.Empty;
///
/// The m nums
///
private static Dictionary m_nums = new Dictionary();
///
/// Initializes static members of the class.
///
static UCLEDNum()
{
m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
m_nums['1'] = new int[] { 2, 3 };
m_nums['2'] = new int[] { 1, 2, 5, 4, 7 };
m_nums['3'] = new int[] { 1, 2, 7, 3, 4 };
m_nums['4'] = new int[] { 2, 3, 6, 7 };
m_nums['5'] = new int[] { 1, 6, 7, 3, 4 };
m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
m_nums['7'] = new int[] { 1, 2, 3 };
m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
m_nums['A'] = new int[] { 1, 2, 3, 5, 6, 7 };
m_nums['b'] = new int[] { 3, 4, 5, 6, 7 };
m_nums['C'] = new int[] { 1, 6, 5, 4 };
m_nums['c'] = new int[] { 7, 5, 4 };
m_nums['d'] = new int[] { 2, 3, 4, 5, 7 };
m_nums['E'] = new int[] { 1, 4, 5, 6, 7 };
m_nums['F'] = new int[] { 1, 5, 6, 7 };
m_nums['H'] = new int[] { 2, 3, 5, 6, 7 };
m_nums['h'] = new int[] { 3, 5, 6, 7 };
m_nums['J'] = new int[] { 2, 3, 4 };
m_nums['L'] = new int[] { 4, 5, 6 };
m_nums['o'] = new int[] { 3, 4, 5, 7 };
m_nums['P'] = new int[] { 1, 2, 5, 6, 7 };
m_nums['r'] = new int[] { 5, 7 };
m_nums['U'] = new int[] { 2, 3, 4, 5, 6 };
m_nums['-'] = new int[] { 7 };
m_nums[':'] = new int[0];
m_nums['.'] = new int[0];
}
///
/// Initializes a new instance of the class.
///
public UCLEDNum()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
SizeChanged += LEDNum_SizeChanged;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
Size = new System.Drawing.Size(40, 70);
if (m_drawRect == Rectangle.Empty)
m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
}
///
/// Handles the SizeChanged event of the LEDNum control.
///
/// The source of the event.
/// The instance containing the event data.
void LEDNum_SizeChanged(object sender, EventArgs e)
{
m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
}
///
/// The m value
///
private char m_value = '0';
///
/// Gets or sets the value.
///
/// The value.
[Description("值"), Category("自定义")]
public char Value
{
get { return m_value; }
set
{
if (!m_nums.ContainsKey(value))
{
return;
}
if (m_value != value)
{
m_value = value;
Refresh();
}
}
}
///
/// Sets the character value.
///
/// The character value.
public LEDNumChar ValueChar
{
get
{
return (LEDNumChar)((int)m_value);
}
set
{
Value = (char)value;
}
}
///
/// The m line width
///
private int m_lineWidth = 8;
///
/// Gets or sets the width of the line.
///
/// The width of the line.
[Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")]
public int LineWidth
{
get { return m_lineWidth; }
set
{
m_lineWidth = value;
Refresh();
}
}
///
/// 获取或设置控件的前景色。
///
/// The color of the fore.
///
///
///
[Description("颜色"), Category("自定义")]
public override System.Drawing.Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
///
/// 引发 事件。
///
/// 包含事件数据的 。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
if (m_value == '.')
{
Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth);
e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2);
}
else if (m_value == ':')
{
Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth);
e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1);
Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth);
e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2);
}
else
{
int[] vs = m_nums[m_value];
if (vs.Contains(1))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Left + 2, m_drawRect.Top),
new Point(m_drawRect.Right - 2, m_drawRect.Top),
new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth),
new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth),
new Point(m_drawRect.Left + 2, m_drawRect.Top)
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(2))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Right, m_drawRect.Top),
new Point(m_drawRect.Right, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+m_lineWidth),
new Point(m_drawRect.Right, m_drawRect.Top)
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(3))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Right, m_drawRect.Bottom),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-m_lineWidth),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2),
new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(4))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Left + 2, m_drawRect.Bottom),
new Point(m_drawRect.Right - 2, m_drawRect.Bottom),
new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Bottom-m_lineWidth),
new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Bottom-m_lineWidth),
new Point(m_drawRect.Left + 2, m_drawRect.Bottom)
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(5))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Left, m_drawRect.Bottom),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-m_lineWidth),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2),
new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(6))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Left, m_drawRect.Top),
new Point(m_drawRect.Left, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+m_lineWidth),
new Point(m_drawRect.Left, m_drawRect.Top)
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
if (vs.Contains(7))
{
GraphicsPath path = new GraphicsPath();
path.AddLines(new Point[]
{
new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1),
new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Height/2+1),
new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1),
new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1),
new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1)
});
path.CloseAllFigures();
e.Graphics.FillPath(new SolidBrush(ForeColor), path);
}
}
}
}
/// Enum LEDNumChar
public enum LEDNumChar : int
{
///
/// The character 0
///
CHAR_0 = (int)'0',
///
/// The character 1
///
CHAR_1 = (int)'1',
///
/// The character 2
///
CHAR_2 = (int)'2',
///
/// The character 3
///
CHAR_3 = (int)'3',
///
/// The character 4
///
CHAR_4 = (int)'4',
CHAR_5 = (int)'5',
///
/// The character 6
///
CHAR_6 = (int)'6',
///
/// The character 7
///
CHAR_7 = (int)'7',
///
/// The character 8
///
CHAR_8 = (int)'8',
///
/// The character 9
///
CHAR_9 = (int)'9',
///
/// The character a
///
CHAR_A = (int)'A',
///
/// The character b
///
CHAR_b = (int)'b',
///
/// The character c
///
CHAR_C = (int)'C',
///
/// The character c
///
CHAR_c = (int)'c',
///
/// The character d
///
CHAR_d = (int)'d',
///
/// The character e
///
CHAR_E = (int)'E',
///
/// The character f
///
CHAR_F = (int)'F',
///
/// The character h
///
CHAR_H = (int)'H',
///
/// The character h
///
CHAR_h = (int)'h',
///
/// The character j
///
CHAR_J = (int)'J',
///
/// The character l
///
CHAR_L = (int)'L',
///
/// The character o
///
CHAR_o = (int)'o',
///
/// The character p
///
CHAR_P = (int)'P',
///
/// The character r
///
CHAR_r = (int)'r',
///
/// The character u
///
CHAR_U = (int)'U',
///
/// The character horizontal line
///
CHAR_HorizontalLine = (int)'-',
///
/// The character colon
///
CHAR_Colon = (int)':',
///
/// The character dot
///
CHAR_Dot = (int)'.',
}
}