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

用处及效果

准备工作

这个用到GDI+画的,请先了解一下GDI+

还有用到了基类控件UCControlBase来控制圆角和背景色,如果还不了解请移步查看

(一)c#Winform自定义控件-基类控件

另外用到了水波控件UCWave,如果不了解请移步查看

(四十四)c#Winform自定义控件-水波

开始

添加一个用户控件UCProcessWave,继承UCControlBase

一些属性

private bool m_isRectangle = false;
        [Description("是否矩形"), Category("自定义")]
        public bool IsRectangle
        {
            get { return m_isRectangle; }
            set
            {
                m_isRectangle = value;
                if (value)
                {
                    base.ConerRadius = 10;
                }
                else
                {
                    base.ConerRadius = Math.Min(this.Width, this.Height);
                }
            }
        }
        #region 不再使用的父类属性    English:Parent class attributes that are no longer used
        [Browsable(false)]
        public new int ConerRadius
        {
            get;
            set;
        }
        [Browsable(false)]
        public new bool IsRadius
        {
            get;
            set;
        }

        [Browsable(false)]
        public new Color FillColor
        {
            get;
            set;
        }
        #endregion


        [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);
                ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
                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();
            }
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Description("值颜色"), Category("自定义")]
        public Color ValueColor
        {
            get { return this.ucWave1.WaveColor; }
            set
            {
                this.ucWave1.WaveColor = value;
            }
        }

        [Description("边框宽度"), Category("自定义")]
        public override int RectWidth
        {
            get
            {
                return base.RectWidth;
            }
            set
            {
                if (value < 4)
                    base.RectWidth = 4;
                else
                    base.RectWidth = value;
            }
        }

构造函数一些设置

public UCProcessWave()
        {
            InitializeComponent();
            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);
            base.IsRadius = true;
            base.IsShowRect = false;
            RectWidth = 4;
            RectColor = Color.White;
            ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
            this.SizeChanged += UCProcessWave_SizeChanged;
            this.ucWave1.OnPainted += ucWave1_Painted;
            base.ConerRadius = Math.Min(this.Width, this.Height);
        }

重绘

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SetGDIHigh();
            if (!m_isRectangle)
            {
                //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
                SolidBrush solidBrush = new SolidBrush(Color.White);
                e.Graphics.DrawEllipse(new Pen(solidBrush, 2), new Rectangle(-1, -1, this.Width + 2, this.Height + 2));
            }
            string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
        }

波形控件重绘时处理

void ucWave1_Painted(object sender, PaintEventArgs e)
        {
            e.Graphics.SetGDIHigh();
            if (IsShowRect)
            {
                if (m_isRectangle)
                {
                    Color rectColor = RectColor;
                    Pen pen = new Pen(rectColor, (float)RectWidth);
                    Rectangle clientRectangle = new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height);
                    GraphicsPath graphicsPath = new GraphicsPath();
                    graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f);
                    graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f);
                    graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f);
                    graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f);
                    graphicsPath.CloseFigure();
                    e.Graphics.DrawPath(pen, graphicsPath);
                }
                else
                {
                    SolidBrush solidBrush = new SolidBrush(RectColor);
                    e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height));
                }
            }

            if (!m_isRectangle)
            {
                //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
                SolidBrush solidBrush1 = new SolidBrush(Color.White);
                e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.ucWave1.Height - this.Height - 1, this.Width + 2, this.Height + 2));
            }
            string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / 2));
        }

不知道你们有没有注意这句话

//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡

因为设置原价导致了区域毛边,所有画个没有毛边的边框覆盖之

完整代码

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 partial class UCProcessWave : UCControlBase
    {
        private bool m_isRectangle = false;
        [Description("是否矩形"), Category("自定义")]
        public bool IsRectangle
        {
            get { return m_isRectangle; }
            set
            {
                m_isRectangle = value;
                if (value)
                {
                    base.ConerRadius = 10;
                }
                else
                {
                    base.ConerRadius = Math.Min(this.Width, this.Height);
                }
            }
        }
        #region 不再使用的父类属性    English:Parent class attributes that are no longer used
        [Browsable(false)]
        public new int ConerRadius
        {
            get;
            set;
        }
        [Browsable(false)]
        public new bool IsRadius
        {
            get;
            set;
        }

        [Browsable(false)]
        public new Color FillColor
        {
            get;
            set;
        }
        #endregion


        [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);
                ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
                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();
            }
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Description("值颜色"), Category("自定义")]
        public Color ValueColor
        {
            get { return this.ucWave1.WaveColor; }
            set
            {
                this.ucWave1.WaveColor = value;
            }
        }

        [Description("边框宽度"), Category("自定义")]
        public override int RectWidth
        {
            get
            {
                return base.RectWidth;
            }
            set
            {
                if (value < 4)
                    base.RectWidth = 4;
                else
                    base.RectWidth = value;
            }
        }

        public UCProcessWave()
        {
            InitializeComponent();
            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);
            base.IsRadius = true;
            base.IsShowRect = false;
            RectWidth = 4;
            RectColor = Color.White;
            ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
            this.SizeChanged += UCProcessWave_SizeChanged;
            this.ucWave1.OnPainted += ucWave1_Painted;
            base.ConerRadius = Math.Min(this.Width, this.Height);
        }

        void ucWave1_Painted(object sender, PaintEventArgs e)
        {
            e.Graphics.SetGDIHigh();
            if (IsShowRect)
            {
                if (m_isRectangle)
                {
                    Color rectColor = RectColor;
                    Pen pen = new Pen(rectColor, (float)RectWidth);
                    Rectangle clientRectangle = new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height);
                    GraphicsPath graphicsPath = new GraphicsPath();
                    graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f);
                    graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f);
                    graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f);
                    graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f);
                    graphicsPath.CloseFigure();
                    e.Graphics.DrawPath(pen, graphicsPath);
                }
                else
                {
                    SolidBrush solidBrush = new SolidBrush(RectColor);
                    e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height));
                }
            }

            if (!m_isRectangle)
            {
                //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
                SolidBrush solidBrush1 = new SolidBrush(Color.White);
                e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.ucWave1.Height - this.Height - 1, this.Width + 2, this.Height + 2));
            }
            string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / 2));
        }

        void UCProcessWave_SizeChanged(object sender, EventArgs e)
        {
            if (!m_isRectangle)
            {
                base.ConerRadius = Math.Min(this.Width, this.Height);
                if (this.Width != this.Height)
                {
                    this.Size = new Size(Math.Min(this.Width, this.Height), Math.Min(this.Width, this.Height));
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SetGDIHigh();
            if (!m_isRectangle)
            {
                //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
                SolidBrush solidBrush = new SolidBrush(Color.White);
                e.Graphics.DrawEllipse(new Pen(solidBrush, 2), new Rectangle(-1, -1, this.Width + 2, this.Height + 2));
            }
            string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCProcessWave
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.ucWave1 = new HZH_Controls.Controls.UCWave();
            this.SuspendLayout();
            // 
            // ucWave1
            // 
            this.ucWave1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.ucWave1.Location = new System.Drawing.Point(0, 140);
            this.ucWave1.Name = "ucWave1";
            this.ucWave1.Size = new System.Drawing.Size(150, 10);
            this.ucWave1.TabIndex = 0;
            this.ucWave1.Text = "ucWave1";
            this.ucWave1.WaveColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
            this.ucWave1.WaveHeight = 15;
            this.ucWave1.WaveSleep = 100;
            this.ucWave1.WaveWidth = 100;
            // 
            // UCProcessWave
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(197)))), ((int)(((byte)(229)))), ((int)(((byte)(250)))));
            this.Controls.Add(this.ucWave1);
            this.Name = "UCProcessWave";
            this.Size = new System.Drawing.Size(150, 150);
            this.ResumeLayout(false);

        }

        #endregion

        private UCWave ucWave1;
    }
}

最后的话

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

作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

原文出处:冰封一夏

原文链接:https://www.cnblogs.com/bfyx/p/11399183.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论