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
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.
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.
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. :)
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.