Disabling .NET progressbar animation when changing value?

后端 未结 5 1340
灰色年华
灰色年华 2020-11-27 05:56

I realize there are other questions on SO regarding animations and progressbars, but they seem to revolve around getting rid of the animation drawn on top of the progress ba

5条回答
  •  时光取名叫无心
    2020-11-27 05:57

    Here is my extension method, based on David Heffernan's recommendation:

    Wrap it up, hide it from view, and pretend it's not there!

    public static class ExtensionMethods
    {
        /// 
        /// Sets the progress bar value, without using Windows Aero animation
        /// 
        public static void SetProgressNoAnimation(this ProgressBar pb, int value)
        {
            // Don't redraw if nothing is changing.
            if (value == pb.Value)
                return;
    
            // To get around this animation, we need to move the progress bar backwards.
            if (value == pb.Maximum) {
                // Special case (can't set value > Maximum).
                pb.Value = value;           // Set the value
                pb.Value = value - 1;       // Move it backwards
            }
            else {
                pb.Value = value + 1;       // Move past
            }
            pb.Value = value;               // Move to correct value
        }
    }
    

提交回复
热议问题