How to smoothly animate Windows Forms location with different speeds?

后端 未结 4 1482
走了就别回头了
走了就别回头了 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:07

    I think Windows limits the repaint rate, so it wouldn't matter if you cranked the FPS up to insane values; if you want to see higher frame rates you'll probably have to result to something like XNA/DirectX animation.

    You could use a Timer, and write an elapsed event handler that would both move & invalidate your form. To that end you wouldn't have to do the Thread.Sleep or the book-keeping with the last ticks and interval calcs, and it would happen with a regular cadence. Additionally, instead of the conditional code around the 'dir' boolean, you could negate the PX value when you want to change directions (and do additions-only instead of the ternary operator on dir); this is possible since subtraction is the same as adding a negative.

    This should make your animation easier to extend to do other things. For fun, you could also create a PY to move vertically as well. Whatever the case, I hope you have some fun with it. :)

提交回复
热议问题