前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://blog.csdn.net/kwwwvagaa/article/details/100586547

用处及效果

准备工作

依然使用GDI+绘画,这个比较简单,就是画圆

开始

新增一个类UCSignalLamp,继承UserControl

添加属性

复制代码

  1 /// <summary>
  2         /// The is show border
  3         /// </summary>
  4         private bool isShowBorder = false;
  5 
  6         /// <summary>
  7         /// Gets or sets a value indicating whether this instance is show border.
  8         /// </summary>
  9         /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
 10         [Description("是否显示边框"), Category("自定义")]
 11         public bool IsShowBorder
 12         {
 13             get { return isShowBorder; }
 14             set
 15             {
 16                 isShowBorder = value;
 17                 Refresh();
 18             }
 19         }
 20 
 21         /// <summary>
 22         /// The lamp color
 23         /// </summary>
 24         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 25 
 26         /// <summary>
 27         /// Gets or sets the color of the lamp.
 28         /// </summary>
 29         /// <value>The color of the lamp.</value>
 30         [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
 31         public Color[] LampColor
 32         {
 33             get { return lampColor; }
 34             set
 35             {
 36                 if (value == null || value.Length <= 0)
 37                     return;
 38                 lampColor = value;
 39                 Refresh();
 40             }
 41         }
 42 
 43         /// <summary>
 44         /// The is highlight
 45         /// </summary>
 46         private bool isHighlight = true;
 47 
 48         /// <summary>
 49         /// Gets or sets a value indicating whether this instance is highlight.
 50         /// </summary>
 51         /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
 52         [Description("是否高亮显示"), Category("自定义")]
 53         public bool IsHighlight
 54         {
 55             get { return isHighlight; }
 56             set
 57             {
 58                 isHighlight = value;
 59                 Refresh();
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// The twinkle speed
 65         /// </summary>
 66         private int twinkleSpeed = 0;
 67 
 68         /// <summary>
 69         /// Gets or sets the twinkle speed.
 70         /// </summary>
 71         /// <value>The twinkle speed.</value>
 72         [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
 73         public int TwinkleSpeed
 74         {
 75             get { return twinkleSpeed; }
 76             set
 77             {
 78                 if (value < 0)
 79                     return;
 80                 twinkleSpeed = value;
 81                 if (value == 0 || lampColor.Length <= 1)
 82                 {
 83                     timer.Enabled = false;
 84                 }
 85                 else
 86                 {
 87                     intColorIndex = 0;
 88                     timer.Interval = value;
 89                     timer.Enabled = true;
 90                 }
 91                 Refresh();
 92             }
 93         }
 94         /// <summary>
 95         /// The timer
 96         /// </summary>
 97         Timer timer;
 98         /// <summary>
 99         /// The int color index
100         /// </summary>
101         int intColorIndex = 0;

复制代码

重绘

复制代码

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6             Color c1 = lampColor[intColorIndex];
 7             g.FillEllipse(new SolidBrush(c1), this.ClientRectangle);
 8 
 9             if (isHighlight)
10             {
11                 GraphicsPath gp = new GraphicsPath();
12 
13                 Rectangle rec = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
14                 gp.AddEllipse(rec);
15 
16                 Color[] surroundColor = new Color[] { c1 };
17                 PathGradientBrush pb = new PathGradientBrush(gp);
18                 pb.CenterColor = Color.White;
19                 pb.SurroundColors = surroundColor;
20                 g.FillPath(pb, gp);
21             }
22 
23             if (isShowBorder)
24             {
25                 g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
26             }
27         }

复制代码

全部代码

// ***********************************************************************
// Assembly         : HZH_Controls
// Created          : 2019-09-09
//
// ***********************************************************************
// <copyright file="UCSignalLamp.cs">
//     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace HZH_Controls.Controls.FactoryControls.Lamp
{
    /// <summary>
    /// Class UCSignalLamp.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    public class UCSignalLamp : UserControl
    {
        /// <summary>
        /// The is show border
        /// </summary>
        private bool isShowBorder = false;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is show border.
        /// </summary>
        /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
        [Description("是否显示边框"), Category("自定义")]
        public bool IsShowBorder
        {
            get { return isShowBorder; }
            set
            {
                isShowBorder = value;
                Refresh();
            }
        }

        /// <summary>
        /// The lamp color
        /// </summary>
        private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };

        /// <summary>
        /// Gets or sets the color of the lamp.
        /// </summary>
        /// <value>The color of the lamp.</value>
        [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
        public Color[] LampColor
        {
            get { return lampColor; }
            set
            {
                if (value == null || value.Length <= 0)
                    return;
                lampColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The is highlight
        /// </summary>
        private bool isHighlight = true;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is highlight.
        /// </summary>
        /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
        [Description("是否高亮显示"), Category("自定义")]
        public bool IsHighlight
        {
            get { return isHighlight; }
            set
            {
                isHighlight = value;
                Refresh();
            }
        }

        /// <summary>
        /// The twinkle speed
        /// </summary>
        private int twinkleSpeed = 0;

        /// <summary>
        /// Gets or sets the twinkle speed.
        /// </summary>
        /// <value>The twinkle speed.</value>
        [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
        public int TwinkleSpeed
        {
            get { return twinkleSpeed; }
            set
            {
                if (value < 0)
                    return;
                twinkleSpeed = value;
                if (value == 0 || lampColor.Length <= 1)
                {
                    timer.Enabled = false;
                }
                else
                {
                    intColorIndex = 0;
                    timer.Interval = value;
                    timer.Enabled = true;
                }
                Refresh();
            }
        }
        /// <summary>
        /// The timer
        /// </summary>
        Timer timer;
        /// <summary>
        /// The int color index
        /// </summary>
        int intColorIndex = 0;
        /// <summary>
        /// Initializes a new instance of the <see cref="UCSignalLamp"/> class.
        /// </summary>
        public UCSignalLamp()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Size = new Size(50, 50);
            this.SizeChanged += UCSignalLamp_SizeChanged;
            timer = new Timer();
            timer.Interval = 200;
            timer.Tick += timer_Tick;
        }

        /// <summary>
        /// Handles the Tick event of the timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void timer_Tick(object sender, EventArgs e)
        {
            intColorIndex++;
            if (intColorIndex >= lampColor.Length)
                intColorIndex = 0;
            Refresh();
        }
        /// <summary>
        /// Handles the SizeChanged event of the UCSignalLamp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void UCSignalLamp_SizeChanged(object sender, EventArgs e)
        {
            var maxSize = Math.Min(this.Width, this.Height);
            if (this.Width != maxSize)
                this.Width = maxSize;
            if (this.Height != maxSize)
                this.Height = maxSize;
        }

        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SetGDIHigh();
            Color c1 = lampColor[intColorIndex];
            g.FillEllipse(new SolidBrush(c1), this.ClientRectangle);

            if (isHighlight)
            {
                GraphicsPath gp = new GraphicsPath();

                Rectangle rec = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
                gp.AddEllipse(rec);

                Color[] surroundColor = new Color[] { c1 };
                PathGradientBrush pb = new PathGradientBrush(gp);
                pb.CenterColor = Color.White;
                pb.SurroundColors = surroundColor;
                g.FillPath(pb, gp);
            }

            if (isShowBorder)
            {
                g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 8, this.Height - 8));
            }
        }
    }
}

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐