C#平滑进度条

ContractedBlock.gif ExpandedBlockStart.gif SmoothProgressBar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmoothProgressBar
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class SmoothProgressBar : UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
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(87148249);        // Color of progress meter

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (disposing && (components != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>

        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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(1611);
            
this.panel1.Name = "panel1";
            
this.panel1.Size = new System.Drawing.Size(26114);
            
this.panel1.TabIndex = 0;
            
// 
            
// label1
            
// 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(28313);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(1712);
            
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(31639);
            
this.ResumeLayout(false);
            
this.PerformLayout();

        }


        
#endregion


        
private System.Windows.Forms.Panel panel1;
        
private System.Windows.Forms.Label label1;


        
//
        public SmoothProgressBar()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
            
this.panel1.Paint += new PaintEventHandler(panel1_Paint);
            
this.panel1.Resize += new EventHandler(panel1_Resize);
        }


        
void panel1_Resize(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//throw new Exception("The method or operation is not implemented.");
            this.panel1.Invalidate();
        }


        
void panel1_Paint(object sender, PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return min;
            }


            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
// Prevent a negative value.
                if (value < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    min 
= 0;
                }


                
// Make sure that the minimum value is never set higher than the maximum value.
                if (value > max)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    min 
= value;
                    min 
= value;
                }


                
// Ensure value is still in range
                if (val < min)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    val 
= min;
                }


                
// Invalidate the control to get a repaint.
                this.panel1.Invalidate();
            }

        }


        
public int Maximum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return max;
            }


            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
// Make sure that the maximum value is never set lower than the minimum value.
                if (value < min)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    min 
= value;
                }


                max 
= value;

                
// Make sure that value is still in range.
                if (val > max)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    val 
= max;
                }


                
// Invalidate the control to get a repaint.
                this.panel1.Invalidate();              
            }

        }


        
public int Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return val;
            }


            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
int oldValue = val;

                
// Make sure that the value does not stray outside the valid range.
                if (value < min)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    val 
= min;
                    
this.label1.Text = "0%";
                }

                
else if (value > max)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    val 
= max;
                    
this.label1.Text = "100%";
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    updateRect.X 
= oldValueRect.Size.Width;
                    updateRect.Width 
= newValueRect.Width - oldValueRect.Width;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    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
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return BarColor;
            }


            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                BarColor 
= value;

                
// Invalidate the control to get a repaint.
                this.panel1.Invalidate();
            }

        }


        
private void Draw3DBorder(Graphics g)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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));
        }

    }

}

效果图:

转载于:https://www.cnblogs.com/JinDin/archive/2009/08/31/1557263.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值