灯控制

1 效果

 

 

2 代码展示

  public partial class ULightControl : Control
    {
        public ULightControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.Selectable,true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            lightText = stateText;
        }
        public event EventHandler ClickEvent;//单击事件
        private Color m_centerColor = Color.White;
        private Color m_surroundColor = Color.Green;

        public Color CenterColor
        {
            get => m_centerColor;
            set
            {
                m_centerColor = value;
                Refresh();
            }
        }

        private Color m_borderColor = Color.Gray;
        public Color BorderColor
        {
            get => m_borderColor;
            set
            {
                m_borderColor = value;
                Refresh();
            }
        }

      

        private Color m_focusColor = Color.Green;
        /// <summary>
        /// 按下时灯的颜色
        /// </summary>
        public Color LightFocusColor
        {
            get => m_focusColor;
            set
            {
                m_focusColor = value;
                Refresh();
            }
        }

        private Color m_normalColor = Color.Red;
        /// <summary>
        /// 未启动时灯的颜色
        /// </summary>
        public Color LightNormalColor
        {
            get => m_normalColor;
            set
            {
                m_normalColor = value;
                m_surroundColor = m_normalColor;
                Refresh();
            }
        }

        private string stateText = "运行";
        /// <summary>
        /// 指示灯状态
        /// </summary>
        public string StateText
        {
            get { return stateText; }
            set
            {
                stateText = value;
                lightText = stateText;
                Refresh();
            }
        }

        private string focusstateText = "已运行";
        /// <summary>
        /// 指示灯状态
        /// </summary>
        public string FocusStateText
        {
            get { return focusstateText; }
            set
            {
                focusstateText = value;
                Refresh();
            }
        }

        private string varName;
        /// <summary>
        /// 状态参数名称
        /// </summary>
        public string VarName
        {
            get { return varName; }
            set { varName = value; }
        }

        private bool  isOn=false;

        public bool  IsOn
        {
            get { return isOn; }
            set { isOn = value;
                if(isOn)
                {
                    m_surroundColor = LightFocusColor;
                    lightText = FocusStateText;
                }
                else
                {
                    m_surroundColor = LightNormalColor;
                    lightText = StateText;
                }
                Refresh();
            }
        }


        //protected override void OnPaint(PaintEventArgs e)
        //{

        //    //Graphics g = e.Graphics;
        //    //g.SmoothingMode = SmoothingMode.AntiAlias;
        //    //Rectangle rect = e.ClipRectangle;

        //    //Rectangle rect1 = new Rectangle(rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
        //    //g.DrawEllipse(new Pen(this.ForeColor), rect1);
        //    //g.FillEllipse(new SolidBrush(this.ForeColor), rect1);
        //    //使控件边界也为圆形
        //    var graphics = new GraphicsPath();
        //    graphics.AddEllipse(this.Width/4,this.Height/8, this.Width/2,this.Height/2);
        //    // this.Region = new System.Drawing.Region(graphics);
        //    base.OnPaint(e);
        //}

        private void ULightControl_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //重绘时 画出中心放射颜色的圆形
            var path = new GraphicsPath();
            Rectangle rectEllipse;
            Rectangle borderEllipse;
            if(!string.IsNullOrEmpty(lightText))
            {
                borderEllipse = new Rectangle(this.Width / 4, this.Height / 8, this.Width / 2, this.Height / 2);
                rectEllipse = new Rectangle(this.Width / 4+ this.Width / 12, this.Height / 8+ this.Height / 12, this.Width / 2- this.Width / 6, this.Height / 2 - this.Height / 6);
            }
            else
            {
                borderEllipse = new Rectangle(5, 5, this.Width -10, this.Height -10);
                rectEllipse= new Rectangle(5+this.Width/10, 5+this.Height/10, this.Width - 10- this.Width / 5, this.Height - 10- this.Height / 5);
            }
            path.AddEllipse(rectEllipse);
            var pthGrBrush = new PathGradientBrush(path)
            {
                CenterColor = CenterColor
            };
            Color[] colors = { this.m_surroundColor };
            pthGrBrush.SurroundColors = colors;
            g.FillEllipse(new SolidBrush(BorderColor), borderEllipse);
            g.FillEllipse(pthGrBrush, rectEllipse);
          
            if (!string.IsNullOrEmpty(lightText))
            {
                Rectangle rect = new Rectangle(this.Width / 2 - this.Width / 3, (this.Height - this.Height / 3), this.Width - this.Width / 3, this.Height / 3);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                g.DrawString(lightText, this.Font, new SolidBrush(this.ForeColor), rect, format);
            }
           
        }

        private string lightText;

        protected override void OnClick(EventArgs e)
        {
            if(ClickEvent!=null)
            {
                ClickEvent.Invoke(this, new EventArgs());
            }
           
           
          
        }

    }

 

posted @ 2024-05-20 11:44  陌念  阅读(8)  评论(0)    收藏  举报