【发布时间】:2010-10-12 18:23:14
【问题描述】:
我不知道一个操作需要多长时间,我想在对话框中向用户显示一个进度条。我试过使用 System.Windows.Forms.ProgressBar 但它似乎不支持它。
我想要的一个示例是 Windows 在 Internet 上寻找新驱动程序时向您显示的进度条。进度条上只有三四个“条”来回选取框样式。
我该怎么做?
【问题讨论】:
我不知道一个操作需要多长时间,我想在对话框中向用户显示一个进度条。我试过使用 System.Windows.Forms.ProgressBar 但它似乎不支持它。
我想要的一个示例是 Windows 在 Internet 上寻找新驱动程序时向您显示的进度条。进度条上只有三四个“条”来回选取框样式。
我该怎么做?
【问题讨论】:
【讨论】:
您是否尝试将System.Windows.Forms.ProgressBar 的@987654321@ 属性设置为Marquee?
然而,令人惊讶的是,该属性仅在以下平台上可用(根据MSDN):
Windows XP 家庭版、Windows XP 专业 x64 版、Windows Server 2003
可能是文档尚未更新到 Vista。有人知道 Vista 的限制吗?
编辑:正如在另一条评论中发布的那样,关于支持的平台,文档似乎是错误的。应该可以在 Vista 和 Windows 7 上运行。
【讨论】:
只需使用动画 gif :)
您可以在这里制作自己的: http://www.ajaxload.info/
【讨论】:
我发现 Chris Lawl 的解决方案是最好的、非常好的和干净的解决方案,只需包含一个 gif http://www.ajaxload.info/ 并且不会创建永无止境的进度条。
【讨论】:
这对我有用。我为您创建了一个不确定的进度条。 将自定义控件添加到您的项目/表单并插入此代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace AnimatedCustomControls
{
sealed class IndeterminateProgressbar : Control
{
private readonly List<int> positions = new List<int>();
private readonly Timer tmrAnimation = new Timer {Interval = 5, Enabled = false};
private readonly Timer tmrAddPosition = new Timer {Interval = 500, Enabled = true};
public Color ProgressColor { get; set; }
public Color InactiveColor { get; set; }
public IndeterminateProgressbar()
{
DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
ProgressColor = Color.FromArgb(40, 190, 245);
InactiveColor = Color.FromArgb(40, 40, 40);
tmrAnimation.Tick += tmrAnimation_Tick;
tmrAddPosition.Tick += tmrAddPosition_Tick;
if (!DesignMode) tmrAnimation.Start();
}
void tmrAddPosition_Tick(object sender, EventArgs e)
{
positions.Add(1);
}
void tmrAnimation_Tick(object sender, EventArgs e)
{
if (DesignMode) tmrAnimation.Stop();
for (int i = 0; i < positions.Count; i++)
{
positions[i] += 2 + Math.Abs(positions[i]) / 50;
if (positions[i] > Width) positions.RemoveAt(i);
}
Invalidate();
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
if (Enabled)
{
positions.Clear();
positions.AddRange(new[] { Width / 10, Width / 3, Width / 2, (int)(Width * 0.7) });
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (Enabled)
{
e.Graphics.Clear(BackColor);
foreach (int i in positions)
{
e.Graphics.DrawLine(new Pen(Brushes.Black, 4f), i, 0, i, Height);
}
}
else e.Graphics.Clear(InactiveColor);
base.OnPaint(e);
}
}
}
然后您应该构建您的解决方案,当您返回设计器时,新控件应该在您的工具箱中。将其拖入表单中,设置最大值和最小值即可。
我创建了一个示例程序,让您了解它的使用方法:
private void Form1_Load(object sender, EventArgs e)
{
indeterminateProgressbar1.BackColor = Color.FromArgb(40, 190, 245); //it's an nice color ;)
indeterminateProgressbar1.Size = new Size(400, 4); //make it small in the height looks better
indeterminateProgressbar1.Visible = true;
}
【讨论】:
可能有更好的方法,但一种方法是在结束时将 Value 设置回 0(假设您的任务未完成)
【讨论】: