C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Application windows, menus & toolbars »

Creating an Animated GUI


Posted Date: 12 Nov 2008    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 10



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;
}
OnResize(EventArgs.Empty);
}

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.
grfx.DrawImage(arrImages[intCurrentImage],
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.
grfx.Dispose();
// 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);
grfx.Dispose();
// 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;
Microsoft.VisualBasic.Interaction.Beep();
}
// 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;
Microsoft.VisualBasic.Interaction.Beep();
}
}
}

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.
this.Refresh();
grfx.Dispose();
}
else if (optBall.Checked)
{
// Obtain the Graphics object exposed by the Form and erase any drawings.
Graphics grfx = CreateGraphics();
grfx.Clear(BackColor);
// 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);
grfx.Dispose();

// 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.Clear(BackColor);
grfx.FillEllipse(Brushes.Red, new Rectangle(intBallMoveX,
intBallMoveY, 2 * intBallRadiusX, 2 * intBallRadiusY));
grfx.Dispose();
// Reset the ball's position to the center of the client area.
intBallPositionX = Convert.ToInt32(ClientSize.Width / 2);
intBallPositionY = Convert.ToInt32(ClientSize.Height / 2);

}
}


Do changes as per your requirement
If anyone wants then i'll provide you with my Sample Application..





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
WinForms  .  Animation  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Device Application - Single instance of the EXE
Previous Resource: ADO Example
Return to Discussion Resource Index
Post New Resource
Category: Application windows, menus & toolbars


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use