Page 1 of 1

Animating a Windows Form Animate a window using C# and PInvoke

#1 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 12 June 2009 - 08:55 AM

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Animating a Windows Form

#2 develomer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 09

Posted 02 July 2009 - 06:40 PM

AWESOME POST! Cant stress enough how great this post was. It answered so many questions and it is so well written. Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#3 me.ankit22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 09

Posted 23 September 2009 - 08:43 AM

hello !!

i'm using this with a oval form. but i can't find any animation in that form. i have increased the animation time then also there's no difference. it's as it is as it was earlier.
help me plzz....

and thanks for your great effort. I love the way you explain. it's really vry helpful and interesting too.

i'll be waiting for your next demo. hope it'll be here soon..
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Posted 02 October 2009 - 11:25 AM

View Postme.ankit22, on 23 Sep, 2009 - 09:43 AM, said:

hello !!

i'm using this with a oval form. but i can't find any animation in that form. i have increased the animation time then also there's no difference. it's as it is as it was earlier.
help me plzz....

and thanks for your great effort. I love the way you explain. it's really vry helpful and interesting too.

i'll be waiting for your next demo. hope it'll be here soon..


I guessing that the problem is that you are drawing the form yourself.

If you post the code you are using, I may be able to provide more help. Please keep in mind that I have been very busy the last few months, and I may not be able to post back quickly. If you need a fast reply, post you question here, in the C# forums.
Was This Post Helpful? 0
  • +
  • -

#5 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Posted 10 January 2010 - 03:54 PM

This is pretty useful. :P I usually run away when I see code with [DllImport] in it, but now that I have, it's great now- learned something new. Something I found was that if you were wondering how to get the enums, you can go to PInvoke.net and find pretty much everything. For example, the page on AnimateWindow is located here: Animate Window
Was This Post Helpful? 0
  • +
  • -

#6 Cemgates101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-February 10

Posted 01 February 2010 - 10:37 AM

Thank you very much! This was awesome and fun to try out. Great tutorial.
Was This Post Helpful? 0
  • +
  • -

#7 prashant.gudadhe  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 3
  • Joined: 08-June 09

Posted 26 December 2012 - 11:19 PM

View PostlesPaul456, on 12 June 2009 - 09:25 PM, said:

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!


Thank you very much..really its very good post...many years i was searching for it!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1