c# 自定义控件 柱状进度条 WINFORM

很多时候进度条都是横向的,但是表示一些数量时,柱状体的进度条好一些,本文将介绍一下竖版的自定义进度条制作。

先说一下自定义进度条的原理。初始化一个30,150大小的自定义控件。然后赋值一个底色,然后对高度值自增,通过自增的高度值来画一个矩形,这个矩形颜色用其它颜色表示,这个不断变小的矩形,就会露出下边底色的矩形。给人的感觉就是进度条再走。不多说了,上代码,已测试过。



按暂停间效果。

自定义控件:UserControls.cs

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

namespace UserProgressBar
{
    public partial class UserProgressBar : UserControl
    {
        private int min;//最小值
        private int max;//最大值
        private int val;//进度值
        private Color BarColor;//初始化颜色
        public UserProgressBar(int min, int max, int val, Color BackColor)
        {
            InitializeComponent();
            this.Min = min;
            this.Max = max;
            this.Val = val;
            this.ProgressBarColor = BackColor;
        }
        public Color ProgressBarColor
        {
            get
            {
                return BarColor;
            }
            set
            {
                BarColor = value;
            }
        }
       
        public int Min
        {
            get 
            { 
                return min;
            }
            set 
            { 
               min = value;
               this.Invalidate();
            }
        }

        public int Max
        {
            get 
            { 
                return max; 
            }
            set 
            { 
                max = value;
                this.Invalidate();
            }
        }
               
        public int Val
        {
            get 
            { 
                return val; 
            }
            set 
            {
                int oldValue = val;
                if(value < min)
                {
                    val = min;
                }
                else
                {
                    val = value; 
                }
                float percent;

                Rectangle newValueRect = this.ClientRectangle;
                Rectangle oldValueRect = this.ClientRectangle;
                
                percent = 1.0f - (float)(val - min) / (float)(max - min);
                newValueRect.Height = (int)((float)newValueRect.Height * percent);

                percent = 1.0f - (float)(oldValue - min) / (float)(max - min);
                oldValueRect.Height = (int)((float)oldValueRect.Height * percent);

                Rectangle updateRect = new Rectangle();
                if (newValueRect.Height > oldValueRect.Height)
                {
                    updateRect.Y = oldValueRect.Size.Height;
                    updateRect.Height = newValueRect.Height - oldValueRect.Height;
                }
                else
                {
                    updateRect.Y = newValueRect.Size.Height;
                    updateRect.Height = oldValueRect.Height - newValueRect.Height;
                }
                updateRect.Width = this.Width;
                this.Invalidate(updateRect);
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(BarColor);
            float percent = 1.0f - (float)(val - min) / (float)(max - min);
            Rectangle rect = this.ClientRectangle;
            rect.Height = (int)((float)rect.Height * percent);
            Console.WriteLine("高度:{0}", rect.Height);
            g.FillRectangle(brush, rect);
            brush.Dispose();
            g.Dispose();
        }
    }
}

自定义控件UserControls.Designer.cs

namespace UserProgressBar
{
    partial class UserProgressBar
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "UserProgressBar";
            this.Size = new System.Drawing.Size(30, 150);
            this.ResumeLayout(false);
        }

        #endregion
    }
}

测试工程:Form1.cs 记得添加自定义控件引用,这里就不再说明了。

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

namespace TestProject
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();

        //free console
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();

        public Form1()
        {
            InitializeComponent();
            AllocConsole();
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Yellow;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.userProgressBar1.Val >= 0)
            {
                this.userProgressBar1.Val++;
                this.userProgressBar2.Val++;
            }
            else
            {
                this.timer1.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.userProgressBar1.Val = 0;
            this.userProgressBar2.Val = 0;
            this.timer1.Interval = 1;
            this.timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.button2.Text == "暂停")
            {
                this.button2.Text = "继续";
                this.timer1.Enabled = false;
            }
            else
            {
                this.button2.Text = "暂停";
                this.timer1.Enabled = true;
            }
        }
    }
}

Form1.Designer.cs  两个按钮,两个自定义控件,一个Timer计时器控件。

namespace TestProject
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.userProgressBar1 = new UserProgressBar.UserProgressBar(0, 200, 0, System.Drawing.Color.MediumSpringGreen);
            this.userProgressBar2 = new UserProgressBar.UserProgressBar(0, 100, 0, System.Drawing.Color.MediumSpringGreen);
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(61, 82);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "开始";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // userProgressBar1
            // 
            this.userProgressBar1.BackColor = System.Drawing.Color.Red;
            this.userProgressBar1.Location = new System.Drawing.Point(218, 37);
            this.userProgressBar1.Name = "userProgressBar1";
            this.userProgressBar1.ProgressBarColor = System.Drawing.Color.Blue;
            this.userProgressBar1.Size = new System.Drawing.Size(30, 150);
            this.userProgressBar1.TabIndex = 0;
            this.userProgressBar1.Val = 0;
            // 
            // userProgressBar2
            // 
            this.userProgressBar2.BackColor = System.Drawing.Color.Red;
            this.userProgressBar2.Location = new System.Drawing.Point(152, 39);
            this.userProgressBar2.Name = "userProgressBar2";
            this.userProgressBar2.ProgressBarColor = System.Drawing.Color.MediumSpringGreen;
            this.userProgressBar2.Size = new System.Drawing.Size(30, 150);
            this.userProgressBar2.TabIndex = 2;
            this.userProgressBar2.Val = 0;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(61, 125);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "暂停";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.userProgressBar2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.userProgressBar1);
            this.Name = "Form1";
            this.Text = "向上进度条演示";
            this.ResumeLayout(false);

        }

        #endregion

        private UserProgressBar.UserProgressBar userProgressBar1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Button button1;
        private UserProgressBar.UserProgressBar userProgressBar2;
        private System.Windows.Forms.Button button2;
    }
}




猜你喜欢

转载自blog.csdn.net/chulijun3107/article/details/79665634