代码之家  ›  专栏  ›  技术社区  ›  Malfist

如何在WinForms中创建无休止的进度条?

  •  10
  • Malfist  · 技术社区  · 16 年前

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

    我想要的一个例子是Windows在Internet上查找新驱动程序时显示的进度条。它只是在进度条上有三到四个“栏”来回切换。

    我该怎么做?

    6 回复  |  直到 11 年前
        1
  •  24
  •   Community CDub    8 年前

    System.Windows.Forms.ProgressBar有一个名为 Style . 设置 风格 Marquee 会达到你想要的效果。

    编辑: Divo 指出天棚 Style 仅在上可用

    Windows XP家庭版、Windows XP专业版x64版、Windows Server 2003

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

        2
  •  7
  •   Dirk Vollmar    16 年前

    你试过设置 Style 性质 System.Windows.Forms.ProgressBar Marquee ?

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

    Windows XP家庭版、Windows XP专业版x64版、Windows Server 2003

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

    编辑:正如在另一条评论中发布的那样,文档在支持的平台方面似乎是错误的。应该在Vista和Windows7上工作。

        3
  •  6
  •   Chris Lawlor    16 年前

    只需使用动画gif:)

    您可以在这里制作自己的: http://www.ajaxload.info/

        4
  •  1
  •   NightlyHakr    14 年前

    我发现克里斯·劳尔的解决方案是最好的,非常好的,干净的解决方案,只包括一个GIF http://www.ajaxload.info/ 没有乱七八糟的创造永不结束的进度条。

        5
  •  1
  •   Colyn Jonker    11 年前

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

    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;
        }
    
        6
  •  0
  •   Davy8    16 年前

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