C#可同时设置为亮灭或者是闪烁两用的LED控件

//可同时设置为亮灭或者是闪烁两用的LED控件:
//控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace JSControl
{
    public partial class MyLEDControl : UserControl
    {
        private bool isBlinking = false;
        private bool isOn = false;
        private Thread blinkThread;
        public MyLEDControl()
        {
            this.Size = new Size(30, 30);//初始化控件大小
        }      
        public bool IsBlinking
        {
            get { return isBlinking; }
            set
            {
                isBlinking = value;

                if (isBlinking)
                {
                    if (blinkThread == null || !blinkThread.IsAlive)
                    {
                        blinkThread = new Thread(Blink);
                        blinkThread.Start();
                    }
                }
                else
                {
                    if (blinkThread != null && blinkThread.IsAlive)
                    {
                        blinkThread.Abort();
                    }

                    isOn = true;
                    Invalidate();
                }
            }
        }
        private void Blink()
        {
            while (true)
            {
                isOn = !isOn;
                Invalidate();
                Thread.Sleep(500);
            }
        }
        #region Filed
        private float outWidth = 4.0f;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("外环宽度")]
        public float OutWidth
        {
            get { return outWidth; }
            set
            {
                if (value <= 0 || value > 0.1f * this.Width)
                {
                    return;
                }
                outWidth = value;
                this.Invalidate();
            }
        }

        private float outGap = 3.0f;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("外环间隙")]
        public float OutGap
        {
            get { return outGap; }
            set
            {
                if (value <= 0 || value > 0.1f * this.Width || inGap <= value)
                {
                    return;
                }
                outGap = value;



                this.Invalidate();
            }
        }

        private float inGap = 8.0f;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("内环间隙")]
        public float InGap
        {
            get { return inGap; }
            set
            {
                if (value <= 0 || value <= outGap)
                {
                    return;
                }
                inGap = value;
                this.Invalidate();
            }
        }

        private Color color1 = Color.Gray;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("指示灯颜色")]
        public Color Color1
        {
            get { return color1; }
            set
            {
                color1 = value;
                this.Invalidate();
            }
        }

        private Color color2 = Color.Lime;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("Lime")]
        public Color Color2
        {
            get { return color2; }
            set
            {
                color2 = value;
                this.Invalidate();
            }
        }

        private Color color3 = Color.Red;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("Red")]
        public Color Color3
        {
            get { return color3; }
            set
            {
                color3 = value;
                this.Invalidate();
            }
        }

        private Color color4 = Color.DarkGoldenrod;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("DarkGoldenrod")]
        public Color Color4
        {
            get { return color4; }
            set
            {
                color4 = value;
                this.Invalidate();
            }
        }

        private Color color5 = Color.Blue;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("Blue")]
        public Color Color5
        {
            get { return color5; }
            set
            {
                color5 = value;
                this.Invalidate();
            }
        }

        //指示灯颜色索引
        private int currentValue = 0;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("当前值")]
        public int CurrentValue
        {
            get { return currentValue; }
            set
            {
                if (value > 4 || value < 0)
                {
                    return;
                }
                currentValue = value;
                this.Invalidate();
            }
        }
        #endregion

        #region verride
        private Graphics g;

        private Pen p;//画笔

        private SolidBrush sb;//画刷

        private int width;//控件宽度

        private int height;//控件高度
        private Color CurrentColor;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            g = e.Graphics;
            width = this.Width;
            height = this.Height;
            //这里是为了设置渲染效果,消除锯齿,高效果显示
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            if (inGap > 0.5f * this.Height || inGap > 0.5f * this.Width)
            {
                return;
            }

            if(isBlinking)
                CurrentColor = isOn ? Color.Lime : Color.Gray;
            else
                CurrentColor = GetCurrentColor(this.currentValue);
            //设置画笔的宽度
            //p = new Pen(CurrentColor, outWidth);
            //先绘制外环
            RectangleF rec = new RectangleF(outGap, outGap, this.width - 2 * outGap, this.height - 2 * outGap);
            //g.DrawEllipse(p, rec);
            sb = new SolidBrush(CurrentColor);
            //再绘制内圆
            //rec = new RectangleF(inGap, inGap, this.width - 2 * inGap, this.height - 2 * inGap);
            g.FillEllipse(sb, rec);

        }
        #endregion


        #region Private Methods
        /// <summary>
        /// 设置控件颜色
        /// </summary>
        /// <returns></returns>
        private Color GetCurrentColor(int currentColor)
        {
            List<Color> ColorList = new List<Color>();
            ColorList.Add(color1);
            ColorList.Add(color2);
            ColorList.Add(color3);
            ColorList.Add(color4);
            ColorList.Add(color5);
            return ColorList[currentValue];
        }

        #endregion
    }
}


在这里插入图片描述
在这里插入图片描述

设置isBlinking=True,LED绿色灰色交替闪烁。
设置isBlinking=False,currentValue=0;(0-Gray;1-Lime;2-Red;3-DarkGoldenrod;4-Blue,LED控制颜色。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值