using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Form_Animation_and_Toast_Demo
{
///
/// Animates a form when it is shown, hidden or closed.
///
///
/// MDI child forms do not support the Blend method and only support other methods while being displayed for the first time and when closing.
///
public sealed class FormAnimator
{
#region Types
///
/// The methods of animation available.
///
public enum AnimationMethod
{
///
/// Rolls out from edge when showing and into edge when hiding.
///
///
/// This is the default animation method and requires a direction.
///
Roll = 0x0,
///
/// Expands out from centre when showing and collapses into centre when hiding.
///
Centre = 0x10,
///
/// Slides out from edge when showing and slides into edge when hiding.
///
///
/// Requires a direction.
///
Slide = 0x40000,
///
/// Fades from transaprent to opaque when showing and from opaque to transparent when hiding.
///
Blend = 0x80000
}
///
/// The directions in which the Roll and Slide animations can be shown.
///
///
/// Horizontal and vertical directions can be combined to create diagonal animations.
///
[Flags()]
public enum AnimationDirection
{
///
/// From left to right.
///
Right = 0x1,
///
/// From right to left.
///
Left = 0x2,
///
/// From top to bottom.
///
Down = 0x4,
///
/// From bottom to top.
///
Up = 0x8
}
#endregion // Types
#region Constants
///
/// Hide the form.
///
private const int AW_HIDE = 0x10000;
///
/// Activate the form.
///
private const int AW_ACTIVATE = 0x20000;
///
/// The number of milliseconds over which the animation occurs if no value is specified.
///
private const int DEFAULT_DURATION = 250;
#endregion // Constants
#region Variables
///
/// The form to be animated.
///
private Form _form;
///
/// The animation method used to show and hide the form.
///
private AnimationMethod _method;
///
/// The direction in which to Roll or Slide the form.
///
private AnimationDirection _direction;
///
/// The number of milliseconds over which the animation is played.
///
private int _duration;
#endregion // Variables
#region Properties
///
/// Gets or sets the animation method used to show and hide the form.
///
///
/// The animation method used to show and hide the form.
///
///
/// Roll is used by default if no method is specified.
///
public AnimationMethod Method
{
get
{
return this._method;
}
set
{
this._method = value;
}
}
///
/// Gets or Sets the direction in which the animation is performed.
///
///
/// The direction in which the animation is performed.
///
///
/// The direction is only applicable to the Roll and Slide methods.
///
public AnimationDirection Direction
{
get
{
return this._direction;
}
set
{
this._direction = value;
}
}
///
/// Gets or Sets the number of milliseconds over which the animation is played.
///
///
/// The number of milliseconds over which the animation is played.
///
public int Duration
{
get
{
return this._duration;
}
set
{
this._duration = value;
}
}
///
/// Gets the form to be animated.
///
///
/// The form to be animated.
///
public Form Form
{
get
{
return this._form;
}
}
#endregion // Properties
#region APIs
///
/// Windows API function to animate a window.
///
[DllImport("user32")]
private extern static bool AnimateWindow(IntPtr hWnd,
int dwTime,
int dwFlags);
#endregion // APIs
#region Constructors
///
/// Creates a new FormAnimator object for the specified form.
///
///
/// The form to be animated.
///
///
/// No animation will be used unless the Method and/or Direction properties are set independently. The Duration is set to quarter of a second by default.
///
public FormAnimator(Form form)
{
this._form = form;
this._form.Load += new EventHandler(Form_Load);
this._form.VisibleChanged += new EventHandler(Form_VisibleChanged);
this._form.Closing += new CancelEventHandler(Form_Closing);
this._duration = DEFAULT_DURATION;
}
///
/// Creates a new FormAnimator object for the specified form using the specified method over the specified duration.
///
///
/// The form to be animated.
///
///
/// The animation method used to show and hide the form.
///
///
/// The number of milliseconds over which the animation is played.
///
///
/// No animation will be used for the Roll or Slide methods unless the Direction property is set independently.
///
public FormAnimator(Form form,
AnimationMethod method,
int duration)
: this(form)
{
this._method = method;
this._duration = duration;
}
///
/// Creates a new FormAnimator object for the specified form using the specified method in the specified direction over the specified duration.
///
///
/// The form to be animated.
///
///
/// The animation method used to show and hide the form.
///
///
/// The direction in which to animate the form.
///
///
/// The number of milliseconds over which the animation is played.
///
///
/// The direction argument will have no effect if the Centre or Blend method is
/// specified.
///
public FormAnimator(Form form,
AnimationMethod method,
AnimationDirection direction,
int duration)
: this(form, method, duration)
{
this._direction = direction;
}
#endregion // Constructors
#region Event Handlers
///
/// Animates the form automatically when it is loaded.
///
private void Form_Load(object sender, EventArgs e)
{
// MDI child forms do not support transparency so do not try to use the Blend method.
if (this._form.MdiParent == null || this._method != AnimationMethod.Blend)
{
// Activate the form.
AnimateWindow(this._form.Handle,
this._duration,
AW_ACTIVATE | (int)this._method | (int)this._direction);
}
}
///
/// Animates the form automatically when it is shown or hidden.
///
private void Form_VisibleChanged(object sender, EventArgs e)
{
// Do not attempt to animate MDI child forms while showing or hiding as they do not behave as expected.
if (this._form.MdiParent == null)
{
int flags = (int)this._method | (int)this._direction;
if (this._form.Visible)
{
// Activate the form.
flags = flags | AW_ACTIVATE;
}
else
{
// Hide the form.
flags = flags | AW_HIDE;
}
AnimateWindow(this._form.Handle,
this._duration,
flags);
}
}
///
/// Animates the form automatically when it closes.
///
private void Form_Closing(object sender, CancelEventArgs e)
{
if (!e.Cancel)
{
// MDI child forms do not support transparency so do not try to use the Blend method.
if (this._form.MdiParent == null || this._method != AnimationMethod.Blend)
{
// Hide the form.
AnimateWindow(this._form.Handle,
this._duration,
AW_HIDE | (int)this._method | (int)this._direction);
}
}
}
#endregion // Event Handlers
}
}