在C#中移动图像

3

我想在WinForms的pictureBox控件中加载一张小图片,然后使它动起来移动到窗体的另一边。

我已经加载了图片并使用了一个计时器来移动图片,但是当我运行它时,应用程序只显示pictureBox及其图像的最终位置。

我该如何让图片平滑地过渡到最终位置呢?

以下是我的代码:

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个回答

3

这个可行吗?很抱歉,我现在所在的地方没有安装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();
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接