.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Code Snippets >> Web Services >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Create Animated GUI

Posted By:Deepika Haridas       Posted Date: September 30, 2009    Points: 10    Category: Web Services    URL: http://www.dotnetspark.com  

Open a New Project that is Windows Application + C#

Add two three .png images in your folder.. For example i ahve taken picture of an eyea and a ball

Now Insert Form1 --> In that take two radio buttons --> Eye and ball
Also take a timer on the form

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

private System.Windows.Forms.Label Label1;
private System.Windows.Forms.RadioButton optWink;
private System.Windows.Forms.RadioButton optBall;
private System.Windows.Forms.Timer tmrAnimation;

const int WINK_TIMER_INTERVAL = 150; //' In milliseconds
protected Image[] arrImages = new Image[4];
protected int intCurrentImage = 0;
protected int j = 1;
const int BALL_TIMER_INTERVAL = 25; //' In milliseconds;
private int intBallSize = 16; //' fraction of client area;
private int intMoveSize = 4; //' fraction of ball size;
private Bitmap bitmap;
private int intBallPositionX, intBallPositionY ;
private int intBallRadiusX, intBallRadiusY, intBallMoveX, intBallMoveY,
intBallBitmapWidth, intBallBitmapHeight;
private int intBitmapWidthMargin, intBitmapHeightMargin ;
const int TEXT_TIMER_INTERVAL = 15; //' In milliseconds;
protected int intCurrentGradientShift = 10;
protected int intGradiantStep = 5;

// This subroutine handles the Load event for the Form.
private void frmMain_Load(object sender, System.EventArgs e)
// Fills the image array for the Winking Eye example.
int i;
for (i=0;i<=3;i++)
string sFileName = "..\\..\\Eye" + (i + 1).ToString() + ".png";

arrImages[i]= new Bitmap(sFileName);

// This subroutine handles the CheckChanged event for the radio buttons.
private void RadioButtons_CheckedChanged(object sender, System.EventArgs e) //optWink.CheckedChanged, optBall.CheckedChanged;
if (optWink.Checked)
tmrAnimation.Interval = WINK_TIMER_INTERVAL;
else if (optBall.Checked)
tmrAnimation.Interval = BALL_TIMER_INTERVAL;

protected void TimerOnTick(object obj ,EventArgs ea )
if (optWink.Checked)
// Obtain the Graphics object exposed by the Form.
Graphics grfx = CreateGraphics();
// Call DrawImage, using Overload #8, which takes the current image to be
// displayed, the X and Y coordinates (which, in this case centers the
// image in the client area), and the image's width and height.
Convert.ToInt32((ClientSize.Width - arrImages[intCurrentImage].Width) / 2),
Convert.ToInt32((ClientSize.Height - arrImages[intCurrentImage].Height) / 2),
arrImages[intCurrentImage].Width, arrImages[intCurrentImage].Height);
// It is always a good idea to call Dispose for objects that expose this
// method instead of waiting for the Garbage Collector to do it for you.
// This almost always increases the application's performance.
// Loop through the images.
intCurrentImage += j;

if (intCurrentImage == 3)
// This is the last image of the four, so reverse the animation
// order so that the eye closes.
j = -1;
else if (intCurrentImage == 0)
// This is the first image of the four, so reverse the animation
// order so that the eye opens again.
j = 1;
else if ( optBall.Checked)
// Obtain the Graphics object exposed by the Form.
Graphics grfx = CreateGraphics();
// Draw the bitmap containing the ball on the Form.
grfx.DrawImage(bitmap, Convert.ToInt32(intBallPositionX - intBallBitmapWidth / 2),
Convert.ToInt32(intBallPositionY - intBallBitmapHeight / 2),
intBallBitmapWidth, intBallBitmapHeight);
// Increment the ball position by the distance it has
// moved in both X and Y after being redrawn.
intBallPositionX += intBallMoveX;
intBallPositionY += intBallMoveY;
// Reverse the ball's direction when it hits a boundary.

if ((intBallPositionX + intBallRadiusX >= ClientSize.Width )
|| (intBallPositionX - intBallRadiusX <= 0))
intBallMoveX = -intBallMoveX;
// Set the Y boundary at 40 instead of 0 so the ball does not bounce
// into controls on the Form.

if ((intBallPositionY + intBallRadiusY >= ClientSize.Height)
|| (intBallPositionY - intBallRadiusY <= 40))
intBallMoveY = -intBallMoveY;

protected override void OnResize(EventArgs ea)
if (optWink.Checked)
// Obtain the Graphics object exposed by the Form and erase any drawings.
Graphics grfx = CreateGraphics();
// You could also call grfx.Clear(BackColor) or this.Invalidate() to clear
// off the screen.
else if (optBall.Checked)
// Obtain the Graphics object exposed by the Form and erase any drawings.
Graphics grfx = CreateGraphics();
// Set the radius of the ball to a fraction of the width or height
// of the client area, whichever is less.
double dblRadius = Math.Min(ClientSize.Width / grfx.DpiX,
ClientSize.Height / grfx.DpiY) / intBallSize;

// Set the width and height of the ball in most cases the DPI is
// identical in the X and Y axes.
intBallRadiusX = Convert.ToInt32(dblRadius * grfx.DpiX);
intBallRadiusY = Convert.ToInt32(dblRadius * grfx.DpiY);

// Set the distance the ball moves to 1 pixel or a fraction of the
// ball's size, whichever is greater. This means that the distance the
// ball moves each time it is drawn is proportional to its size, which
// is, in turn, proportional to the size of the client area. Thus, when
// the client area is shrunk the ball slows down, and when it is
// increased, the ball speeds up.
intBallMoveX = Convert.ToInt32(Math.Max(1, intBallRadiusX / intMoveSize));
intBallMoveY = Convert.ToInt32(Math.Max(1, intBallRadiusY / intMoveSize));

// Notice that the value of the ball's movement also serves the
// margin around the ball, which determines the size of the actual
// bitmap on which the ball is drawn. Thus, the distance the ball moves
// is exactly equal to the size of the bitmap, which permits the previous
// image of the ball to be erased before the next image is drawn, all
// without an inordinate amount of flickering.
intBitmapWidthMargin = intBallMoveX;
intBitmapHeightMargin = intBallMoveY;
// Determine the actual size of the Bitmap on which the ball is drawn by
// adding the margins to the ball's dimensions.
intBallBitmapWidth = 2 * (intBallRadiusX + intBitmapWidthMargin);
intBallBitmapHeight = 2 * (intBallRadiusY + intBitmapHeightMargin);
// Create a new bitmap, passing in the width and height
bitmap = new Bitmap(intBallBitmapWidth, intBallBitmapHeight);
// Obtain the Graphics object exposed by the Bitmap, clear the existing
// ball, and draw the new ball.
grfx = Graphics.FromImage(bitmap);
grfx.FillEllipse(Brushes.Red, new Rectangle(intBallMoveX,
intBallMoveY, 2 * intBallRadiusX, 2 * intBallRadiusY));
// Reset the ball's position to the center of the client area.
intBallPositionX = Convert.ToInt32(ClientSize.Width / 2);
intBallPositionY = Convert.ToInt32(ClientSize.Height / 2);



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend