Move images in C#

2020-02-12 21:35发布

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form.

I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image.

How I can show image smoothly transition to the final location?

Here is my code so far:

public partial class Form1 : Form
{
    private int counter = 0;

    void timer_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter == 1)
        {
            pictureBox1.Show();
            timer1.Stop();
            counter = 0;
        }
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        while(i<=100){

             int x = pictureBox1.Location.X;
             int y = pictureBox1.Location.Y;

             pictureBox1.Location = new Point(x+25, y);
             timer1.Start();
        }
     }
}

1条回答
Ridiculous、
2楼-- · 2020-02-12 22:12

Does this work? Sorry, I can't test it where I am right now (on netbook without VS).

public partial class Form1 : Form
{
    void timer_Tick(object sender, EventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        pictureBox1.Location = new Point(x+25, y);

        if (x > this.Width)
            timer1.Stop();
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Show();
        timer1.Start();
     }
}
查看更多
登录 后发表回答