Winform GDI+自定义LED指示灯控件
1.新建一个类库
2.添加用户控件
3.在用户窗体写代码
点击查看代码
namespace MyControls
{
public partial class LED : UserControl
{
public LED()
{
InitializeComponent();
//添加事件
this.Paint += LED_Paint;
}
//圆颜色
private Color _roundColor = Color.Gray;
[Category("自定义属性")]
[Description("圆颜色")]
public Color RoundColor
{
get { return _roundColor; }
set
{
_roundColor = value;
this.Invalidate();
}
}
//圆环颜色
private Color _ringColor = Color.White;
[Category("自定义属性")]
[Description("圆环颜色")]
public Color RingColor
{
get { return _ringColor; }
set
{
_ringColor = value;
this.Invalidate();
}
}
//圆环距离
private int _distance = 2;
[Category("自定义属性")]
[Description("圆环距离")]
public int Distance
{
get { return _distance; }
set
{
_distance = value;
this.Invalidate();
}
}
//圆环宽度
private float _ringWidth = 1.0f;
[Category("自定义属性")]
[Description("圆环宽度")]
public float RingWidth
{
get { return _ringWidth; }
set
{ _ringWidth = value;
this.Invalidate();
}
}
private void LED_Paint(object sender, PaintEventArgs e)
{
//获取画布
Graphics g = e.Graphics;
// 启用抗锯齿以获得更平滑的边缘
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//获取圆画刷
SolidBrush solidBrush = new SolidBrush(_roundColor);
//获取圆矩形
Rectangle rectangle = new Rectangle(1,1,this.Width - 2,this.Height - 2);
//绘制椭圆
g.FillEllipse(solidBrush, rectangle);
//获取圆环画笔
Pen pen = new Pen(_ringColor, _ringWidth);
//获取圆环矩形
RectangleF rectangleF = new RectangleF(_distance + 1 + _ringWidth / 2, _distance + 1 + _ringWidth / 2, this.Width - (_distance * 2) - 2 - _ringWidth, this.Height - (_distance * 2) - 2 - _ringWidth);
//绘制圆环
g.DrawEllipse(pen, rectangleF);
}
}
}


浙公网安备 33010602011771号