Drop in class for exAnimateWindow

Diamonddrake 1 Tallied Votes 253 Views Share

This simple class encapsulates the exAnimatewindow api call. I got tired of adding all the jazz to every form I need to use as a pop up, or any borderless forms I used. (Windows 7 and vista don't animate windows with no border by default)

This class could easily be expanded to offer all the animation directions and types But I included just the ones I use most often. I like a small footprint. No need for code you don't use. So just expand it if you need more functionality.

here is the simple usage. Just throw this line in the constructor of the form you wish to animate.

DDFormsExtentions.WindowAnimator DDwa = new DDFormsExtentions.WindowAnimator(this, DDFormsExtentions.WindowAnimator.AniType.SlideDown, 200);
kvprajapati commented: Wonderful! +9
//2010 RickeyWard@DiamondDrake.com
//Free to use for any purpose, NO garantees whatsoever.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DDFormsExtentions
{
    public class WindowAnimator
    {
        private AniType aniT;
        private int anispeed = 175;
        IntPtr FormHwnd;

        public WindowAnimator(Form FormToAni, AniType TypeAni, int AniSpeed_175)
        {
            aniT = TypeAni;
            anispeed = AniSpeed_175;
            FormHwnd = FormToAni.Handle;

            FormToAni.Load += new EventHandler(FormToAni_Load);
            FormToAni.FormClosing += new FormClosingEventHandler(FormToAni_FormClosing);
        }

        void FormToAni_FormClosing(object sender, FormClosingEventArgs e)
        {
            switch (aniT)
            {
                case AniType.SlideDown:
                    AnimateWindow(FormHwnd, anispeed, (int)AnimateWindowFlags.AW_SLIDE| (int)

AnimateWindowDirections.Up | (int)AnimateWindowFlags.AW_HIDE);
                    break;
                case AniType.Fade:
                    AnimateWindow(FormHwnd, anispeed, (int)AnimateWindowFlags.AW_BLEND | (int)

AnimateWindowFlags.AW_HIDE);
                    break;
                default:
                    break;
            }
          
        }

        void FormToAni_Load(object sender, EventArgs e)
        {
            switch (aniT)
            {
                case AniType.SlideDown:
                    AnimateWindow(FormHwnd, anispeed, (int)AnimateWindowFlags.AW_SLIDE | (int)

AnimateWindowDirections.Down | (int)AnimateWindowFlags.AW_ACTIVATE);
                    break;
                case AniType.Fade:
                    AnimateWindow(FormHwnd, anispeed, (int)AnimateWindowFlags.AW_BLEND | (int)

AnimateWindowFlags.AW_ACTIVATE);
                    break;
                default:
                    break;
            }
        }

        public enum AniType
        {
            SlideDown,
            Fade
        }

        #region Win32

        const int AW_HIDE = 0X10000;
        const int AW_ACTIVATE = 0X20000;
        const int AW_HOR_POSITIVE = 0X1;
        const int AW_HOR_NEGATIVE = 0X2;
        const int AW_SLIDE = 0X40000;
        const int AW_BLEND = 0X80000;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int AnimateWindow
        (IntPtr hwand, int dwTime, int dwFlags);

        #endregion

        #region Variables

        public 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
        }

        public enum AnimateWindowDirections
        {
            Right = 0x00000001,
            Left = 0x00000002,
            Down = 0x00000004,
            Up = 0x00000008,
            Center = 0x00000010
        }

        #endregion
    }
}