所以我有一个自定义的 WF 控件,里面有一些形状和文本。这些物体往往会经常移动。由于没有像 this.DoubleBuffered = true 或 "WS_EX_COMPOSITED" 这样的工作(至少对我而言)来消除闪烁,我设法自己创建了一个非常简单的双缓冲。问题是渲染非常慢。只有在小分辨率下它才足够快。
它是这样工作的:
Graphics g;
Bitmap buffer;
public SomeControl()
{
InitializeComponent();
buffer = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(buffer);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
g.Clear(BackColor);
//some painting with "g"
e.Graphics.DrawImageUnscaled(buffer, Point.Empty);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
buffer = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(buffer);
}
void Repaint()
{
this.InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
}
protected override void OnMouseMove(MouseEventArgs e)
{
//Move something around
Repaint();
}
如果我不使用“g”而是使用“e.Graphics”来绘制它,它就足够快了(但它会闪烁)。那么,是否有更快的方法来使用 WF 控件进行双缓冲?谢谢!
(如果我的英语不好也很抱歉)