Simple Animation for your Windows Forms or Controls using C#.NET
Simple Animation with Timer control in C#.NET (Sliding and Scrolling Effects)
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:
//frmMain // 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 { // //declares variables used in this class // int scnrWidth; int scnrHeight; int pnlWidth; int pnlHeight; public frmMain() { InitializeComponent(); } // //allocate values to variables on Load event // 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; } // //timer control activated on click event of button Animation 1 - first option // private void btnAnimation1_Click(object sender, EventArgs e) { tmrMain.Enabled = true; btnAnimation1.Enabled = false; btnAnimation2.Enabled = false; } // //when timer activated, the width and height of panel increased using a while loop // 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; } } // //this is another way to increase panel width and height using while loop - second option // private void btnAnimation2_Click(object sender, EventArgs e) { pnlMain.Height = scnrHeight; while (pnlMain.Width < scnrWidth) { pnlMain.Width++; } btnAnimation2.Enabled = false; btnAnimation2.Enabled = false; } // //re-allocate values to variables and properties of controls // 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; } // //following lines of code will give your form a //upward scrolling effect - by decreasing height of form // private void btnQuitApp_Click(object sender, EventArgs e) { while (this.Height>60) { this.Height--; } Application.Exit(); } } } //End of code.
The sample project will be as shown in Figure 1:
Figure 1
Thank you for using my tip.
Enjoy programming...