0

我有一个 PictureBox 图像,它随着面板上的鼠标移动而移动。

它按照我的意愿移动,但它一直在闪烁(如刷新),我了解到这是表单的问题。

我在表单的构造函数中尝试了以下代码行,但没有成功:

SetStyle( ControlStyles.ResizeRedraw, true );

SetStyle( ControlStyles.UserPaint, true );

SetStyle( ControlStyles.AllPaintingInWmPaint, true );   

SetStyle( ControlStyles.OptimizedDoubleBuffer, true );    

如果有助于查看所有图片,这是鼠标移动的事件处理程序。chipHolder 是一个面板,image 是分别从文件中导入的图像。

private void grid_MouseMove(object sender, MouseEventArgs e)
{ 
      columnPosition = e.X;

      if (columnPosition != -1)
      {
          if (!(columnPosition < 35 || columnPosition > 610))
          {
                chipHolder.Controls.Clear();  
                PictureBox picBox = new PictureBox();
                chipHolder.Controls.Add(picBox);
                picBox.Image = image;
                picBox.Width = image.Width;
                picBox.Height = image.Height;
                picBox.Location = new Point(columnPosition - 33, 0);
                picBox.Show();
          }
      }
      chipHolder.Update();
}

有任何想法吗?

4

2 回答 2

4

不要重新创建PictureBox,只需移动它。

刚试过这个,图像移动没有任何闪烁:

private void button1_Click(object sender, EventArgs e)
{
    for (int iter = 0; iter < 500; iter++)
    {
        pictureBox1.Location = new Point(pictureBox1.Left + 1, pictureBox1.Top + 1);
        Application.DoEvents();
        System.Threading.Thread.Sleep(30);
    }
}

对于鼠标移动:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    pictureBox1.Location = new Point(e.X, e.Y);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    pictureBox1.Location = new Point(e.X + pictureBox1.Left, e.Y + pictureBox1.Top);
}
于 2012-11-27T18:57:16.697 回答
0

伊戈尔说的:

private void grid_MouseMove(object sender, MouseEventArgs e)
{ 
      columnPosition = e.X;

      if (columnPosition != -1)
      {
          if (!(columnPosition < 35 || columnPosition > 610))
          {
                PictureBox picBox = chipHolder.Controls[0] // whatever your picbox id is;
                picBox.Location = new Point(columnPosition - 33, 0);
          }
      }
}
于 2012-11-27T19:22:07.363 回答