如何在WinForms中创建无休止的progressbar?

如何在WinForms中创建无休止的progressbar?,winforms,.net-3.5,Winforms,.net 3.5,我不知道一个操作需要多长时间,我想在对话框中向用户显示一个进度条。我尝试过使用System.Windows.Forms.ProgressBar,但它似乎不支持它 我想要的一个例子是windows在internet上查找新驱动程序时显示的进度条。它只是在进度条上有三个或四个来回移动的字幕样式的“条” 我怎样才能做到这一点?只需使用动画gif:) 您可以在此处自行制作: System.Windows.Forms.ProgressBar有一个名为Style的属性。将Style设置为Marquee将达到

我不知道一个操作需要多长时间,我想在对话框中向用户显示一个进度条。我尝试过使用System.Windows.Forms.ProgressBar,但它似乎不支持它

我想要的一个例子是windows在internet上查找新驱动程序时显示的进度条。它只是在进度条上有三个或四个来回移动的字幕样式的“条”

我怎样才能做到这一点?

只需使用动画gif:)

您可以在此处自行制作:

System.Windows.Forms.ProgressBar有一个名为
Style
的属性。将
Style
设置为
Marquee
将达到您想要的效果

编辑:指出字幕仅在

Windows XP家庭版、Windows XP Professional x64版、Windows Server 2003


注释提供了更多的信息,表明只要您使用.NET 2.0或更高版本,这似乎在任何地方都有效。

您是否尝试过将
System.Windows.Forms.ProgressBar的
属性设置为
Marquee

然而,令人惊讶的是,该属性仅在以下平台上可用(根据):

Windows XP家庭版、Windows XP Professional x64版、Windows Server 2003

可能是文档尚未更新到Vista。有人知道Vista的限制吗


编辑:正如在另一条评论中所发布的,关于支持的平台,文档似乎是错误的。应该可以在Vista和Windows 7上使用。

可能有更好的方法,但其中一种方法是在完成任务时将值设置回0(假设任务未完成)

我发现Chris Lawl的解决方案最好、非常好且干净的解决方案只包括一个gif,并且没有混乱创建永无止境的进度条

这就是我的工作。我为您创建一个不确定的进度条。 将自定义控件添加到项目/窗体并插入以下代码:

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);
    }
}

似乎微软不能完全放弃“字幕”-d使其成为“Windows XP或更高版本”。我刚刚在Windows Vista和Windows 7 Beta版上进行了测试(虽然都是64位测试版),但效果不错。注意:这是一个.NET 2.0+功能。NET 1.1未公开style属性。由于问题已标记。net3.5我认为可以安全地假设提问者不会尝试在.NET 1.1中使用它。每当我看到程序这样做时,我都会感到身体疼痛。从可用性的角度来看,这是一个懒惰的程序员能做的最糟糕的事情之一。这是一个缺乏信息的、错误的、分散注意力的、恼人的、正在削弱用户对“进度条”的信任的隐喻。即使没有进度条也更好。同意。对于无法获得进度信息的长期运行流程(问题有点不清楚),最好的解决方案可能是根本没有进度条。。。但是,嘿,他问如何得到一个不确定的:)似乎我误解了预期的效果。这是我脑海中唯一能记得的无限进度条。如果进度条太长,没有哪个进度条会更糟糕(超过10-30秒取决于人的耐心和他们希望计算机的速度)。我通常考虑在没有进步的30秒后杀死一个进程。indication@Davy8:我一直喜欢一个徽标(即模拟时钟抽象)的想法,它在发生某些事情时会稍微旋转(“文件复制”事件或类似事件)。如果发生了很多事情,它会旋转得很快,如果发生的事情很少,它会旋转得很慢。如果进程停止了,它根本不会旋转。没有混乱创建永无止境的进度条?您只需在完成后将字幕动画设置为0,并将进度条的Visible属性设置为false。这不是什么神奇的事情。
        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;
    }