GDI+下的闪烁问题

画多个点运动的时候,窗体会闪烁,查了一下网上的文章,说是OptimizedDoubleBuffer可以减少闪烁。但是我添加了效果更差,整个窗口经常白色的。搜了所有文章,基本上没有有用的。

后来经过多次试验,才了解了Graphics gc需要用e.Graphics获得,而不能用this.CreateGraphics获得。

代码如下:

private void initToolStripMenuItem_Click(object sender, EventArgs e)
{
Init();
this.Invalidate();

}

protected override void OnPaint(PaintEventArgs e)
{
Graphics gc = e.Graphics;
gc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
gc.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
gc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);

base.OnPaint(e);

}

Leave a Comment

Your email address will not be published. Required fields are marked *