Progressbar issue .net Windows 7/Vista



I noticed a rather annoying issue while working with progressbars in .net & Windows 7/Vista.

The progressbar in my application worked perfectly on Windows XP, but under Windows 7/Vista I found the bar lagging behind the actual percentages passed to the control (The bar eventually catches up).

After some googling I found that this is due to the Aero theme - apparently thanks to delays generated by the animation the theme adds to progressbars (If you disable the Areo theme, everything works fine).

I came up with the following workaround:
 
using System;
using System.Windows.Forms;
using System.Drawing;
 
class myProgressBar : ProgressBar
{
    public myProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer, true);
    }
 
    protected override void OnPaint(PaintEventArgs e)
    {
        ProgressBarRenderer.DrawHorizontalBar(e.Graphics, this.ClientRectangle);
        Rectangle bounds = new Rectangle
        {
            X = this.ClientRectangle.X,
            Y = this.ClientRectangle.Y,
            Width = (Int32)Math.Floor(((double)this.Value / this.Maximum) * this.ClientRectangle.Width),
            Height = this.ClientRectangle.Height
        };
        bounds.Inflate(-1, -1);
        ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, bounds);
    }
}
 

We simply inherit from the existing ProgressBar class and override its OnPaint method, visually the progressbar will look the same as the themed one, unfortunately this does however disable the animation effect - but at least the bar keeps up.







Post comment

Name *
Email
Title
Body *
Security Code
*
* Required fields

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51