Posted by: Zeeshan Amjad | November 8, 2009

Doing Animation with Code using VC++


We have already seen the example of doing animation with XAML. Now we are going to study that how can we do the animation with Code. Doing animation with code is not very difficult. First we have to create an object of timeline inherited class, then set the properties of animation and then call the BeginAnimation method. Here is a sample code to demonstrate this.

  1: TextBlock^ tb = gcnew TextBlock();
  2: tb->Text = "Hello Animation World";
  3: tb->Foreground = Brushes::Blue;
  4: 
  5: Content = tb;
  6: 
  7: DoubleAnimation^ da = gcnew DoubleAnimation();
  8: da->From = 10;
  9: da->To = 32;
 10: da->Duration = TimeSpan::FromSeconds(5);
 11: da->RepeatBehavior = RepeatBehavior::Forever;
 12: 
 13: tb->BeginAnimation(TextBlock::FontSizeProperty, da);
 14: 

DoubleAnimation class is already defined for us for double data type. WPF framework already define 22 classes for different data types such as Boolean, character, integer, Color, point, size, string, etc. Here is a partial class diagram of DoubleAnimation class

DoubleAnimationBase

BeginAnimation method is actually defined in IAnimatable interface and it is impmeneted by UIElement class in WPF. In other words any class inherited by UIElement can do animation. Here is a class diagram of IAnimatable interface.

IAnimatable

There are two important things to do the animation. First the animation can only be done with dependency property. We can’t do animation on normal CLR properties. Second the property meta data of dependency property must allow the animation. What does it mean? If we recall the property meta class class hierarchy then it would be something like this.

PropertyMetaDAta

The UIPropertyMetadata class define one property named IsAnimationProhibited. If this property is set to true then animation can’t be performed on that property.

Here is a complete VC++ program to show the animation using Vc++ code alone.

  1: #include <windows.h>
  2: 
  3: using namespace System;
  4: using namespace System::Windows;
  5: using namespace System::Windows::Controls;
  6: using namespace System::Windows::Media;
  7: using namespace System::Windows::Media::Animation;
  8: 
  9: public ref class MyWindow : Window
 10: {
 11: public:
 12: 	MyWindow()
 13: 	{
 14: 		Title = "Animation";
 15: 		Width = 400;
 16: 		Height = 300;	
 17: 
 18: 		TextBlock^ tb = gcnew TextBlock();
 19: 		tb->Text = "Hello Animation World";
 20: 		tb->Foreground = Brushes::Blue;
 21: 
 22: 		Content = tb;
 23: 
 24: 		DoubleAnimation^ da = gcnew DoubleAnimation();
 25: 		da->From = 10;
 26: 		da->To = 32;
 27: 		da->Duration = TimeSpan::FromSeconds(5);
 28: 		da->RepeatBehavior = RepeatBehavior::Forever;
 29: 
 30: 		tb->BeginAnimation(TextBlock::FontSizeProperty, da);
 31: 	}
 32: };
 33: 
 34: [STAThread]
 35: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 36: 				   LPSTR lpCmd, int nCmd)
 37: {
 38: 	MyWindow^ win = gcnew MyWindow();
 39: 
 40: 	Application^ app = gcnew Application();
 41: 	app->Run(win);
 42: 
 43: 	return 0;
 44: }
 45: 

The output of a program is a text message “Hello Animation world” initially with font size 10 and increase the font size till it is 32. Then it will go back and decrees the font size from 32 to 10 and keep doing this.  Here is one of the output of this program.

AnimationCode

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: