目录
1.首先,定义一个用于封装AnimateWindow函数的类
一.涉及到的知识点
(1) AnimateWindow函数
用API函数AnimateWindow函数来实现窗体的动画效果。在C#中,你可以使用P/Invoke技术调用Windows API中的AnimateWindow函数来实现动画窗体。语法格式如下:
[DllImportAttribute("user32.dll")]
private static extern bool Animate Window(IntPtr hwnd,int dwTime,int dwFlags);参数说明
hwnd:IntPtr,窗口句柄。
dwTime:动画的持续时间,数值越大动画效果的时间就越长。
dwFlags:动画效果类型选项,其常量值及说明如表:
(2)操作流程
1.首先,定义一个用于封装AnimateWindow函数的类
public static partial class Animations
{
[LibraryImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool AnimateWindow(IntPtr hWnd, int dwTime, int dwFlags);
}
2.在窗体类中使用这个方法
private void Form1_Load(object sender, EventArgs e)
{
Animations.AnimateWindow(Handle, 500, (int)AnimationFlags.AW_HOR_POSITIVE | (int)AnimationFlags.AW_VER_POSITIVE);
}
[Flags]
public enum AnimationFlags
{
AW_HOR_POSITIVE = 0x0001,
AW_HOR_NEGATIVE = 0x0002,
AW_VER_POSITIVE = 0x0004,
AW_VER_NEGATIVE = 0x0008,
AW_CENTER = 0x0010
}
3.生成效果
在这个例子中,AW_HOR_POSITIVE 和 AW_VER_POSITIVE 标志表示窗口将从左上角向右下角展开。
二、实例
本实例设计的是一个动画显示的窗体,该程序运行后,窗体是慢慢地以拉伸的效果显示到用户的面前;窗体关闭时,也是一样慢慢地消失。
(1)Resources.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace _189.Properties {
using System;
/// <summary>
/// 一个强类型的资源类,用于查找本地化的字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// 返回此类使用的缓存的 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_189.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
internal static System.Drawing.Bitmap _03 {
get {
object obj = ResourceManager.GetObject("_03", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}
(2)Form1.Designer.cs
namespace _189
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
BackgroundImage = Properties.Resources._03;
BackgroundImageLayout = ImageLayout.Stretch;
ClientSize = new Size(354, 261);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "动画窗体";
FormClosed += Form1_FormClosed;
Load += Form1_Load;
ResumeLayout(false);
}
#endregion
}
}
(3)Form1.cs
// 动画窗体
using System.Runtime.InteropServices;
namespace _189
{
public partial class Form1 : Form
{
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;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 开始窗体动画
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
AnimateWindow(Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE);
}
//重写API函数,用来执行窗体动画显示操作
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
/// <summary>
/// 结束窗体动画
/// </summary>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
AnimateWindow(Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE + AW_HIDE);
}
}
}
(4)生成的动画效果
打开和关闭窗体都会产生动画效果,可惜动画无法截图,网友自己体验去吧。