hi
can someone please tell me how to add an animation in my application?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mahesh ChandPosted Apr 20, 2010, 2:28 PM
Sam HobbsPosted Apr 20, 2010, 2:14 PM
Vladimir NaniPosted Apr 20, 2010, 4:36 AM
To add animation to your project:
- You can use Storyboards in WPF(I think this is the best way)
- Use Timer and receive Tick event
The following example simply animate rectangle moving.public partial class Form1 : Form
{
Graphics g;
//Declare timer var
private Timer timer1;
//dx & dy are amount of offset
int dx = 0;
int dy = 0;
public Form1()
{
InitializeComponent();
//Add new timer
timer1 = new Timer(this.components);
//Add timer event
timer1.Tick += new EventHandler(timer1_Tick);
//Interval between ticks 200 ms (Actually it is speed of your animation)
timer1.Interval = 200;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
g = CreateGraphics();
g.Clear(this.BackColor);
//Each timers tick you increase offset amount
dx++; dy++;
Pen pen = Pens.Brown;
Rectangle rect = new Rectangle(50 + dx, 50 + dy, 50, 50);
g.DrawRectangle(pen, rect);
}
}