以下是学习笔记:
参考:https://www.bilibili.com/video/BV1eQ4y1M7ZY?p=5
效果如下:
思考:实现以上效果要用到哪些属性
颜色,是否有边框,外环宽度,是否高亮,中心颜色,是否闪烁,颜色列表,闪烁的频率
如果是开关,就需要bool类型 True,False颜色
如果是多种状态,当前值数值类型
常规操作:
一,自定义LED显示的控件
1,添加“用户控件”,命名MyLED
2,编写用户控件的MyLED的代码
public partial class MyLED : UserControl
{
public MyLED()
{
InitializeComponent();
#region 【1】设置双缓冲等属性
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);
#endregion
}
#region 【2】定义三个字段
private Graphics g;
private Pen p;
private SolidBrush sb;
#endregion
#region 【3】添加一个设置Graphics的方法
private void SetGraphics(Graphics g)
{
//设置画布的属性
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
}
#endregion
#region 【4】根据实际控件分析的结果,创建属性
private Color ledColor = Color.Green;
[Category("jason控件属性")]
[Description("LED指示灯演示")]
public Color LedColor
{
get { return ledColor; }
set
{
ledColor = value;
this.Invalidate();
}
}
#endregion
#region 【5】创建重绘的事件
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
g = e.Graphics;//获取画布
SetGraphics(g);//设置画布
//情况1:如果宽度大于高度(以高度为准)
if (this.Width > this.Height)
{
sb = new SolidBrush(ledColor);
Rectangle rec=new Rectangle(1,1,this.Height-2,this.Height-2);//创建矩形
g.FillEllipse(sb,rec);//画圆
}
}
#endregion
}
3,重新生成后在工具箱里面就可以看到MyLED的控件了,拖入后可以直接使用
注意:如果只有这个代码,那只有宽度大于高度的时候才会画圆。
根据指示灯的功能需求,最终代码如下
public partial class MyLED : UserControl
{
public MyLED()
{
InitializeComponent();
#region 【1】设置双缓冲等属性
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);
#endregion
}
#region 【2】定义三个字段
private Graphics g;
private Pen p;
private SolidBrush sb;
#endregion
#region 【3】添加一个设置Graphics的方法
private void SetGraphics(Graphics g)
{
//设置画布的属性
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
}
#endregion
#region 【4】根据实际控件分析的结果,创建属性
private Color ledColor = Color.Green;
[Category("jason控件属性")]
[Description("LED指示灯演示")]
public Color LedColor
{
get { return ledColor; }
set
{
ledColor = value;
this.Invalidate();
}
}
private bool isBorder = true;
[Category("jason控件属性")]
[Description("是否有边框")]
public bool IsBorder
{
get { return isBorder; }
set
{
isBorder = value;
this.Invalidate();
}
}
private int borderWidth = 5;
[Category("jason控件属性")]
[Description("圆环的宽度")]
public int BorderWidth
{
get { return borderWidth; }
set
{
borderWidth = value;
this.Invalidate();
}
}
private int gapWidth