110,533
社区成员
发帖
与我相关
我的任务
分享
//自定义指示灯控件
public class BulbControl : Control
{
private BulbStatus status = BulbStatus.Off;
//自定义属性可在属性编辑窗口编辑
[Category("UserDefine Attributes")]
[Description("BulbControl used to indicate a state!")]
[Browsable(true)]
public BulbStatus Status
{
get { return status; }
set
{
if (value != status)
{
status = value;
Invalidate();
}
}
}
public BulbControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
Width = Height = 30;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawLight(g);
base.OnPaint(e);
}
//画灯,用图片绘制效果更佳
private void DrawLight(Graphics g)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(0, 0, Width, Height);
using (PathGradientBrush brush = new PathGradientBrush(path))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
brush.CenterColor = Color.White;
brush.CenterPoint = new PointF(Width >> 1, Height >> 1);
brush.SurroundColors = new Color[] { status == BulbStatus.Off ? Color.Red : Color.Green };
g.FillPath(brush, path);
}
Region = new Region(path);
}
}
}
//状态枚举
public enum BulbStatus
{
On,
Off
}
public bool Status
{
get{return _status;}
set{
_status = value;
this.BackgroundImage = _status ? _img1 : _img2;
}