Create animated GUI

Bubbly
Posted by Bubbly under C# category on | Views : 2880
Open a New Project that is Windows Application + C#

Add two three .png images in your folder.. For example i have 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)
{
Graphics grfx = CreateGraphics();
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);
grfx.Dispose();
intCurrentImage += j;
if (intCurrentImage == 3)
{
j = -1;
}
else if (intCurrentImage == 0)
{
j = 1;
}
}
else if ( optBall.Checked)
{
Graphics grfx = CreateGraphics();
grfx.DrawImage(bitmap, Convert.ToInt32(intBallPositionX - intBallBitmapWidth / 2),
Convert.ToInt32(intBallPositionY - intBallBitmapHeight / 2),
intBallBitmapWidth, intBallBitmapHeight);
grfx.Dispose();
intBallPositionX += intBallMoveX;
intBallPositionY += intBallMoveY;
if ((intBallPositionX + intBallRadiusX >= ClientSize.Width )
|| (intBallPositionX - intBallRadiusX <= 0))
{
intBallMoveX = -intBallMoveX;
Microsoft.VisualBasic.Interaction.Beep();
}
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();
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);
double dblRadius = Math.Min(ClientSize.Width / grfx.DpiX,
ClientSize.Height / grfx.DpiY) / intBallSize;
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);
}
}

Comments or Responses

Login to post response