2010-05-14 382 views
10

是否有禁用進度條動畫的可能性?禁用WinForms ProgressBar動畫

我需要它的一些暫停,並沒有運行的時刻。如果進度條閃爍,普通用戶會認爲該進程正在運行。

創建自己的進度條控件的建議不是我正在尋找的。

+1

你說的是Vista的圖形效果嗎?一個帳篷酒吧? – SLaks 2010-05-14 14:13:14

+0

是的。 XP,Vista和W7的影響。 – 2010-05-14 14:14:19

+0

這可能是相關的https://social.msdn.microsoft.com/Forums/vstudio/en-US/d223204c-24ab-4ca5-bb45-1400e3af2922/is-there-a-progress-bar-alternative-available-from -uuget-for-windows-7-that-sets-it-value-without?forum = csharpgeneral和http://stackoverflow.com/questions/37625930/cannot-get-progressbar-animation-with-code-progresschanged-not - 叫 – barlop 2016-06-04 22:36:21

回答

18

可以使用Vista的進度條的暫停狀態,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); 
+1

這就是我一直在尋找的。謝謝。該項目是偉大的:http://windowsformsaero.codeplex.com/ – 2010-05-15 06:33:09

+0

只是自己碰到這個相同的問題。其中一個問題是:這個解決方案是否也適用於XP上運行的軟件,還是僅僅是Vista/Win7? – Nailuj 2010-05-27 15:24:47

+0

@Nailuj:它不能在XP上工作 – SLaks 2010-05-27 15:38:40

2

向用戶傳達動作暫停或無法準確測量的標準方法是使用選取框顯示樣式。

progressBar1.Style = ProgressBarStyle.Marquee; 

這種風格忽略了MaximumValue性能和顯示進度條「段」,不斷跨越的進度條移動和周圍循環(它不填充進度條,它的動作是什麼樣子節橫跨控件並重新開始。)

+0

謝謝。但我需要MAximum和Value。用戶需要查看進程暫停的位置。 – 2010-05-14 14:32:27

+0

@Vasily:這是唯一的與操作系統無關的過程;如果你保證Vista或7,那麼SLak的答案應該可以解決你的問題。 – 2010-05-14 15:06:58

0

您要做的就是在此控件上專門設置樣式以覆蓋主題更改。這article給你一些信息。

+0

http://www.codeproject.com/KB/cpp/ExtendedProgressbar.aspx看起來不錯。但我懷疑我的老闆會批准非標準的外觀控制。 – 2010-05-14 14:35:49

-1

您可以覆蓋進度的OnPaint()方法。您實際上並不需要重寫整個事情,只需繼承進度條並像這樣覆蓋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); 
     } 
    } 
+0

你誤解了'e.ClipRectangle';這將無法正確呈現。另外,你需要一個Vista風格的酒吧。調用'ProgressBarRenderer.DrawHorizo​​ntalChunks'會使它稍好一些。 – SLaks 2010-05-14 15:20:43

+0

'e.ClipRectangle'是需要重新繪製的區域。你應該使用'this.ClientRectangle'。 – SLaks 2010-05-14 15:21:23

+0

正如我在我的問題中所說的:「建立自己的進度條控制的建議不是我要找的。」 – 2010-05-15 06:37:49

-1

將此代碼粘貼到代碼中。這將重寫進度條,它也可以通過顏色進行自定義。

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

順便說一句,它的工作。如果有人把這個數字計算在內,這意味着他太便宜,無法親自嘗試。 – ad48 2016-12-27 21:32:56

+0

我覺得你,只是倒下而已,甚至沒有說出原因。儘管在使用.Net 4.5時代碼存在很多問題:類需要是部分的,MyBrush不能用非靜態值初始化,最後也沒有任何內容被繪製。我錯了什麼? – 2017-05-12 23:20:15

+0

由於int轉換,寬度*(_value/Maximum)始終爲零。 – 2017-05-12 23:28:11

0

我的解決方法是使用面板控件而不是ProgressBar。我改變了BackColor,BorderStyle(到Fixed3D),我操縱它的Width來顯示所需的進度。我假設100%的進度等於表格的寬度。

Panel as a ProgressBar