[C# Control] 仿RAR式进度条 (RarProgressBar)

本文介绍了一种仿RAR风格的自定义进度条组件RarProgressBar,该组件为Windows Forms应用程序提供了一个不同于默认样式的新选择。文章详细展示了如何通过C#实现这一控件,并提供了关键代码片段。
Download Source & Demo

r_rarprogressbar.png

System.Windows.Forms.ProgressBar的样式实在是太普通了, RarProgressBar让你多一种选择.
我并没有实现双进度显示, 因为我想不出除了文件压缩外还有什么地方能够用到双进度显示


ExpandedBlockStart.gif ContractedBlock.gif /**/ ///////////////////////////////////////////////////////////////////////////////
None.gif //   RarProgressBar.cs
None.gif
//   仿RAR式进度条
None.gif
//
None.gif
//   @version 1.00
None.gif
//   @remarks For study purpose
None.gif
//   @author Icebird @date 2006-07-03
ExpandedBlockStart.gifContractedBlock.gif
/**/ ///////////////////////////////////////////////////////////////////////////////
None.gif using  System;
None.gif
using  System.Windows.Forms;
None.gif
using  System.ComponentModel;
None.gif
using  System.Collections;
None.gif
using  System.Diagnostics;
None.gif
using  System.Drawing;
None.gif
None.gif
namespace  ControlsEx
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// RarProgressBar
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Value")]
InBlock.gif    
public class RarProgressBar : System.Windows.Forms.Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Pen whitePen, whitePen2;
InBlock.gif        
private Pen blackPen;
InBlock.gif        
private Pen blackPen2;
InBlock.gif        
private Pen lightGrayPen;
InBlock.gif        
private Pen lightGrayPen2;
InBlock.gif        
private Brush foreBrush;
InBlock.gif
InBlock.gif        
private int maximum;
InBlock.gif        
private int minimum;
InBlock.gif        
private int step;
InBlock.gif        
private int value;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Constructor
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public RarProgressBar()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SetStyle(ControlStyles.Selectable, 
false);
InBlock.gif            SetStyle(ControlStyles.SupportsTransparentBackColor 
| ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer |
InBlock.gif                ControlStyles.AllPaintingInWmPaint 
| ControlStyles.UserPaint, true);
InBlock.gif            
this.minimum = 0;
InBlock.gif            
this.maximum = 100;
InBlock.gif            
this.step = 10;
InBlock.gif            
this.value = 0;
InBlock.gif            
this.BackColor = ColorTranslator.FromHtml("#946D6B");
InBlock.gif            
this.ForeColor = ColorTranslator.FromHtml("#D6D7DE");
InBlock.gif            
this.Height = 12;
InBlock.gif
InBlock.gif            whitePen 
= new Pen(Color.White);
InBlock.gif            whitePen2 
= new Pen(ColorTranslator.FromHtml("#EFEBEF"));
InBlock.gif            blackPen 
= new Pen(ColorTranslator.FromHtml("#636163"));
InBlock.gif            blackPen2 
= new Pen(ColorTranslator.FromHtml("#424142"));
InBlock.gif            lightGrayPen 
= new Pen(ColorTranslator.FromHtml("#B59694"));
InBlock.gif            lightGrayPen2 
= new Pen(ColorTranslator.FromHtml("#9C8284"));
InBlock.gif            foreBrush 
= new SolidBrush(ForeColor);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                whitePen.Dispose();
InBlock.gif                whitePen2.Dispose();
InBlock.gif                blackPen.Dispose();
InBlock.gif                blackPen2.Dispose();
InBlock.gif                lightGrayPen.Dispose();
InBlock.gif                lightGrayPen2.Dispose();
InBlock.gif                foreBrush.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前值增加指定增量
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="value"> 增量</param>

InBlock.gif        public void Increment(int value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.value += value;
InBlock.gif            
if (this.value < this.minimum)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.value = this.minimum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.value > this.maximum)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.value = this.maximum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.UpdatePos();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前值增加固定增量(Step)
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void PerformStep()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Increment(this.step);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 输出字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string s = base.ToString();
InBlock.gif            
return (s + ", Minimum: " + this.Minimum.ToString() + ", Maximum: " + this.Maximum.ToString() + ", Value: " + this.Value.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void UpdatePos()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Invalidate();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
false), EditorBrowsable(EditorBrowsableState.Never)]
InBlock.gif        
public override bool AllowDrop
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.AllowDrop;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.AllowDrop = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [EditorBrowsable(EditorBrowsableState.Never), Browsable(
false)]
InBlock.gif        
public override Color BackColor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.BackColor;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.BackColor = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
false), EditorBrowsable(EditorBrowsableState.Never)]
InBlock.gif        
public override Image BackgroundImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.BackgroundImage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.BackgroundImage = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override System.Windows.Forms.ImeMode DefaultImeMode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return System.Windows.Forms.ImeMode.Disable;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override Size DefaultSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Size(10012);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [EditorBrowsable(EditorBrowsableState.Never), Browsable(
false)]
InBlock.gif        
public override System.Drawing.Font Font
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Font;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Font = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
false), EditorBrowsable(EditorBrowsableState.Never)]
InBlock.gif        
public override Color ForeColor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.ForeColor;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.ForeColor = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DescriptionAttribute(
"此 ProgressBar 正使用的范围的上限"),RefreshProperties(RefreshProperties.Repaint), DefaultValue(100), CategoryAttribute("行为")]
InBlock.gif        
public int Maximum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.maximum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.maximum != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (value < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
throw new ArgumentException(String.Format("'{1}'不是'{0}'的有效值。'{0}'必须大于或等于 {2}。"new object[] dot.gif"maximum", value.ToString(), "0" }));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if (this.minimum > value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.minimum = value;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
this.maximum = value;
InBlock.gif                    
if (this.value > this.maximum)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.value = this.maximum;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
this.UpdatePos();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [RefreshProperties(RefreshProperties.Repaint), DescriptionAttribute(
"此 ProgressBar 正使用的范围的下限"), CategoryAttribute("行为"), DefaultValue(0)]
InBlock.gif        
public int Minimum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.minimum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.minimum != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (value < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
throw new ArgumentException(String.Format("'{1}'不是'{0}'的有效值。'{0}'必须大于或等于 {2}。"new object[] dot.gif"minimum", value.ToString(), "0" }));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if (this.maximum < value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.maximum = value;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
this.minimum = value;
InBlock.gif                    
if (this.value < this.minimum)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.value = this.minimum;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
this.UpdatePos();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
false), EditorBrowsable(EditorBrowsableState.Never)]
InBlock.gif        
public override System.Windows.Forms.RightToLeft RightToLeft
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.RightToLeft;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.RightToLeft = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DescriptionAttribute(
"当调用 Step() 方法时,控件当前值的增量。"), DefaultValue(10), CategoryAttribute("行为")]
InBlock.gif        
public int Step
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.step;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.step = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Bindable(
false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
InBlock.gif        
public override string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Text = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DefaultValue(
0), DescriptionAttribute("ProgressBar 的当前值,在由最小和最大属性指定的范围之内。"), Bindable(true), CategoryAttribute("行为")]
InBlock.gif        
public int Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.value != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ((value < this.minimum) || (value > this.maximum))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
throw new ArgumentException(String.Format("'{1}'不是'{0}'的有效值。'{0}'应介于 '{2}' 和 '{3}' 之间。"new object[] dot.gif"value", value.ToString(), "'minimum'""'maximum'" }));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
this.value = value;
InBlock.gif                    
this.UpdatePos();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnMouseEnter(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnMouseEnter (e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int ProgressWidth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToInt32(Math.Floor(1.0 * ClientSize.Width / Maximum * Value));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Graphics g 
= e.Graphics;
InBlock.gif
InBlock.gif            
int pWidth = ProgressWidth;
InBlock.gif            Pen pen;
InBlock.gif            
if (Parent != null)
InBlock.gif                pen 
= new Pen(Parent.BackColor);
InBlock.gif            
else
InBlock.gif                pen 
= new Pen(ColorTranslator.FromHtml("#EFEBE7"));
InBlock.gif            g.DrawRectangle(pen, 
00, ClientSize.Width - 1, ClientSize.Height - 1);
InBlock.gif            pen.Dispose();
InBlock.gif
InBlock.gif            g.FillRectangle(foreBrush, 
00, pWidth, ClientSize.Height - 3 );
InBlock.gif            g.DrawRectangle(whitePen, 
00, pWidth, ClientSize.Height - 2);
InBlock.gif            g.DrawRectangle(whitePen2, 
11, pWidth - 2, ClientSize.Height - 4);
InBlock.gif
InBlock.gif            
//drawing right progress
InBlock.gif
            if (Value < Maximum)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                g.DrawRectangle(lightGrayPen, pWidth, 
0, ClientSize.Width - pWidth - 2, ClientSize.Height - 3);
InBlock.gif                g.DrawRectangle(lightGrayPen2, pWidth 
+ 11, ClientSize.Width - pWidth - 4, ClientSize.Height - 5);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//drawing separator
InBlock.gif
            g.DrawLine(blackPen, pWidth, 0, pWidth,  ClientSize.Height - 3);
InBlock.gif
InBlock.gif            
//drawing right & bottom borders
InBlock.gif
            g.DrawLine(blackPen,  0, ClientSize.Height - 2,  ClientSize.Width - 1,  ClientSize.Height - 2);
InBlock.gif            g.DrawLine(blackPen2,  
1, ClientSize.Height - 1,  ClientSize.Width,  ClientSize.Height - 1);
InBlock.gif            g.DrawLine(blackPen2,  ClientSize.Width 
- 1,  ClientSize.Height - 1,  ClientSize.Width - 1,  2);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值