Visual Studio .NET 2002.NET 1.0Windows 2003.NET 1.1Windows 2000Windows XP中级开发Visual StudioWindows.NETVisual BasicC#
彩色进度条






4.60/5 (43投票s)
2004年5月1日
2分钟阅读

330068

16696
一个有吸引力且更精美的进度条。
引言
这是一个相当简单的进度条控件,继承自Windows.Forms.Control
。我重新发明这个“轮子”的原因是我不喜欢 VS.NET 中提供的传统ProgressBar
的外观。我的ColorProgressBar
看起来更有吸引力且更精美,但保留了旧ProgressBar
的功能。希望我能得到您的一些真诚的意见或建议。
完整的源代码可以从上面的链接下载。
控件属性
BarColor
ColorProgressBar
的主颜色。BorderColor
ColorProgressBar
的边框颜色。FillStyle
条形样式,"
Solid
" 或 "Dashed
"。最大
ColorProgressBar
的最大值。最低
ColorProgressBar
的最小值或初始值。值
ColorProgressBar
的当前值。步骤
当您调用方法
PerformStep()
和PerformStepBack()
时,每次递增或递减的值。
控制方法
PerformStep()
调用
PerformStep()
方法来增加在Step
属性中设置的值。PerformStepBack()
调用
PerformStepBack()
方法来减少在Step
属性中设置的值。Increment(int value)
调用
Increment()
方法来增加您指定的整数值。Decrement(int value)
调用
Decrement()
方法来减少您指定的整数值。
示例
- 参见上面的快照
使用计时器来模拟进度。
//
// Control declarations
//
this.cpb1 = new ColorProgressBar.ColorProgressBar();
this.cpb2 = new ColorProgressBar.ColorProgressBar();
this.cpb3 = new ColorProgressBar.ColorProgressBar();
this.cpb4 = new ColorProgressBar.ColorProgressBar();
this.cpb5 = new ColorProgressBar.ColorProgressBar();
this.cpb6 = new ColorProgressBar.ColorProgressBar();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// cpb1
//
this.cpb1.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(128)), ((System.Byte)(128)));
this.cpb1.BorderColor = System.Drawing.Color.Black;
this.cpb1.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Dashed;
this.cpb1.Location = new System.Drawing.Point(56, 48);
this.cpb1.Maximum = 100;
this.cpb1.Minimum = 0;
this.cpb1.Name = "cpb1";
this.cpb1.Size = new System.Drawing.Size(150, 15);
this.cpb1.Step = 10;
this.cpb1.TabIndex = 0;
this.cpb1.Value = 0;
//
// cpb2
//
this.cpb2.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(255)), ((System.Byte)(128)));
this.cpb2.BorderColor = System.Drawing.Color.Black;
this.cpb2.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Solid;
this.cpb2.Location = new System.Drawing.Point(56, 72);
this.cpb2.Maximum = 100;
this.cpb2.Minimum = 0;
this.cpb2.Name = "cpb2";
this.cpb2.Size = new System.Drawing.Size(150, 15);
this.cpb2.Step = 10;
this.cpb2.TabIndex = 1;
this.cpb2.Value = 100;
//
// cpb3
//
this.cpb3.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(128)),
((System.Byte)(255)), ((System.Byte)(128)));
this.cpb3.BorderColor = System.Drawing.Color.Black;
this.cpb3.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Dashed;
this.cpb3.Location = new System.Drawing.Point(56, 104);
this.cpb3.Maximum = 100;
this.cpb3.Minimum = 0;
this.cpb3.Name = "cpb3";
this.cpb3.Size = new System.Drawing.Size(192, 24);
this.cpb3.Step = 10;
this.cpb3.TabIndex = 2;
this.cpb3.Value = 0;
//
// cpb4
//
this.cpb4.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(128)),
((System.Byte)(255)), ((System.Byte)(255)));
this.cpb4.BorderColor = System.Drawing.Color.Black;
this.cpb4.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Solid;
this.cpb4.Location = new System.Drawing.Point(56, 144);
this.cpb4.Maximum = 100;
this.cpb4.Minimum = 0;
this.cpb4.Name = "cpb4";
this.cpb4.Size = new System.Drawing.Size(192, 24);
this.cpb4.Step = 10;
this.cpb4.TabIndex = 3;
this.cpb4.Value = 100;
//
// cpb5
//
this.cpb5.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(128)), ((System.Byte)(255)));
this.cpb5.BorderColor = System.Drawing.Color.Black;
this.cpb5.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Solid;
this.cpb5.Location = new System.Drawing.Point(56, 184);
this.cpb5.Maximum = 100;
this.cpb5.Minimum = 0;
this.cpb5.Name = "cpb5";
this.cpb5.Size = new System.Drawing.Size(272, 32);
this.cpb5.Step = 10;
this.cpb5.TabIndex = 4;
this.cpb5.Value = 0;
//
// cpb6
//
this.cpb6.BarColor = System.Drawing.Color.FromArgb(((System.Byte)(255)),
((System.Byte)(128)), ((System.Byte)(0)));
this.cpb6.BorderColor = System.Drawing.Color.Black;
this.cpb6.FillStyle = ColorProgressBar.ColorProgressBar.FillStyles.Dashed;
this.cpb6.Location = new System.Drawing.Point(56, 232);
this.cpb6.Maximum = 100;
this.cpb6.Minimum = 0;
this.cpb6.Name = "cpb6";
this.cpb6.Size = new System.Drawing.Size(272, 32);
this.cpb6.Step = 10;
this.cpb6.TabIndex = 5;
this.cpb6.Value = 100;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Actual manipulation by timer1
//
private void timer1_Tick(object sender, System.EventArgs e)
{
//
// All values are reset at the end to have endless loop
//
cpb1.PerformStep();
if (cpb1.Value == cpb1.Maximum)
cpb1.Value = cpb1.Minimum;
cpb2.PerformStepBack();
if (cpb2.Value == cpb2.Minimum)
cpb2.Value = cpb2.Maximum;
cpb3.Increment(1);
if (cpb3.Value == cpb3.Maximum)
cpb3.Value = cpb3.Minimum;
cpb4.Decrement(1);
if (cpb4.Value == cpb4.Minimum)
cpb4.Value = cpb4.Maximum;
cpb5.Increment(1);
if (cpb5.Value == cpb5.Maximum)
cpb5.Value = cpb5.Minimum;
cpb6.Decrement(1);
if (cpb6.Value == cpb6.Minimum)
cpb6.Value = cpb6.Maximum;
}
缺失的功能
- 百分比指示器。
贡献
- 非常感谢 CodeProject 成员 mav.northwind 帮助我重写了
onPaint()
方法,请参阅他的帖子这里。
使用控件
将 ColorProgressBar 控件添加到 Windows 窗体工具箱
- 右键单击Windows 窗体工具箱,然后从快捷菜单中选择自定义工具箱...。将打开自定义工具箱对话框。
- 选择 .NET Framework 组件 选项卡,然后单击浏览。浏览到 ColorProgressBar\bin\Release 或 \debug 文件夹,然后选择 ColorProgressBar.dll。
ColorProgressBar
将显示在自定义工具箱对话框中的组件列表中。 - 选中
ColorProgressBar
旁边的复选框,然后选择确定。现在,ColorProgressBar
已添加到 Windows 工具箱的选项卡中。
我的其他控件项目
反馈
请为这篇文章投票。
如果您有任何问题,请 给我发电子邮件 或留下您的消息
- 错误报告
- 代码改进
- 任何评论或建议。