工控自定义winform控件:3色灯,支持DataBinding

 

 产线那个工位没有装3色报警灯,于是手撸了一个。

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace UNI.WinControls
{
    public enum LED_Status
    {
        OK = 1,
        Error = 2,
        Warning = 3,
        Unknowing = 4
    }

    public class ThreeColorLED : Control
    {
        [DllImport("Kernel32.dll")]
        public static extern bool Beep(int frequency, int duration);

        public ThreeColorLED()
        {
            Width = 64;
            Height = 64;
            Status = LED_Status.Unknowing;
        }

        public LED_Status Status
        {
            get; set;
        }

        private string statusCode="0";

        public string StatusCode
        {
            get { return statusCode; }
            set 
            {
                statusCode = value;
                switch (value) 
                {
                    case "1":
                        Status = LED_Status.OK;
                        break;
                    case "2":
                        Status = LED_Status.Error;
                        break;
                    case "3":
                        Status = LED_Status.Warning;
                        break;
                    default:
                        Status = LED_Status.Unknowing;
                        break;
                }
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var color = Color.Transparent;
            switch (Status)
            {
                case LED_Status.OK:
                    try
                    {
                        Beep(1888,1000);
                    }
                    catch  
                    {  
                    }
                    color = Color.Green;
                    break;
                case LED_Status.Error:
                    color = Color.Red;
                    break;
                case LED_Status.Warning:
                    color = Color.Yellow;
                    break;
                default:
                    color = Color.Gray;
                    break;
            }
            e.Graphics.Clear(color);
            var b = new SolidBrush(color);
            Pen p = new Pen(new SolidBrush(color)); 
            Rectangle r = new Rectangle(0, 0, this.Width, this.Height);//标识圆的大小
            e.Graphics.DrawEllipse(p, r);
            e.Graphics.FillEllipse(b, r);

            GraphicsPath g = new GraphicsPath();
            g.AddEllipse(0, 0, Width, Height);
            this.Region = new Region(g);//这句就是设置圆形的规格区域的 
        }
    }
}

使用:

 

posted @ 2020-03-09 15:01  数据酷软件  阅读(403)  评论(0编辑  收藏