[System.Runtime.InteropServices.DllImport("user32")]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
const int AW_HOR_POSITIVE = 0x0001;
const int AW_HOR_NEGATIVE = 0x0002;
const int AW_VER_POSITIVE = 0x0004;
const int AW_VER_NEGATIVE = 0x0008;
const int AW_CENTER = 0x0010;
const int AW_HIDE = 0x10000;
const int AW_ACTIVATE = 0x20000;
const int AW_SLIDE = 0x40000;
const int AW_BLEND = 0x80000;
/// <summary>
/// 窗体由中心点放大
/// </summary>
/// <param name="hwnd"></param>
public static void WindowZoomIn(IntPtr hwnd)
{
AnimateWindow(hwnd, 1000, AW_CENTER | AW_ACTIVATE);
}
/// <summary>
/// 窗体由下往上收缩
/// </summary>
/// <param name="hwnd"></param>
public static void WindowZoomOut(IntPtr hwnd)
{
AnimateWindow(hwnd, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
}
}
在使用时只要在Form窗体的Load和FormClosing事件里分别加入以下代码:
/// <summary>
/// 窗体启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
//窗体由中心向四周放大
AnimateWindowFrom.WindowZoomIn(this.Handle);
}
/// <summary>
/// 窗体关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//窗体由下往上伸缩
AnimateWindowFrom.WindowZoomOut(this.Handle);
}