c#做的指示灯控件用法_C#--自定义控件-开发LED指示灯控件(带闪烁效果)

以下是学习笔记:参考:https://www.bilibili.com/video/BV1eQ4y1M7ZY?p=5效果如下:思考:实现以上效果要用到哪些属性颜色,是否有边框,外环宽度,是否高亮,中心颜色,是否闪烁,颜色列表,闪烁的频率如果是开关,就需要bool类型 True,False颜色如果是多种状态,当前值数值类型常规操作:一,自定义LED显示的控件1,添加“用户控件”,命名MyLED2,编...
摘要由CSDN通过智能技术生成

以下是学习笔记:

参考:https://www.bilibili.com/video/BV1eQ4y1M7ZY?p=5

效果如下:

d36c7c4ccbf31019c878e853468f1e46.png

思考:实现以上效果要用到哪些属性

颜色,是否有边框,外环宽度,是否高亮,中心颜色,是否闪烁,颜色列表,闪烁的频率

如果是开关,就需要bool类型 True,False颜色

如果是多种状态,当前值数值类型

常规操作:

一,自定义LED显示的控件

1,添加“用户控件”,命名MyLED

0ace1c9234527d4bcb71faf80e7e88ff.png

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的控件了,拖入后可以直接使用

5f86cd84550741a70910a09f4e73b4d3.gif

dc12f08812000943d3548ae4e6b5ebe0.png

注意:如果只有这个代码,那只有宽度大于高度的时候才会画圆。

根据指示灯的功能需求,最终代码如下

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 gapWidt

  • 4
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值