开发者

Disable WinForms ProgressBar animation

Is there a possbility to disable animation of th开发者_Go百科e progress bar?

I need it for some pocess which is paused and not running at the moment. An average user would think the process is running if the progress bar is being blinking.

The advice to create own progress bar control is not what I'm looking for.


You can use the Vista progress bar's paused state, like this:

// Assuming a Form1 with 3 ProgressBar controls
private void Form1_Load(object sender, EventArgs e)
{
  SendMessage(progressBar2.Handle,
    0x400 + 16, //WM_USER + PBM_SETSTATE
    0x0003, //PBST_PAUSED
    0);

  SendMessage(progressBar3.Handle,
    0x400 + 16, //WM_USER + PBM_SETSTATE
    0x0002, //PBST_ERROR
    0);
}

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern uint SendMessage(IntPtr hWnd,
  uint Msg,
  uint wParam,
  uint lParam);


My workaround was to use a Panel Control instead of ProgressBar. I changed BackColor, BorderStyle (to Fixed3D) and I manipulate its Width to display needed level of progress. I assumed that 100% of progress is equal to Width of the Form.

Disable WinForms ProgressBar animation


The standard means of communicating to a user that an action is either paused or can't be accurately measured is to use the marquee display style.

progressBar1.Style = ProgressBarStyle.Marquee;

This style ignores the Maximum and Value properties and displays a progress bar "segment" that continually moves across the progress bar and loops around (it doesn't fill the progress bar, it moves what looks like a section of the bar all the way across the control and around to the beginning again.)


What you will want to do is set the style on this control specifically to override the theme changes. This article gives you a bit of information.


You could override the OnPaint() of the progressbar. You don't actually need to rewrite the whole thing, you just have to inherit the progressbar and override OnPaint like this:

public class my_progress_bar : ProgressBar {
        public Brush brush;
        public my_progress_bar() {
            this.SetStyle(ControlStyles.UserPaint, true);
            brush = Brushes.ForestGreen;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Rectangle rectangle = e.ClipRectangle;
            rectangle.Width = (int)(rectangle.Width * ((double)Value / Maximum)) - 4;
            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
            rectangle.Height = Height - 4;
            e.Graphics.FillRectangle(brush, 2, 2, rectangle.Width, rectangle.Height);
        }
    }


Paste this as to code. This will rewrite the progress bar, it's also customizable to color.

public class CProgressBar : ProgressBar
{

public Color MyColor {
    get { return _color; }
    set {
        _color = value;
        MyBrush = new SolidBrush(_color);
        Invalidate();
    }
}

private Color _color = Color.Green;

public CProgressBar()
{
    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
}

public int Value {
    get { return _value; }
    set {
        _value = value;
        Invalidate();
    }
}

private int _value;

private SolidBrush MyBrush = new SolidBrush(_color);

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.FillRectangle(MyBrush, new Rectangle(0, 0, Width * (_value / Maximum), Height));
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜