C#平滑进度条
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace SmoothProgressBar

{
public partial class SmoothProgressBar : UserControl
{
int min = 0; // Minimum value for progress range
int max = 100; // Maximum value for progress range
int val = 0; // Current progress
Color BarColor = Color.FromArgb(87, 148, 249); // Color of progress meter

/**//// <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.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Location = new System.Drawing.Point(16, 11);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(261, 14);
this.panel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(283, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 12);
this.label1.TabIndex = 1;
this.label1.Text = "0%";
//
// SmoothProgressBar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label1);
this.Controls.Add(this.panel1);
this.Name = "SmoothProgressBar";
this.Size = new System.Drawing.Size(316, 39);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;

//
public SmoothProgressBar()
{
InitializeComponent();
this.panel1.Paint += new PaintEventHandler(panel1_Paint);
this.panel1.Resize += new EventHandler(panel1_Resize);
}
void panel1_Resize(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
this.panel1.Invalidate();
}
void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush(BarColor);
float percent = (float)(val - min) / (float)(max - min);
Rectangle rect = this.panel1.ClientRectangle;
// Calculate area for drawing the progress.
rect.Width = (int)((float)rect.Width * percent);
// Draw the progress meter.
g.FillRectangle(brush, rect);
// Draw a three-dimensional border around the control.
Draw3DBorder(g);
// Clean up.
brush.Dispose();
g.Dispose();
}
public int Minimum
{
get
{
return min;
}
set
{
// Prevent a negative value.
if (value < 0)
{
min = 0;
}
// Make sure that the minimum value is never set higher than the maximum value.
if (value > max)
{
min = value;
min = value;
}
// Ensure value is still in range
if (val < min)
{
val = min;
}
// Invalidate the control to get a repaint.
this.panel1.Invalidate();
}
}
public int Maximum
{
get
{
return max;
}
set
{
// Make sure that the maximum value is never set lower than the minimum value.
if (value < min)
{
min = value;
}
max = value;
// Make sure that value is still in range.
if (val > max)
{
val = max;
}
// Invalidate the control to get a repaint.
this.panel1.Invalidate();
}
}
public int Value
{
get
{
return val;
}
set
{
int oldValue = val;
// Make sure that the value does not stray outside the valid range.
if (value < min)
{
val = min;
this.label1.Text = "0%";
}
else if (value > max)
{
val = max;
this.label1.Text = "100%";
}
else
{
val = value;
double d_val =Convert.ToDouble( val * 100 / max);
this.label1.Text = Convert.ToDouble(Math.Round(d_val)).ToString() + "%";
}
// Invalidate only the changed area.
float percent;
Rectangle newValueRect = this.panel1.ClientRectangle; //this.ClientRectangle;
Rectangle oldValueRect = this.panel1.ClientRectangle;//this.ClientRectangle;
// Use a new value to calculate the rectangle for progress.
percent = (float)(val - min) / (float)(max - min);
newValueRect.Width = (int)((float)newValueRect.Width * percent);
// Use an old value to calculate the rectangle for progress.
percent = (float)(oldValue - min) / (float)(max - min);
oldValueRect.Width = (int)((float)oldValueRect.Width * percent);
Rectangle updateRect = new Rectangle();
// Find only the part of the screen that must be updated.
if (newValueRect.Width > oldValueRect.Width)
{
updateRect.X = oldValueRect.Size.Width;
updateRect.Width = newValueRect.Width - oldValueRect.Width;
}
else
{
updateRect.X = newValueRect.Size.Width;
updateRect.Width = oldValueRect.Width - newValueRect.Width;
}
updateRect.Height = this.panel1.Height;
// Invalidate the intersection region only.
this.panel1.Invalidate(updateRect);
}
}
public Color ProgressBarColor
{
get
{
return BarColor;
}
set
{
BarColor = value;
// Invalidate the control to get a repaint.
this.panel1.Invalidate();
}
}
private void Draw3DBorder(Graphics g)
{
int PenWidth = (int)Pens.White.Width;
Rectangle clientRect = this.panel1.ClientRectangle;
g.DrawLine(Pens.DarkGray,
new Point(clientRect.Left, clientRect.Top),
new Point(clientRect.Width - PenWidth, clientRect.Top));
g.DrawLine(Pens.DarkGray,
new Point(clientRect.Left, clientRect.Top),
new Point(clientRect.Left, clientRect.Height - PenWidth));
g.DrawLine(Pens.White,
new Point(clientRect.Left, clientRect.Height - PenWidth),
new Point(clientRect.Width - PenWidth, clientRect.Height - PenWidth));
g.DrawLine(Pens.White,
new Point(clientRect.Width - PenWidth, clientRect.Top),
new Point(clientRect.Width - PenWidth, clientRect.Height - PenWidth));
}
}
}效果图:

浙公网安备 33010602011771号