禁用WinForms ProgressBar动画

是否有可能禁用进度条的动画?

我需要它暂停一些暂停但暂不运行的pocess。 如果进度条正在闪烁,普通用户会认为进程正在运行。

创建自己的进度条控件的建议不是我想要的。

您可以使用Vista进度条的暂停状态, 如下所示 :

// 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); 

向用户传达动作暂停或无法准确测量的标准方法是使用选框显示样式。

 progressBar1.Style = ProgressBarStyle.Marquee; 

此样式忽略“ Maximum和“ Value属性,并显示一个进度条“段”,该段继续在进度条上移动并循环(它不会填充进度条,它会移动看起来像条的一部分一直到控制和再次开始。)

我的解决方法是使用Panel Control而不是ProgressBar。 我改变了BackColor,BorderStyle(到Fixed3D),我操纵它的宽度来显示所需的进度水平。 我假设100%的进度等于表格的宽度。

面板作为ProgressBar

您要做的是专门设置此控件上的样式以覆盖主题更改。 本文为您提供了一些信息。

您可以覆盖进度条的OnPaint()。 你实际上并不需要重写整个事情,你只需要inheritance进度条并覆盖OnPaint,如下所示:

 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); } } 

将其粘贴到代码中。 这将重写进度条,它也可以自定义颜色。

 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)); } }