简体   繁体   English

如何通过代码(WPF)绘制自定义进度栏

[英]How to paint a custom Progress Bar by code (WPF)

I created a WinForms custom progress bar, but it flickers a little. 我创建了一个WinForms自定义进度栏,但它闪烁了一点。 It's double buffered, but it still flickers a little bit, so I'm trying WPF to see if the little flickering can get away. 它具有双重缓冲,但仍会闪烁一点,因此我正在尝试使用WPF来查看闪烁是否可以消除。

I'm completely new to WPF. 我是WPF的新手。 As far as I've read, WPF's OnPaint(PaintEventArgs e) method is called OnRender(DrawingContext drawingContext). 据我所读,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);
    }
}

The problem: 问题:

在此处输入图片说明

My image is not "overriding" the green bar. 我的图像没有“覆盖”绿色条。 Now, if I change Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height); 现在,如果我更改Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height); to... let's say, Rect myRect = new Rect(0, 50, ((Width * Value) / (Maximum - Minimum)) + 5, Height); 假设... Rect myRect = new Rect(0, 50, ((Width * Value) / (Maximum - Minimum)) + 5, Height); , the result is this: ,结果是这样的:

在此处输入图片说明

So, the rainbow bar is drawn, but not over the progress bar. 因此,绘制了彩虹条,但未绘制在进度条上。 If I write Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height); 如果我写Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height); , it's drawn but UNDER the progress bar. ,它已绘制但在进度条下。 What do I do to have a rainbow progress bar (and other custom progress bars for that matter)? 要设置Rainbow进度栏(以及与此相关的其他自定义进度栏),我该怎么做?

Thank you for your help. 谢谢您的帮助。

Edit: The original progress bar (that one that flickers a little bit) has way more than the rainbow. 编辑:原始进度条(闪烁的那一点)比彩虹具有更多的功能。 I just started with the rainbow to make a quick test in WPF and then try and add the other stuff. 我刚开始使用Rainbow进行WPF快速测试,然后尝试添加其他内容。 Just in case if you're wondering why such a simple progress bar was flickering in WinForms. 以防万一,如果您想知道为什么WinForms中如此简单的进度条会闪烁。 It was because the WinForms one had more than the rainbow. 这是因为WinForms拥有的不只是彩虹。 Thank you. 谢谢。

If you need to create a ProgressBar at the start of the Window of WPF application, it is possible to do by using this code: 如果您需要在WPF应用程序Window的开始处创建一个ProgressBar ,则可以使用以下代码进行操作:

Your xaml: 您的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>

Code-Behind: 代码隐藏:

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

Where property progressBar.Foreground sets color of your ProgressBar . 其中, progressBar.Foreground属性设置ProgressBar颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM