C#自定义控件五报警按钮
C#自定义控件五报警按钮
效果图:
这个是本人最得意的控件之一,在商业项目中使用,史无前例,个人原创,应用于项目EcanAlarmSystem报警监控系统中,作为报警组件中的核心组件,在速度和展现形式上完美符合现实需求。
分析:一个防区有六种状态,分别是布防、撤防、旁路、报警、故障、断开,需要在不同状态显示不同的颜色,一个防区对应的属性有防区号、防区名、防区图片、防区类型、主机类型、子系统、地图X坐标、地图Y坐标。
第一步:添加用户控件,命名为myAlarmButton
第二步:定义枚举
public enum myButtonState
{
bufang = 1,//布防状态
chefang = 2,//撤防状态
panglu = 3,//旁路状态
baojing = 4,//报警状态
duankai = 5,//防区断开
guzhang = 6//防区故障
}
第三步:定义属性
第四步:重写OnPaint方法
protected override void OnPaint(PaintEventArgs e)
{
this.Size = new Size(30, 30);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(this.nowColor), 0.0f, 0.0f, 30, 30);
g.DrawEllipse(new Pen(Color.Black, 1), 0.0f, 0.0f, 29, 29);
g.DrawString(defenceID, new Font("宋体", 9, FontStyle.Bold), Brushes.White, 3, 9);
}
第五步:通过状态属性改变来改变颜色
/// <summary>
/// 防区当前状态
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区当前状态")]
public myButtonState DefenceState
{
set
{
defenceState = value;
if (value == myButtonState.bufang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.bufangColor;
}
else if (value == myButtonState.chefang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.chefangColor;
}
else if (value == myButtonState.panglu)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.pangluColor;
}
else if (value == myButtonState.duankai)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.duankaiColor;
}
else if (value == myButtonState.guzhang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.guzhangColor;
}
else if (value == myButtonState.baojing)
{
myTimer.Start();
}
this.Invalidate();
}
get { return defenceState; }
}
贴出全部代码:
//控件名:myAlarmButton
//作者:刘典武
//时间:2011-06-09
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace myControl
{
public enum myButtonState
{
bufang = 1,//布防状态
chefang = 2,//撤防状态
panglu = 3,//旁路状态
baojing = 4,//报警状态
duankai = 5,//防区断开
guzhang = 6//防区故障
}
public partial class myAlarmButton : UserControl
{
public myAlarmButton()
{
InitializeComponent();
myTimer = new Timer();
myTimer.Interval = myInterval;
myTimer.Tick += new EventHandler(myTimer_Tick);
//设置Style支持透明背景色
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
private string defenceID = "010";//防区号
private string defenceName = "010";//防区名称
private string defenceType = "03";//防区类型
private string imageName;//防区对应图片名称
private string subSystem = "01";//防区所属子系统
private string hostType = "VISTA120";//防区对应主机类型
private int x;//防区在地图上的X坐标
private int y;//防区在地图上的Y坐标
private int myInterval = 300;//报警颜色更改时间隔
private bool isRedColor = false;//当前是否红色显示
private Timer myTimer;
private myButtonState defenceState = myButtonState.chefang;//防区当前状态
private Color bufangColor = Color.YellowGreen;//布防颜色
private Color chefangColor = Color.Blue;//撤防颜色
private Color pangluColor = Color.Silver;//旁路颜色
private Color baojingColor = Color.Red;//报警颜色
private Color nowColor = Color.Blue;//当前颜色
//DS7400报警主机增加两种情况
private Color duankaiColor = Color.DarkGoldenrod;//断开颜色
private Color guzhangColor = Color.Black;//故障颜色
/// <summary>
/// 防区号
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区号")]
public string DefenceID
{
set { defenceID = value; }
get { return defenceID; }
}
/// <summary>
/// 防区名称
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区名称")]
public string DefenceName
{
set { defenceName = value; }
get { return defenceName; }
}
/// <summary>
/// 防区类型
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区类型")]
public string DefenceType
{
set { defenceType = value; }
get { return defenceType; }
}
/// <summary>
/// 防区对应地图名称
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区对应地图名称")]
public string DefenceImageName
{
set { imageName = value; }
get { return imageName; }
}
/// <summary>
/// 防区所属子系统
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区所属子系统")]
public string SubSystem
{
set { subSystem = value; }
get { return subSystem; }
}
/// <summary>
/// 防区对应主机类型
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区对应主机类型")]
public string HostType
{
set { hostType = value; }
get { return hostType; }
}
/// <summary>
/// 防区X坐标
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区X坐标")]
public int X
{
set { x = value; }
get { return x; }
}
/// <summary>
/// 防区Y坐标
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区Y坐标")]
public int Y
{
set { y = value; }
get { return y; }
}
/// <summary>
/// 报警颜色切换间隔
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("报警颜色切换间隔")]
public int MyInterval
{
set { if (value < 100) { myInterval = 100; } else { myInterval = value; } }
get { return myInterval; }
}
/// <summary>
/// 防区当前状态
/// </summary>
[CategoryAttribute("报警按钮信息"), Description("防区当前状态")]
public myButtonState DefenceState
{
set
{
defenceState = value;
if (value == myButtonState.bufang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.bufangColor;
}
else if (value == myButtonState.chefang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.chefangColor;
}
else if (value == myButtonState.panglu)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.pangluColor;
}
else if (value == myButtonState.duankai)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.duankaiColor;
}
else if (value == myButtonState.guzhang)
{
myTimer.Stop();
isRedColor = false;
this.nowColor = this.guzhangColor;
}
else if (value == myButtonState.baojing)
{
myTimer.Start();
}
this.Invalidate();
}
get { return defenceState; }
}
/// <summary>
/// 布防状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("布防状态颜色")]
public Color BuFangColor
{
set { bufangColor = value; }
get { return bufangColor; }
}
/// <summary>
/// 撤防状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("撤防状态颜色")]
public Color CheFangColor
{
set { chefangColor = value; }
get { return chefangColor; }
}
/// <summary>
/// 旁路状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("旁路状态颜色")]
public Color PangLuColor
{
set { pangluColor = value; }
get { return pangluColor; }
}
/// <summary>
/// 报警状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("报警状态颜色")]
public Color BaoJingColor
{
set { baojingColor = value; }
get { return baojingColor; }
}
/// <summary>
/// 断开状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("断开状态颜色")]
public Color DuanKaiColor
{
set { duankaiColor = value; }
get { return duankaiColor; }
}
/// <summary>
/// 故障状态颜色
/// </summary>
[CategoryAttribute("报警按钮颜色"), Description("故障状态颜色")]
public Color GuZhangColor
{
set { guzhangColor = value; }
get { return guzhangColor; }
}
private void myTimer_Tick(object sender, EventArgs e)
{
if (!isRedColor)
{
this.nowColor = this.baojingColor;
isRedColor = true;
}
else
{
this.nowColor = Color.Black;
isRedColor = false;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
this.Size = new Size(30, 30);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(this.nowColor), 0.0f, 0.0f, 30, 30);
g.DrawEllipse(new Pen(Color.Black, 1), 0.0f, 0.0f, 29, 29);
g.DrawString(defenceID, new Font("宋体", 9, FontStyle.Bold), Brushes.White, 3, 9);
}
}
}