hi
can someone please tell me how to add an animation in my application?
C# animations
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vladimir Nani29 reputationPosted Apr 20, 2010, 12:36 AM EDT
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);
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Apr 20, 2010, 10:14 AM EDT
Mahesh ChandRank #1 · 393.3K reputationPosted Apr 20, 2010, 10:28 AM EDT