1

我在项目中有 pictureBox 控件,并希望使用以下代码移动 Rectangular2:

 int pointx1 = 140, pointx2 = 160, pointx3 = 160, pointx4 = 140, pointy1 = 180, pointy2 = 180, pointy3 = 240, pointy4 = 240;

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox1.Refresh();
        Graphics graphic = pictureBox1.CreateGraphics();
        graphic.FillPolygon(new SolidBrush(Color.Black), new PointF[]{ new Point(100, 200), new Point(200, 200), new Point(200, 220), new Point(100, 220) });
        //Rectangular2 
        graphic.FillPolygon(new SolidBrush(Color.Blue), new PointF[] { new Point(pointx1++, pointy1++), new Point(pointx2++, pointy2++), new Point(pointx3++, pointy3++), new Point(pointx4++, pointy4++) });
    }

但我想使用位图绘制和移动 Rectangular2,因为它的刷新和闪烁。

4

1 回答 1

1

CreateGraphics 是您的敌人。使用已经双缓冲的 PictureBox 的 Paint 事件来进行绘图:

pictureBox1.Paint += pictureBox1_Paint;

void pictureBox1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.Clear(Color.White);
  e.Graphics.FillPolygon(new SolidBrush(Color.Black), 
                         new PointF[] { new Point(100, 200), 
                                        new Point(200, 200),
                                        new Point(200, 220),
                                        new Point(100, 220) });
  e.Graphics.FillPolygon(new SolidBrush(Color.Blue),
                         new PointF[] { new Point(pointx1, pointy1),
                                        new Point(pointx2, pointy2),
                                        new Point(pointx3, pointy3),
                                        new Point(pointx4, pointy4) });
}

然后使用您的计时器更新您的坐标并使您的控制无效:

private void timer1_Tick(object sender, EventArgs e) {
  pointx1++;
  pointy1++;
  pointx2++;
  pointy2++;
  pointx3++;
  pointy3++;
  pointx4++;
  pointy4++;
  pictureBox1.Invalidate();
}
于 2013-11-12T22:33:44.980 回答