(四十一)c#Winform自定义控件-进度条

前提

入行已经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+再重绘一个,不了解GDI+的自行百度了解下先

(七)c#Winform自定义控件-进度条

开始

添加一个类,命名UCProcessLine,继承Control

添加一个枚举,用以如何显示值

复制代码

 1  public enum ValueTextType
 2     {
 3         None,
 4         /// <summary>
 5         /// 百分比
 6         /// </summary>
 7         Percent,
 8         /// <summary>
 9         /// 数值
10         /// </summary>
11         Absolute
12     }

复制代码

 

添加一些属性

复制代码

  1 [Description("值变更事件"), Category("自定义")]
  2         public event EventHandler ValueChanged;
  3         int m_value = 0;
  4         [Description("当前属性"), Category("自定义")]
  5         public int Value
  6         {
  7             set
  8             {
  9                 if (value > m_maxValue)
 10                     m_value = m_maxValue;
 11                 else if (value < 0)
 12                     m_value = 0;
 13                 else
 14                     m_value = value;
 15                 if (ValueChanged != null)
 16                     ValueChanged(this, null);
 17                 Refresh();
 18             }
 19             get
 20             {
 21                 return m_value;
 22             }
 23         }
 24 
 25         private int m_maxValue = 100;
 26 
 27         [Description("最大值"), Category("自定义")]
 28         public int MaxValue
 29         {
 30             get { return m_maxValue; }
 31             set
 32             {
 33                 if (value < m_value)
 34                     m_maxValue = m_value;
 35                 else
 36                     m_maxValue = value;
 37                 Refresh();
 38             }
 39         }
 40 
 41         Color m_valueColor = Color.FromArgb(73, 119, 232);
 42 
 43         [Description("值进度条颜色"), Category("自定义")]
 44         public Color ValueColor
 45         {
 46             get { return m_valueColor; }
 47             set
 48             {
 49                 m_valueColor = value;
 50                 Refresh();
 51             }
 52         }
 53 
 54         private Color m_valueBGColor = Color.White;
 55 
 56         [Description("值背景色"), Category("自定义")]
 57         public Color ValueBGColor
 58         {
 59             get { return m_valueBGColor; }
 60             set
 61             {
 62                 m_valueBGColor = value;
 63                 Refresh();
 64             }
 65         }
 66 
 67         private Color m_borderColor = Color.FromArgb(192, 192, 192);
 68 
 69         [Description("边框颜色"), Category("自定义")]
 70         public Color BorderColor
 71         {
 72             get { return m_borderColor; }
 73             set
 74             {
 75                 m_borderColor = value;
 76                 Refresh();
 77             }
 78         }
 79 
 80         [Description("值字体"), Category("自定义")]
 81         public override Font Font
 82         {
 83             get
 84             {
 85                 return base.Font;
 86             }
 87             set
 88             {
 89                 base.Font = value;
 90                 Refresh();
 91             }
 92         }
 93 
 94         [Description("值字体颜色"), Category("自定义")]
 95         public override System.Drawing.Color ForeColor
 96         {
 97             get
 98             {
 99                 return base.ForeColor;
100             }
101             set
102             {
103                 base.ForeColor = value;
104                 Refresh();
105             }
106         }
107         private ValueTextType m_valueTextType = ValueTextType.Percent;
108 
109         [Description("值显示样式"), Category("自定义")]
110         public ValueTextType ValueTextType
111         {
112             get { return m_valueTextType; }
113             set
114             {
115                 m_valueTextType = value;
116                 Refresh();
117             }
118         }

复制代码

重绘

复制代码

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             Console.WriteLine(DateTime.Now);
 4             base.OnPaint(e);
 5             Graphics g = e.Graphics;
 6             g.SetGDIHigh();
 7 
 8             Brush sb = new SolidBrush(m_valueBGColor);
 9             g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 2));
10             GraphicsPath path1 = ControlHelper.CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + 1, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 4), 3);
11             g.DrawPath(new Pen(m_borderColor, 1), path1);
12             LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(0, base.ClientRectangle.Height - 3), m_valueColor, Color.FromArgb(200, m_valueColor.R, m_valueColor.G, m_valueColor.B));
13             g.FillPath(lgb, ControlHelper.CreateRoundedRectanglePath(new Rectangle(0, (base.ClientRectangle.Height - (base.ClientRectangle.Height - 3)) / 2, (base.ClientRectangle.Width - 3) * Value / m_maxValue, base.ClientRectangle.Height - 4), 3));
14             string strValue = string.Empty;
15             if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Percent)
16                 strValue = ((float)Value / (float)m_maxValue).ToString("0%");
17             else if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Absolute)
18                 strValue = Value + "/" + m_maxValue;
19             if (!string.IsNullOrEmpty(strValue))
20             {
21                 System.Drawing.SizeF sizeF = g.MeasureString(strValue, Font);
22                 g.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
23             }
24         }

复制代码

完整代码来一份

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

namespace HZH_Controls.Controls
{
    public class UCProcessLine : Control
    {
        [Description("值变更事件"), Category("自定义")]
        public event EventHandler ValueChanged;
        int m_value = 0;
        [Description("当前属性"), Category("自定义")]
        public int Value
        {
            set
            {
                if (value > m_maxValue)
                    m_value = m_maxValue;
                else if (value < 0)
                    m_value = 0;
                else
                    m_value = value;
                if (ValueChanged != null)
                    ValueChanged(this, null);
                Refresh();
            }
            get
            {
                return m_value;
            }
        }

        private int m_maxValue = 100;

        [Description("最大值"), Category("自定义")]
        public int MaxValue
        {
            get { return m_maxValue; }
            set
            {
                if (value < m_value)
                    m_maxValue = m_value;
                else
                    m_maxValue = value;
                Refresh();
            }
        }

        Color m_valueColor = Color.FromArgb(73, 119, 232);

        [Description("值进度条颜色"), Category("自定义")]
        public Color ValueColor
        {
            get { return m_valueColor; }
            set
            {
                m_valueColor = value;
                Refresh();
            }
        }

        private Color m_valueBGColor = Color.White;

        [Description("值背景色"), Category("自定义")]
        public Color ValueBGColor
        {
            get { return m_valueBGColor; }
            set
            {
                m_valueBGColor = value;
                Refresh();
            }
        }

        private Color m_borderColor = Color.FromArgb(192, 192, 192);

        [Description("边框颜色"), Category("自定义")]
        public Color BorderColor
        {
            get { return m_borderColor; }
            set
            {
                m_borderColor = value;
                Refresh();
            }
        }

        [Description("值字体"), Category("自定义")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Refresh();
            }
        }

        [Description("值字体颜色"), Category("自定义")]
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                Refresh();
            }
        }
        private ValueTextType m_valueTextType = ValueTextType.Percent;

        [Description("值显示样式"), Category("自定义")]
        public ValueTextType ValueTextType
        {
            get { return m_valueTextType; }
            set
            {
                m_valueTextType = value;
                Refresh();
            }
        }
        public UCProcessLine()
        {
            Size = new Size(200, 15);
            ForeColor = Color.FromArgb(255, 77, 59);
            Font = new Font("Arial Unicode MS", 10);
            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);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Console.WriteLine(DateTime.Now);
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SetGDIHigh();

            Brush sb = new SolidBrush(m_valueBGColor);
            g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 2));
            GraphicsPath path1 = ControlHelper.CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + 1, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 4), 3);
            g.DrawPath(new Pen(m_borderColor, 1), path1);
            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(0, base.ClientRectangle.Height - 3), m_valueColor, Color.FromArgb(200, m_valueColor.R, m_valueColor.G, m_valueColor.B));
            g.FillPath(lgb, ControlHelper.CreateRoundedRectanglePath(new Rectangle(0, (base.ClientRectangle.Height - (base.ClientRectangle.Height - 3)) / 2, (base.ClientRectangle.Width - 3) * Value / m_maxValue, base.ClientRectangle.Height - 4), 3));
            string strValue = string.Empty;
            if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Percent)
                strValue = ((float)Value / (float)m_maxValue).ToString("0%");
            else if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Absolute)
                strValue = Value + "/" + m_maxValue;
            if (!string.IsNullOrEmpty(strValue))
            {
                System.Drawing.SizeF sizeF = g.MeasureString(strValue, Font);
                g.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
            }
        }

    }

    public enum ValueTextType
    {
        None,
        /// <summary>
        /// 百分比
        /// </summary>
        Percent,
        /// <summary>
        /// 数值
        /// </summary>
        Absolute
    }
}

用处及效果

最后的话

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值