Introduction
There are a lot of techniques for bringing animation in your Windows application - simple and complex techniques. Here, I can explain a simple technique for giving animation or moving effects for your Windows Forms or any other controls.
Using the Code
Follow these steps:
- Create a solution (e.g.:
SimpleAnimation
) - Place your controls on it:
e.g.: - Split Container -
scnrMain
- Buttons -
btnAnimation1
, btnAnimation2
, btnReset
, btnQuit
- Panel -
pnlMain
- Add a timer control (e.g.:
tmrMain
) - Write your code as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimpleAnimation
{
public partial class frmMain : Form
{
int scnrWidth;
int scnrHeight;
int pnlWidth;
int pnlHeight;
public frmMain()
{
InitializeComponent();
}
public void frmMain_Load(object sender, EventArgs e)
{
pnlMain.Width = 0;
pnlMain.Height = 0;
scnrWidth = scnrMain.Panel2.Width;
scnrHeight = scnrMain.Panel2.Height;
pnlWidth = pnlMain.Width;
pnlHeight = pnlMain.Height;
}
private void btnAnimation1_Click(object sender, EventArgs e)
{
tmrMain.Enabled = true;
btnAnimation1.Enabled = false;
btnAnimation2.Enabled = false;
}
private void tmrMain_Tick(object sender, EventArgs e)
{
while(pnlWidth < scnrWidth || pnlHeight < scnrHeight)
{
if (pnlMain.Width < scnrWidth)
{
pnlMain.Width = pnlWidth + 1;
pnlWidth++;
}
if (pnlMain.Height < scnrHeight)
{
pnlMain.Height = pnlHeight + 1;
pnlHeight++;
}
break;
}
}
private void btnAnimation2_Click(object sender, EventArgs e)
{
pnlMain.Height = scnrHeight;
while (pnlMain.Width < scnrWidth)
{
pnlMain.Width++;
}
btnAnimation2.Enabled = false;
btnAnimation2.Enabled = false;
}
private void btnReset_Click(object sender, EventArgs e)
{
pnlMain.Width = 0;
pnlMain.Height = 0;
scnrWidth = scnrMain.Panel2.Width;
scnrHeight = scnrMain.Panel2.Height;
pnlWidth = pnlMain.Width;
pnlHeight = pnlMain.Height;
btnAnimation1.Enabled = true;
btnAnimation2.Enabled = true;
tmrMain.Enabled = false;
}
private void btnQuitApp_Click(object sender, EventArgs e)
{
while (this.Height>60)
{
this.Height--;
}
Application.Exit();
}
}
}
The sample project will be as shown in Figure 1:
Figure 1
Thank you for using my tip.
Enjoy programming...