using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rs.Controls { public class IOStatus : Label { private bool _ShowNo = true; public bool ShowNo { get { return _ShowNo; } set { _ShowNo = value; } } private ushort _IoNO; public ushort IoNO { get { return _IoNO; } set { _IoNO = value; } } private int _Radius = 5; public int Radius { get { return _Radius; } set { _Radius = value; } } private ushort _IoID; public ushort IoID { get { return _IoID; } set { _IoID = value; } } private bool _Passed; public bool Passed { get { return _Passed; } set { _Passed = value; Invalidate(); } } private bool _Disabled; public bool Disabled { get { return _Disabled; } set { _Disabled = value;Invalidate(); } } private bool _Reverse; public bool Reverse { get { return _Reverse; } set { _Reverse = value; Invalidate(); } } private Color fillColor = Color.Gray; private int _Status = 1; public int Status { get { return _Status; } set { _Status = value; if(!Disabled) { if (_Status == 0) { this.fillColor = Color.Gray; } else { this.fillColor = Color.FromArgb(74, 164, 15); } } else { this.fillColor = Color.FromArgb(70,56, 56, 56); } Invalidate(); } } public IOStatus() { this.AutoSize = false; this.FlatStyle = FlatStyle.Flat; } protected override void OnPaint(PaintEventArgs e) { this.TextAlign = ContentAlignment.MiddleCenter; Brush bs = new SolidBrush(fillColor); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; Rectangle rect = this.ClientRectangle; GraphicsPath path = new GraphicsPath(); path.AddArc(new Rectangle(rect.Left, rect.Top, _Radius * 2, _Radius * 2), 180, 90); path.AddLine(new Point(rect.Left + _Radius, rect.Top), new Point(rect.Left + rect.Width - _Radius, rect.Top)); path.AddArc(new Rectangle(rect.Left + rect.Width - 1 - (_Radius * 2), rect.Top, _Radius * 2, _Radius * 2), 270, 90); path.AddLine(new Point(rect.Left + rect.Width - 1, rect.Top + _Radius), new Point(rect.Left + rect.Width - 1, rect.Top + rect.Height - _Radius));//右边竖线 path.AddArc(new Rectangle(rect.Left + rect.Width - 1 - (_Radius * 2), rect.Top + rect.Height - 1 - _Radius * 2, _Radius * 2, _Radius * 2), 0, 90); path.AddLine(new Point(rect.Left + _Radius, rect.Top + rect.Height - 1), new Point(rect.Left + rect.Width - _Radius, rect.Top + rect.Height - 1));//右边竖线 path.AddArc(new Rectangle(rect.Left, rect.Top + rect.Height - 1 - _Radius * 2, _Radius * 2, _Radius * 2), 90, 90); path.AddLine(new Point(rect.Left, rect.Top + _Radius), new Point(rect.Left, rect.Top + rect.Height - 1 - _Radius));//右边竖线 //e.Graphics.DrawPath(Pens.White, path); e.Graphics.FillPath(bs, path); //SizeF size = e.Graphics.MeasureString(this.Text, this.Font); //e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Width - size.Width) / 2, (this.Height - size.Height) / 2)); if (ShowNo) { RectangleF yuanRect = new RectangleF(rect.Left, rect.Top, 20, 20); e.Graphics.FillEllipse(new SolidBrush(Color.DarkRed),yuanRect); e.Graphics.DrawEllipse(new Pen(Color.White), yuanRect); Font indexFont = new Font("宋体", 11); SizeF nosize = e.Graphics.MeasureString(IoNO.ToString(), indexFont); e.Graphics.DrawString(IoNO.ToString(), indexFont, new SolidBrush(ForeColor), new PointF(yuanRect.Left + (yuanRect.Width - nosize.Width) / 2, yuanRect.Top + (yuanRect.Height - nosize.Height) / 2)); if(Passed || Disabled) { RectangleF passRect = new RectangleF(rect.Right-20, rect.Top, 20, 20); if(Disabled) { this.ForeColor = Color.Gray; e.Graphics.FillEllipse(new SolidBrush(Color.Yellow), passRect); e.Graphics.DrawEllipse(new Pen(Color.White), passRect); } else { e.Graphics.FillEllipse(new SolidBrush(Color.Green), passRect); e.Graphics.DrawEllipse(new Pen(Color.White), passRect); } Font passFont = new Font("宋体", 11); string msg = "P"; if(Disabled) { msg = "X"; } SizeF passsize = e.Graphics.MeasureString(msg, passFont); if (Disabled) { e.Graphics.DrawString(msg, passFont, new SolidBrush(Color.Black), new PointF(passRect.Left + (passRect.Width - passsize.Width) / 2, passRect.Top + (passRect.Height - passsize.Height) / 2)); } else { e.Graphics.DrawString(msg, passFont, new SolidBrush(ForeColor), new PointF(passRect.Left + (passRect.Width - passsize.Width) / 2, passRect.Top + (passRect.Height - passsize.Height) / 2)); } } if (Reverse) { RectangleF RRect = new RectangleF(rect.Left , rect.Bottom - 12, 12, 12); e.Graphics.FillEllipse(new SolidBrush(Color.Yellow), RRect); e.Graphics.DrawEllipse(new Pen(Color.White), RRect); Font passFont = new Font("宋体", 8); string msg = "R"; SizeF passsize = e.Graphics.MeasureString(msg, passFont); e.Graphics.DrawString(msg, passFont, new SolidBrush(Color.Black), new PointF(RRect.Left + (RRect.Width - passsize.Width) / 2, RRect.Top + (RRect.Height - passsize.Height) / 2)); } } base.OnPaint(e); } } }