3 回答
TA贡献1803条经验 获得超6个赞
这是最受接受的代码的无闪烁版本,您可以找到该问题的答案。所有这些都归功于这些致命答案的发布者。感谢Dusty,Chris,Matt和Josh!
就像其中一个评论中“ Fueled”的请求一样,我还需要一个表现得更...专业的版本。此代码与以前的代码一样保留样式,但是添加了屏幕外图像渲染和图形缓冲(并正确处理了图形对象)。
结果:一切正常,没有闪烁。:)
public class NewProgressBar : ProgressBar
{
public NewProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// None... Helps control the flicker.
}
protected override void OnPaint(PaintEventArgs e)
{
const int inset = 2; // A single inset value to control teh sizing of the inner rect.
using (Image offscreenImage = new Bitmap(this.Width, this.Height))
{
using (Graphics offscreen = Graphics.FromImage(offscreenImage))
{
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
if (ProgressBarRenderer.IsSupported)
ProgressBarRenderer.DrawHorizontalBar(offscreen, rect);
rect.Inflate(new Size(-inset, -inset)); // Deflate inner rect.
rect.Width = (int)(rect.Width * ((double)this.Value / this.Maximum));
if (rect.Width == 0) rect.Width = 1; // Can't draw rec with width of 0.
LinearGradientBrush brush = new LinearGradientBrush(rect, this.BackColor, this.ForeColor, LinearGradientMode.Vertical);
offscreen.FillRectangle(brush, inset, inset, rect.Width, rect.Height);
e.Graphics.DrawImage(offscreenImage, 0, 0);
offscreenImage.Dispose();
}
}
}
}
- 3 回答
- 0 关注
- 1171 浏览
添加回答
举报