Drawing and Moving Shapes in Windows Forms
Learning how to move an object in C# is one of the first steps toward understanding animation and graphics programming. In this beginner-friendly tutorial, you'll learn how to create simple C# windows form animation using Windows Forms, the Paint event, and a Timer.
This step-by-step guide demonstrates how to animate an object in C# Windows Forms by updating its position and redrawing it smoothly across the screen.
Understanding Object Movement in C# Windows Forms
To create C# object movement on a canvas, you need three key components:
- The Graphics class in C# (used for drawing shapes)
- The Paint event (to render the object)
- A Timer (to update the object's position repeatedly)
By combining these elements, you can create smooth movement animation on a form.
Drawing a Shape on the C# Windows Form
We begin by creating a circle (our object) using the FillEllipse method from the Graphics class.
This forms the foundation for object movement in C#.
How to Animate an Object in C#
To animate the object, we repeatedly redraw it in new positions while clearing the previous frame. This creates the illusion of continuous motion - the basis of C# windows form animation.
Steps to Animate a Circle in C#
- Use
Form1Paintclass handles the button click event. - Use loops or timers to gradually change the object's coordinates.
Create a new C# Windows Forms Application
project
;
call it Dymetric_CS.
Create 2 new C# classes;
Call them Dymetric and MovingBody.
Type out the adjoining C# code for animating an image body across the windows form.
Important: To generate the Form1_Paint C# code stub,
select the form in design mode; go to its properties; click on the
symbol and select Paint.
Note: The C# code for our Form1 Class was too big to be squeezed in with the others.
It is simply included as a link of its own.
How the C# Move Object Animation Code Works
The MovingBody C# class handles the actual drawing and movement. It:
- Draws a circle with
FillEllipse. - Uses
ClearfromGraphics.FromImageto erase old drawings. - Updates the $x$ coordinate with a loop.
- Uses
Thread.sleep()to control speed.
This demonstrates a simple but effective C# animation technique.
Why Learn C# Object Animation?
Understanding how to animate an object in C# helps you:
- Build educational simulations
- Create simple games
- Understand event-driven programming
- Master the Graphics class
- Improve problem-solving skills
For students in senior secondary or beginner programming courses, this tutorial provides a clear introduction to C# beginner graphics programming.
Key Takeaways on C# Windows Form and Object Animation
Animating graphics in C# is one of the most exciting ways to understand how programming logic connects with visual design. In this tutorial, you've learnt how to move an object across a canvas in C# using a simple, beginner-friendly code example. We've used Windows Forms and the Paint event to show how to draw, clear, and redraw shapes smoothly - the foundation of any basic C# animation tutorial.
Moving shapes or objects in C# helps students visualize core programming concepts like loops, coordinates, and event handling. This guide demonstrates how to control speed, direction, and timing to create a natural movement effect. By following each step carefully, you'll be able to animate shapes in C#, such as circles or rectangles, and make them glide across the screen with precision.
Whether you're a secondary school student, a beginner developer, or simply curious about graphics animation in C#, this tutorial will help you build confidence in creating visual applications. By the end, you would have understood how object movement in Windows Forms works and how to extend the technique for games, data visualizations, or creative projects.
Summary: Moving an Object in C#
In this tutorial, you learned how to:
- Move an object across a canvas in C#
- Use the Paint event for drawing
- Use a Timer for animation
- Update coordinates to create smooth movement
By combining event handling in C# with simple coordinate updates, you can create effective animation with just a few lines of code.
So! C# Fun Practice Exercise - Animate Circle
As a fun practice exercise, try experimenting with different shapes, speeds, and directions to deepen your understanding of object movement in C# graphics.
C# Moving Body Window Display Code Stub
C# Moving Body Code for Dymetric Class
namespace Dymetric
{
class Dymetric
{
private MovingBody move_body;
private bool do_simulation;
public Dymetric(int width, int height)
{
move_body = new MovingBody(width, height);
do_simulation = false;
}
// decide what course of action to take
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
// do animation
move_body.inPlay(e);
do_simulation = false;
}
else
{
// Put ball on screen
move_body.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# Animation Code for Moving Body Class
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class MovingBody
{
private int x, y, previous_x, previous_y;
private const int ballDIAMETER = 80;
// we'll be drawing to and from a bitmap image
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush ball_colour, bg_colour;
public MovingBody(int screen_width, int screen_height)
{
ball_colour = new SolidBrush(Color.Yellow);
bg_colour = new SolidBrush(Color.LightGray);
// Create bitmap image
offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// point graphic object to bitmap image
offscreen_g = Graphics.FromImage(offscreen_bitmap);
// Set background of bitmap graphic
offscreen_g.Clear(Color.LightGray);
previous_x = x = 10;
previous_y = y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
// draw first appearance of ball on the screen
public void clearAndDraw(PaintEventArgs e)
{
/*
* draw to offscreen bitmap
*/
// clear previous ball
offscreen_g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER);
// draw ball
offscreen_g.FillEllipse(ball_colour, x, y, ballDIAMETER, ballDIAMETER);
previous_x = x;
previous_y = y;
// draw to screen
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
// repetitively clear and draw ball on the screen - Simulate motion
public void inPlay(PaintEventArgs e)
{
// condition for continuing motion
while (x < offscreen_bitmap.Width - ballDIAMETER)
{
// redraw ball
clearAndDraw(e);
x += 10;
// take a time pause
Thread.Sleep(50);
}
x = 10;
y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
}
}