Disable WinForms ProgressBar animation

后端 未结 6 1795
温柔的废话
温柔的废话 2020-12-03 22:03

Is there a possbility to disable animation of the progress bar?

I need it for some pocess which is paused and not running at the moment. An average user would think

相关标签:
6条回答
  • 2020-12-03 22:08

    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.

    0 讨论(0)
  • 2020-12-03 22:09

    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 讨论(0)
  • 2020-12-03 22:15

    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);
    
    0 讨论(0)
  • 2020-12-03 22:22

    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.)

    0 讨论(0)
  • 2020-12-03 22:22

    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);
            }
        }
    
    0 讨论(0)
  • 2020-12-03 22:26

    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.

    0 讨论(0)
提交回复
热议问题