【发布时间】:2015-11-25 04:15:01
【问题描述】:
我创建了一个 WinForms 自定义进度条,但它有点闪烁。它是双缓冲的,但它仍然会闪烁一点,所以我正在尝试 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);
}
}
问题:
我的图像没有“覆盖”绿色条。
现在,如果我将Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height); 更改为...比如说Rect myRect = new Rect(0, 50, ((Width * Value) / (Maximum - Minimum)) + 5, Height);,结果是这样的:
所以,彩虹条被绘制了,但没有超过进度条。如果我写Rect myRect = new Rect(0, 0, ((Width * Value) / (Maximum - Minimum)) + 5, Height);,它会被绘制但在进度条下方。我该怎么做才能拥有彩虹进度条(以及其他自定义进度条)?
感谢您的帮助。
编辑: 原来的进度条(闪烁的那个)远不止彩虹。我刚从 Rainbow 开始在 WPF 中进行快速测试,然后尝试添加其他内容。以防万一您想知道为什么这么简单的进度条会在 WinForms 中闪烁。这是因为 WinForms 不仅仅是彩虹。谢谢。
【问题讨论】:
标签: c# wpf custom-controls onrender