C# Progressbar is not updated accurately in Vista

2019-05-12 22:26发布

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, " ", " ");

4条回答
时光不老,我们不散
2楼-- · 2019-05-12 22:54
  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.

查看更多
唯我独甜
3楼-- · 2019-05-12 22:59

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--;
}
查看更多
爷、活的狠高调
4楼-- · 2019-05-12 23:00
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

查看更多
看我几分像从前
5楼-- · 2019-05-12 23:06

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

There is no fix.

Complain to Microsoft.

查看更多
登录 后发表回答