How to smoothly animate Windows Forms location with different speeds?

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

    If you got the results you want with a FPS value of 300 then I would go with that. The concept of Frames Per Second for WinForms is not the same as graphic-intensive video games, so a high value won't be a problem. The FPS value here simply adjusts how often the code is executed.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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. :)

    0 讨论(0)
  • 2021-01-26 01:26

    Super Over-Kill Solution

    Summary: make an animation of your window moving, and play it back (full-screen, perhaps)

    Details:
    Suppose your window is 100x100, and is @0,0
    Take a screen-shot of the, well, screen : (0,0)-(200,200).
    Use the screen-shot to create a clip, that depicts moving your window from 0,0 to 100,100
    Then create a borderless window (0,0)-(200,200), and put a video-player on it, and replay the animation of your window moving inside.

    Needless to say your window will be static. But you can get the best animation there is. You can even add some effects to your window moving, like bullets in the matrix, or Necromunga ships.

    0 讨论(0)
提交回复
热议问题