实现效果:
知识运用:
API函数AnimateWindows
[DllImportAttribute("user32.dll")]
private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);
- hwnd: IntPtr,窗口句柄
- dwTime:动画的持续时间
- dwFlags:动画类型选项 常量值如下
实现代码:
private const int AW_SLIDE = 0x00040000; private const int AW_VER_NEGATIVE = 0x00000008; private const int AW_HIDE = 0x00010000; public Form1() { InitializeComponent(); AnimateWindow(this.Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE); //开始窗体动画 } [DllImportAttribute("user32.dll")] private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags); //重写API函数用来执行窗体动画显示操作 //绘制渐变背景 private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Color sColor = Color.Green; //颜色1,2 Color eColor = Color.Black; //实例化刷子 (上色域 开始色 结束色 颜色方向) Brush b = new LinearGradientBrush(this.ClientRectangle,sColor,eColor,LinearGradientMode.Vertical); g.FillRectangle(b,this.ClientRectangle); //进行上色 } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { AnimateWindow(this.Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE+AW_HIDE); //结束窗体动画 }