默認窗體瞬間顯示/關閉缺乏視覺反饋。Windows 提供 AnimateWindow API,可實現滑動、淡入淡出、中心展開等豐富動畫,無需複雜 GDI+ 代碼。
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
hwnd:窗體句柄(this.Handle)。dwTime:動畫持續毫秒數,值越大越慢。dwFlags:動畫類型組合標誌(位或運算)。
public const int AW_HOR_POSITIVE = 0x00000001;
public const int AW_HOR_NEGATIVE = 0x00000002;
public const int AW_VER_POSITIVE = 0x00000004;
public const int AW_VER_NEGATIVE = 0x00000008;
public const int AW_CENTER = 0x00000010;
public const int AW_HIDE = 0x00010000;
public const int AW_ACTIVATE = 0x00020000;
public const int AW_SLIDE = 0x00040000;
public const int AW_BLEND = 0x00080000;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 打開:1秒中心展開
AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_CENTER);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// 關閉:0.5秒向下滑動隱藏
AnimateWindow(this.Handle, 500, AW_SLIDE | AW_VER_POSITIVE | AW_HIDE);
}
}