0

我想水平移动表单中的菱形(例如每 200 毫秒 2 个像素)。我在 From_Paint 事件中使用了以下代码。

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Point p1 = new Point(5,0);
    Point p2 = new Point(10, 5);
    Point p3 = new Point(5, 10);
    Point p4 = new Point(0, 5);
    Point[] ps = { p1, p2, p3, p4, p1 };
    g.DrawLines(Pens.Black, ps);
}

我知道如何移动图片框,但如何处理形状。

谢谢,阿尼

4

2 回答 2

2

您需要在表单级别变量中跟踪您的当前位置。如果您这样做,您的 Form1_Paint 事件可以在每次绘制时更改 X 像素位置。

只需在表单中添加一个计时器,并将其间隔设置为 200 毫秒。每 200 毫秒,将 2 添加到当前 X 像素,并使控件无效(因此重绘)。


编辑:将此添加到您的表单中:

int xOffset = 0;

然后,在您的 timer_Tick 中:

private void timer1_Tick(object sender, EventArgs e)
{
    if (xOffset < 500)
        xOffset += 2;
    else
        timer1.Enabled = false; // This will make it only move 500 pixels before stopping.... Change as desired.

    this.Invalidate(); // Forces repaint
}

将您的绘画事件更改为:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Point p1 = new Point(5 + xOffset,0);
    Point p2 = new Point(10 + xOffset, 5);
    Point p3 = new Point(5 + xOffset, 10);
    Point p4 = new Point(0 + xOffset, 5);
    Point[] ps = { p1, p2, p3, p4, p1 };
    g.DrawLines(Pens.Black, ps);
}
于 2010-04-23T17:46:01.233 回答
0

Timer在每个刻度上使用然后重绘。

于 2010-04-23T17:45:03.157 回答