How to smoothly animate Windows Forms location with different speeds?

后端 未结 4 1481
走了就别回头了
走了就别回头了 2021-01-26 00:31

I\'ve been trying to smoothly animate some Windows Form location but I\'m having some issues if I want the speed to be variable. In other words, if I want to allow the user to s

4条回答
  •  伪装坚强ぢ
    2021-01-26 01:04

    Try this implementation of my prior suggestion (without accounting for FPS):

    public partial class Form1 : Form
    {
        private bool go;
        private int dx = 1;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (go)
            {
                this.Location = new Point(this.Location.X + dx, this.Location.Y);
                if (Location.X < 10 || Location.X > 1200)
                {
                    go = false;
                    dx = -dx;
                }
                else
                {
                    this.Invalidate();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            go = true;
            this.Invalidate();
        }
    }
    

    I think you will likely have to go outside of winforms to get higher FPS.

提交回复
热议问题