I would use bitmaps instead of icons, as others have said. Here is how I would do it:
First off, create a class which holds the bitmap and a delegate which will point to a method which will get the position of the bitmap as a function of time.
delegate Point PositionFunction(int time);
class MovingBitmap
{
private Bitmap bitmap;
private PositionFunction positionFunction;
public MovingBitmap(Bitmap bitmap, PositionFunction positionFunction)
{
if (bitmap == null)
{
throw new ArgumentNullException("bitmap");
}
else if (positionFunction == null)
{
throw new ArgumentNullException("bitmap");
}
this.bitmap = bitmap;
this.positionFunction = positionFunction;
}
public Bitmap Bitmap
{
get { return this.bitmap; }
}
public PositionFunction PositionFunction
{
get { return this.positionFunction; }
}
}
For the position function, you will need to decide how you want the bitmaps to move. (Note: time expressed in milliseconds.) It could be as simple as:
private Point SimpleTimeFunction(int time)
{
return new Point(time / 5, time / 5);
}
private Point ParabolaFunction(int time)
{
return new Point(time / 5, (time / 5) * (time / 5));
}
Or it could be a piecewise function composed of multiple equations, such as this:
if (time < 5000)
{
return new Point(time / 5, 2 * (time / 5));
}
else
{
return new Point(time / 5, time / 5) * time);
}
It all comes down to how you want it to move. Hopefully, you like mathematics. :)
Then, in the control which will be holding the bitmaps, add a List<MovingBitmap>
field.
private List<MovingBitmap> bitmaps = new List<MovingBitmap>();
Then you need a bitmap the size of the parent control to use as a buffer, so the user experience will be flicker-less. You will draw all the moving bitmaps on the buffer, and then in turn draw that bitmap on the control in OnPaint
.
private int startTime; // set this to System.Environment.TickCount when you start
// Add this line to the constructor
this.UpdateBufferSize();
private void UpdateBufferSize()
{
if (this.buffer != null)
{
this.buffer.Dispose();
}
this.buffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
}
private void RefreshBuffer()
{
int timeElapsed = Environment.TickCount - this.startTime;
using (Graphics g = Graphics.FromImage(this.buffer))
{
g.Clear(this.BackColor);
foreach (MovingBitmap movingBitmap in this.bitmaps)
{
Rectangle destRectangle = new Rectangle(
movingBitmap.PositionFunction(timeElapsed),
movingBitmap.Bitmap.Size);
g.DrawImage(movingBitmap.Bitmap, destRectangle);
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(this.buffer, Point.Empty);
}
To achieve the animation effect, you will need a timer. When the timer elapses, refresh the buffer and then the control.
private System.Timers.Timer timer;
private ParameterlessVoid refreshMethod;
private delegate void ParameterlessVoid();
// Add these four lines to the constructor
this.timer = new System.Timers.Timer(1000 / 20); // 20 times per second
this.timer.AutoReset = true;
this.timer.Elapsed += this.HandleTimerElapsed;
this.refreshMethod = new ParameterlessVoid(this.Refresh);
private void HandleTimerElapsed(object sender, EventArgs e)
{
this.RefreshBuffer();
this.Invoke(this.refreshMethod);
}
private void Start()
{
this.startTime = System.Environment.TickCount;
this.timer.Start();
}
I think that's basically all you need to do. It's not fully tested and debugged, but it should point you in the right direction.