2015-11-25 50 views
1

我創建了一個WinForms自定義進度欄,但閃爍了一下。它是雙緩衝的,但它仍然有點閃爍,所以我正在嘗試WPF,看看這個小閃爍是否可以消失。如何通過代碼繪製自定義進度條(WPF)

我完全不熟悉WPF。據我所知,WPF的OnPaint(PaintEventArgs e)方法被稱爲OnRender(DrawingContext drawingContext)。

System.Drawing.Image BMP = System.Drawing.Image.FromFile(MyImagePath); 
BMP = new Bitmap(BMP, (int)Width, (int)Height); 

// Working method I founded in this site for converting a System.Drawing.Bitmap to a BitmapSource 
ImageSource Rainbow = CreateBitmapSourceFromGdiBitmap((Bitmap)BMP); 

// Working method I founded in this site for converting a System.Drawing.Bitmap to System.Windows.Media.Brush 
System.Windows.Media.Brush RainbowBrush = CreateBrushFromBitmap((Bitmap)BMP); 

protected override void OnRender(DrawingContext DrawingContext) 
{ 
    if (Value > 0) 
    { 
     Rect myRect = new Rect(0, 0, ((Width * Value)/(Maximum - Minimum)) + 5, Height); 
     DrawingContext.DrawRectangle(RainbowBrush, new System.Windows.Media.Pen(), myRect); 
    } 
} 

問題:

enter image description here

我的形象不是 「壓倒一切」 的綠色條。 現在,如果我改變Rect myRect = new Rect(0, 0, ((Width * Value)/(Maximum - Minimum)) + 5, Height);到......讓我們說,Rect myRect = new Rect(0, 50, ((Width * Value)/(Maximum - Minimum)) + 5, Height);,結果是這樣的:

enter image description here

所以,彩虹條繪製,但不是在進度條。如果我寫Rect myRect = new Rect(0, 0, ((Width * Value)/(Maximum - Minimum)) + 5, Height);,它被繪製,但在進度條下。我該如何做一個彩虹進度條(以及其他自定義進度條)?

謝謝你的幫助。

編輯:最初的進度條(閃爍一點的)比彩虹的方式更多。我剛開始用彩虹在WPF中做一個快速測試,然後嘗試添加其他東西。以防萬一,如果你想知道爲什麼這樣一個簡單的進度條在WinForms中閃爍。這是因爲WinForms比彩虹更多。謝謝。

回答

0

如果您需要在WPF應用Window開始創建ProgressBar,可以通過使用此代碼做:

XAML中:

<Window x:Class="PasswordBoxMVVM.MainWindow" 
    <!--The code omitted for the brevity--> 
    Title="MainWindow" Height="350" Width="525">  
    <StackPanel x:Name="stackPanel"> 
     <TextBox x:Name="textBox" 
     <DataGrid /> 
    </StackPanel> 
</Window> 

代碼 - 背後:

public MainWindow() 
{ 
    InitializeComponent();  
    PaintProgressBar(); 
} 

private void PaintProgressBar() 
{    
    ProgressBar progressBar = new ProgressBar(); 
    progressBar.IsIndeterminate = true; 
    progressBar.Margin = new Thickness(10, 0, 10, 10); 
    progressBar.Visibility = Visibility.Visible; 
    progressBar.Height = 25; 
    //progressBar.FlowDirection = FlowDirection.LeftToRight; 
    progressBar.Foreground = System.Windows.Media.Brushes.Green; 
    progressBar.Background = System.Windows.Media.Brushes.Red; 
    progressBar.Value = 50; 
    stackPanel.Children.Add(progressBar); 
} 

哪裏物業progressBar.Foreground設置您的ProgressBar的顏色。

相關問題