Animating a Windows Form
Introduction
In this tutorial, I will briefly go over how to add animation effects to a windows form in C#.
PInvoke
In order to animate a window in C#, we will need to call a native method using PInvoke. You can read more about PInvoke from here. Basically, for those who do not already know, Platform Invocation Services, or PInvoke, allows managed code to call unmanged functions that are implemented in a DLL.
Creating a New Project
First, we need to create a new project.
Open Visual Studio, and select “File>New>Project>Visual C#>Windows Forms Application”.
Now we need to setup our native methods that we will be using.
AnimateWindow Function
According to the MSDN Library, “the animate window function enables you to produce special effects when showing or hiding windows”. There are only four types of animation that you can use. They are: roll, slide, collapse/expand, and alpha-blended fade.
In order to call the AnimateWindow function, as already stated, we need to use PInvoke. This is the PInvoke signature for the AnimateWindow function (the using System.Runtime.InteropServices; declaration is required):
[DllImport(“user32.dll”)] static extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);
Add this to the top of the “Form1” class.
Notice the “AnimateWindowFlags” parameter. This is a user-defined type. So, in order for this code to compile, you must add this to enum to your code:
[Flags]
enum AnimateWindowFlags
{
AW_HOR_POSITIVE = 0x00000001,
AW_HOR_NEGATIVE = 0x00000002,
AW_VER_POSITIVE = 0x00000004,
AW_VER_NEGATIVE = 0x00000008,
AW_CENTER = 0x00000010,
AW_HIDE = 0x00010000,
AW_ACTIVATE = 0x00020000,
AW_SLIDE = 0x00040000,
AW_BLEND = 0x00080000
}
These are the possible animation types that you can use. Below is a brief overview of what each flag does.
AW_SLIDE :
Uses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER.
AW_ACTIVATE :
Activates the window. Do not use this value with AW_HIDE.
AW_BLEND :
Uses a fade effect. This flag can be used only if hwnd is a top-level window.
AW_HIDE :
Hides the window. By default, the window is shown.
AW_CENTER :
Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used. The various direction flags have no effect.
AW_HOR_POSITIVE :
Animates the window from left to right. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_HOR_NEGATIVE :
Animates the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_VER_POSITIVE :
Animates the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
AW_VER_NEGATIVE :
Animates the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.
Animating a Window
Now that we have the AnimateWindow function setup, let’s animate a window.
First, we need to create a load event handler for our form.
Inside of the form’s load event handler, add this code:
AnimateWindow(this.Handle, 500, AnimateWindowFlags.AW_BLEND);
Build and run your code. When your application starts, you should see the form fade in. Go ahead and mess around with the “time” parameter and the “AnimateWindowFlags” parameter. Remember that the higher the value you set the time, the slower the animation will take place.
Next, create a “Form_closing” event handler. Inside this event handler, add this code:
AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
As you may have guessed, this code will add an animation for the form as it closes.
That’s it!




MultiQuote






|