我在项目中有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,因为它的刷新和闪烁。
答案 0 :(得分: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();
}