如何在 window 的边缘停止对象的动画移动?

How do I stop the animated movement of an object at the edge of the window?

我想在 winforms 上制作一个带有对象的动画,但是当它到达边缘时,它不会停止。它超出了表格 我该怎么办?

创建一个新项目,在其中添加一个带有图片的 pictureBox 或只是一个背景颜色,然后应用这些代码。这会让您知道如何去做。

 public Form1()
    {
        InitializeComponent();
        Timer timer = new Timer();
        timer.Interval = 12;
        timer.Tick += timer_Tick;
        timer.Start();
    }
    int difference = 5;
    void timer_Tick(object sender, EventArgs e)
    {
        if (pictureBox1.Left  < 0 || pictureBox1.Left + pictureBox1.Width > this.Width) difference = -difference;
        pictureBox1.Left += difference;
    }