C#自定義控件開發(2)—LED指示燈


下面來開發一個LED指示燈控件,如下:

 

設計屬性包括:

外環寬度,外環間隙,內環間隙,顏色【五種】,當前值。

 由於該LED指示燈基本是完全獨立設計的,並不是在某個控件的基礎上進行的開發,因此,這里使

用用戶控件的方式進行開發。通過GDI+方式對控件進行繪制。GDI的坐標系如下:

首先繪制外環,然后繪制內圓。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JSControl
{
    public partial class LedControl : UserControl
    {
        public LedControl()
        {
            InitializeComponent();
            this.Size = new Size(40,40);//初始化控件大小
        }

        #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.LimeGreen;
        [Browsable(true)]
        [Category("自定義屬性")]
        [Description("LimeGreen")]
        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;//控件高度

        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;
            }
            Color 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
    }
}

 this.Invalidate()表示進行重繪,調用它后將會執行

 protected override void OnPaint(PaintEventArgs e){}中的代碼進行重繪。

 生成后,將和其他控件一樣可以看到其屬性。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM