3 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個贊
這是最受接受的代碼的無閃爍版本,您可以找到該問題的答案。所有這些都?xì)w功于這些致命答案的發(fā)布者。感謝Dusty,Chris,Matt和Josh!
就像其中一個評論中“ Fueled”的請求一樣,我還需要一個表現(xiàn)得更...專業(yè)的版本。此代碼與以前的代碼一樣保留樣式,但是添加了屏幕外圖像渲染和圖形緩沖(并正確處理了圖形對象)。
結(jié)果:一切正常,沒有閃爍。:)
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 關(guān)注
- 1216 瀏覽
添加回答
舉報(bào)