Sunday, July 15, 2012

Animate Form - Slowly Show and Close

This is something makes a simple Form more aesthetically rich. Form cosmetic aspects plays major part but launch of a form also plays a major aspects how user perceive it. I was looking ways to do that, one way is to use timer to change the opacity of form during show and hide. Other way and better way to animate it. User need to use user32.dll for it.

Implementation:

Add following reference
using System.Runtime.InteropServices;

Initialization - add following in FormClass

namespace TestNamespace
{
    public partial class TestForm : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);
        [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
        }
    }
}

Need to handle two form events "XXX_Load" and "XXX_FormClosing" and add following code in that

private void TestForm_Load(object sender, EventArgs e)
{
   AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_ACTIVATE);
}

private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
{
   AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
}

Reference :- http://subversion.assembla.com/svn/nex-framework/trunk/.NEX%20framework/WindowsAPI/user32.cs

1 comment:

  1. Dear, I like your blog very much, I can get many useful information. Hope that we can communicate with each other. By the way, have you ever used memory card software ? Unfortunately I lost my memory card data. I do not know how to do.

    ReplyDelete