一、实例介绍
本文实现的动画是指,窗体显示的时候慢慢显示到用户面前,窗体关闭时,有一个动态效果!
二、设计思路
需要使用Windows提供的API函数AnimateWindow(),该函数存放在user.dll文件中,该函数的声明方法如下:
[DllImport("user32.dll")]
private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);
其中参数意义如下:
hwnd:目标窗口句柄。
dwTime:动画持续时间。数值越大,动画效果越长。
dwFlags:动画效果类型选项。
使用动画类型效果必须在程序中声明才可以,声明代码如下。
public const Int32 AW_HOR_POSITIVE = 0x00000001;
public const Int32 AW_HOR_NEGATIVE = 0x00000002;
public const Int32 AW_VER_POSITIVE = 0x00000004;
public const Int32 AW_VER_NEGATIVE = 0x00000008;
public const Int32 AW_CENTER = 0x00000010;
public const Int32 AW_HIDE = 0x00010000;
public const Int32 AW_ACTIVATE = 0x00020000;
public const Int32 AW_SLIDE = 0x00040000;
public const Int32 AW_BLEND = 0x00080000;
这些动画效果可以叠加使用。
注意:必须引用命名空间:using System.Runtime.InteropServices;
三、设计方法
创建一个winform项目,添加一个form窗体,在窗体中添加picturebox,设置image属性为要显示的图片,设置dock属性为fill
四、窗体代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 11 namespace Kaifafanli 12 { 13 public partial class Form7 : Form 14 { 15 [DllImport("user32.dll")] 16 private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags); 17 public const Int32 AW_HOR_POSITIVE = 0x00000001; 18 public const Int32 AW_HOR_NEGATIVE = 0x00000002; 19 public const Int32 AW_VER_POSITIVE = 0x00000004; 20 public const Int32 AW_VER_NEGATIVE = 0x00000008; 21 public const Int32 AW_CENTER = 0x00000010; 22 public const Int32 AW_HIDE = 0x00010000; 23 public const Int32 AW_ACTIVATE = 0x00020000; 24 public const Int32 AW_SLIDE = 0x00040000; 25 public const Int32 AW_BLEND = 0x00080000; 26 public Form7() 27 { 28 InitializeComponent(); 29 } 30 31 private void Form7_Load(object sender, EventArgs e) 32 { 33 AnimateWindow(this.Handle,300,AW_SLIDE+AW_HOR_NEGATIVE); 34 } 35 36 private void Form7_FormClosed(object sender, FormClosedEventArgs e) 37 { 38 AnimateWindow(this.Handle, 300, AW_SLIDE + AW_HOR_NEGATIVE+AW_HIDE); 39 } 40 } 41 }