C# Progressbar 在 Vista 或 Windows7 中无法准确更新

发布于 2024-08-28 12:05:52 字数 677 浏览 5 评论 0原文

public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
     if (this.progressBar1.Value >= 100)
     {
         this.timer1.Stop();
         this.timer1.Enabled = false;
     }
     else
     {
         this.progressBar1.Value += 10;
         this.label1.Text = Convert.ToString(this.progressBar1.Value);                
     }
  }
  //......
}

这里我使用了一个计时器来更新进度条值。在 XP 下运行得很好。但在Windows7或Vista中,当进度值设置为100时,但图形进度不是100!

搜索一些帖子发现其在 Vista/Windows7 中动画滞后。

如何摆脱这个东西?

我不想使用以下方法来失去 Vista/Window7 的外观和感觉:

SetWindowTheme(progressBar1.Handle, " ", " ");
public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
     if (this.progressBar1.Value >= 100)
     {
         this.timer1.Stop();
         this.timer1.Enabled = false;
     }
     else
     {
         this.progressBar1.Value += 10;
         this.label1.Text = Convert.ToString(this.progressBar1.Value);                
     }
  }
  //......
}

Here I used a timer to update the progress bar value. It works fine in XP. But in Windows7 or Vista when the progress value is set to say 100 but the graphical progress is not 100!

Searching some threads found that its for animation lag in Vista/Windows7.

How to get rid of this thing?

I don't want to loose the look and feel of Vista/Window7 using:

SetWindowTheme(progressBar1.Handle, " ", " ");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懒的傷心 2024-09-04 12:05:52

我也有同样的问题。 Fozi的提示对我很有帮助。 Samir 的解决方案将正常工作,除非达到最大值(100%)。为了使这项工作也达到 100%,之前必须增加最大值。以下对我来说效果很好。

if (NewValue < progressBar.Maximum)
{
  progressBar.Value = NewValue + 1;
  progressBar.Value--;
}
else
{
  progressBar.Maximum++;
  progressBar.Value = progressBar.Maximum;
  progressBar.Value--;
  progressBar.Maximum--;
}

I had the same problem. Fozi's tipps was helping me. The solution from Samir will work fine unless the maximum (100%). To make this work also for 100% the maximum must be increased before. The following worked fine for me.

if (NewValue < progressBar.Maximum)
{
  progressBar.Value = NewValue + 1;
  progressBar.Value--;
}
else
{
  progressBar.Maximum++;
  progressBar.Value = progressBar.Maximum;
  progressBar.Value--;
  progressBar.Maximum--;
}
戴着白色围巾的女孩 2024-09-04 12:05:52

这就是 Vista 及更高版本中愚蠢的进度条的工作原理。

没有解决办法。

向微软投诉。

This is just how the stupid progress bars work in Vista and later.

There is no fix.

Complain to Microsoft.

剪不断理还乱 2024-09-04 12:05:52
  private void timer1_Tick(object sender, EventArgs e)
    {

        if (progressBar1.Maximum == 1) progressBar1.Maximum = 100;
        if (progressBar1.Value==100) {
            progressBar1.Value = 0;
            return;
        }
        progressBar1.Increment(1);
        if (progressBar1.Value == 100) {
            progressBar1.Value = 1; progressBar1.Maximum = 1;
            progressBar1.Update();
        }
    }

这些是我通过正确绘制进度条来解决 win7 问题的技巧。

  private void timer1_Tick(object sender, EventArgs e)
    {

        if (progressBar1.Maximum == 1) progressBar1.Maximum = 100;
        if (progressBar1.Value==100) {
            progressBar1.Value = 0;
            return;
        }
        progressBar1.Increment(1);
        if (progressBar1.Value == 100) {
            progressBar1.Value = 1; progressBar1.Maximum = 1;
            progressBar1.Update();
        }
    }

These are my tricks to tackle the win7 problem with proper full scale paint of the progressbar.

请持续率性 2024-09-04 12:05:52
public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
    if (this.progressBar1.Value >= 100)
    {
     this.timer1.Stop();
     this.timer1.Enabled = false;
    }
    else
    {
      int tempValue = this.progressBar1.Value + 10;
      if (tempValue < 100 && tempValue >=0 )
      {
       this.progressBar1.Value = tempValue + 1;
       this.progressBar1.Value = tempValue;
      }
      else if (tempValue >= 100)
      {
       this.progressBar1.Value = 100;
       this.progressBar1.Value = 99;
       this.progressBar1.Value = 100;
      }
     this.label1.Text = Convert.ToString(this.progressBar1.Value);                
    }
  }

//......
}

else 部分使进度条现在看起来正常。但进度条应该有一些标准的方式。这个想法来自Fozi的评论这里

public partial class Form1 : Form
{
  //....
  private void timer1_Tick(object sender, EventArgs e)
  {
    if (this.progressBar1.Value >= 100)
    {
     this.timer1.Stop();
     this.timer1.Enabled = false;
    }
    else
    {
      int tempValue = this.progressBar1.Value + 10;
      if (tempValue < 100 && tempValue >=0 )
      {
       this.progressBar1.Value = tempValue + 1;
       this.progressBar1.Value = tempValue;
      }
      else if (tempValue >= 100)
      {
       this.progressBar1.Value = 100;
       this.progressBar1.Value = 99;
       this.progressBar1.Value = 100;
      }
     this.label1.Text = Convert.ToString(this.progressBar1.Value);                
    }
  }

//......
}

The else part makes the progress bar looks OK now. But there should have been some standard way for progress bars. The idea is from from Fozi's comment here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文