我一直在使用 doublebuffered 面板将图像绘制到自身上,但是当我将图片框移到它上面时,它会闪烁并且滞后。
我用来移动pictureBoxes的代码是:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
panel1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += (e.X - x);
pictureBox1.Top += (e.Y - y);
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));
}
正如您所见,无法看到的pictureBox2被绘制到双缓冲面板。当我移动pictureBox1时,它会在整个面板上闪烁。是的,我一直在使用 Invalidate()面板
我使用的双缓冲面板类代码是:
public class DoubleBufferPanel : Panel
{
public DoubleBufferPanel()
{
// Set the value of the double-buffering style bits to true.
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
}
}
这是我想要实现的没有任何闪烁的图片。(没有被鼠标移动的图片框被绘制到面板上)。没有看到闪烁,我无法做到这一点