我一直在使用双缓冲面板将图像绘制到自身上,但是当我在其上移动图片框时,它会闪烁和滞后。
我用来移动图片框的代码是:
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));
}
如您所见,不可见的图片框2被绘制到双缓冲面板上。但是,当我移动 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();
}
}
这是我试图在没有任何闪烁的情况下实现的图片。(未被鼠标移动的图片框正在绘制到面板上)。我无法做到这一点而不会看到闪烁