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

用处及效果

准备工作

这个是基于(四十一)c#Winform自定义控件-进度条 扩展的,如果你还没有了解,请先移步了解一下

开始

添加一个用户控件,命名UCProcessLineExt

属性

[Description("值变更事件"), Category("自定义")]
        public event EventHandler ValueChanged;

        [Description("当前属性"), Category("自定义")]
        public int Value
        {
            set
            {
                ucProcessLine1.Value = value;
                Refresh();
            }
            get
            {
                return ucProcessLine1.Value;
            }
        }



        [Description("最大值"), Category("自定义")]
        public int MaxValue
        {
            get { return ucProcessLine1.MaxValue; }
            set
            {
                ucProcessLine1.MaxValue = value;
                Refresh();
            }
        }


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


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


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

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

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

重绘

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SetGDIHigh();
            float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;

            int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
            GraphicsPath path = new GraphicsPath();
            Rectangle rect = new Rectangle(x, 1, 30, 20);
            int cornerRadius = 2;
            path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//下
            path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
            path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
            path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//下
            path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//上
            path.CloseFigure();

            e.Graphics.FillPath(new SolidBrush(ForeColor), path);

            string strValue = ((float)Value / (float)MaxValue).ToString("0%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
        }

全部代码

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 UCProcessLineExt : UserControl
    {
        [Description("值变更事件"), Category("自定义")]
        public event EventHandler ValueChanged;

        [Description("当前属性"), Category("自定义")]
        public int Value
        {
            set
            {
                ucProcessLine1.Value = value;
                Refresh();
            }
            get
            {
                return ucProcessLine1.Value;
            }
        }



        [Description("最大值"), Category("自定义")]
        public int MaxValue
        {
            get { return ucProcessLine1.MaxValue; }
            set
            {
                ucProcessLine1.MaxValue = value;
                Refresh();
            }
        }


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


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


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

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

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

        public UCProcessLineExt()
        {
            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);
            ucProcessLine1.ValueChanged += ucProcessLine1_ValueChanged;
        }

        void ucProcessLine1_ValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, e);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SetGDIHigh();
            float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;

            int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
            GraphicsPath path = new GraphicsPath();
            Rectangle rect = new Rectangle(x, 1, 30, 20);
            int cornerRadius = 2;
            path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//下
            path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
            path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
            path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//下
            path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//上
            path.CloseFigure();

            e.Graphics.FillPath(new SolidBrush(ForeColor), path);

            string strValue = ((float)Value / (float)MaxValue).ToString("0%");
            System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
            e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCProcessLineExt
    {
        /// <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.ucProcessLine1 = new HZH_Controls.Controls.UCProcessLine();
            this.SuspendLayout();
            // 
            // ucProcessLine1
            // 
            this.ucProcessLine1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.ucProcessLine1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
            this.ucProcessLine1.Font = new System.Drawing.Font("Arial Unicode MS", 10F);
            this.ucProcessLine1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.ucProcessLine1.Location = new System.Drawing.Point(18, 33);
            this.ucProcessLine1.MaxValue = 100;
            this.ucProcessLine1.Name = "ucProcessLine1";
            this.ucProcessLine1.Size = new System.Drawing.Size(399, 16);
            this.ucProcessLine1.TabIndex = 0;
            this.ucProcessLine1.Text = "ucProcessLine1";
            this.ucProcessLine1.Value = 0;
            this.ucProcessLine1.ValueBGColor = System.Drawing.Color.White;
            this.ucProcessLine1.ValueColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
            this.ucProcessLine1.ValueTextType = HZH_Controls.Controls.ValueTextType.None;
            // 
            // UCProcessLineExt
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this.ucProcessLine1);
            this.Name = "UCProcessLineExt";
            this.Size = new System.Drawing.Size(434, 50);
            this.ResumeLayout(false);

        }

        #endregion

        private UCProcessLine ucProcessLine1;
    }
}

最后的话

如果你喜欢的话,请到 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/11392351.html

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

发表评论

登录后才能评论