C#使用AnimateWindow()實(shí)現(xiàn)動(dòng)畫窗體的方法
一.涉及到的知識(shí)點(diǎn)
(1) AnimateWindow函數(shù)
用API函數(shù)AnimateWindow函數(shù)來(lái)實(shí)現(xiàn)窗體的動(dòng)畫效果。在C#中,你可以使用P/Invoke技術(shù)調(diào)用Windows API中的AnimateWindow函數(shù)來(lái)實(shí)現(xiàn)動(dòng)畫窗體。語(yǔ)法格式如下:
[DllImportAttribute("user32.dll")]
private static extern bool Animate Window(IntPtr hwnd,int dwTime,int dwFlags);參數(shù)說(shuō)明
hwnd:IntPtr,窗口句柄。
dwTime:動(dòng)畫的持續(xù)時(shí)間,數(shù)值越大動(dòng)畫效果的時(shí)間就越長(zhǎng)。
dwFlags:動(dòng)畫效果類型選項(xiàng),其常量值及說(shuō)明如表:常 量 | 值 | 說(shuō) 明 |
AW_SLIDE | 0x0004000 | 使用滑動(dòng)類型。默認(rèn)則為滾動(dòng)動(dòng)畫類型。當(dāng)使用AW_CENTER標(biāo)志時(shí),這個(gè)標(biāo)志就被忽略 |
AW_ACTIVATE | 0x00020000 | 激活窗口。在使用AW_HIDE標(biāo)志后不要使用這個(gè)標(biāo)志 |
AW_BLEND | 0x00080000 | 使用淡入效果。只有當(dāng)hWnd為頂層窗口時(shí)才可以使用此標(biāo)志 |
AW_HIDE | 0x00010000 | 隱藏窗口,默認(rèn)則顯示窗口 |
AW_CENTER | 0x00000010 | 若使用AW_HIDE標(biāo)志,則使窗口向內(nèi)重疊;若未使用AW_HIDE標(biāo)志,則使窗口向外擴(kuò)展 |
AW_HOR_POSITIVE | 0x00000001 | 自左向右顯示窗口。該標(biāo)志可以在滾動(dòng)動(dòng)畫和滑動(dòng)動(dòng)畫中使用。當(dāng)使用AW_CENTER標(biāo)志時(shí),該標(biāo)志將被忽略 |
AW_HOR_NEGATIVE | 0x00000002 | 自右向左顯示窗口。當(dāng)使用AW_CENTER標(biāo)志時(shí)該標(biāo)志被忽略 |
AW_VER_POSITIVE | 0x00000004 | 自頂向下顯示窗口。該標(biāo)志可以在滾動(dòng)動(dòng)畫和滑動(dòng)動(dòng)畫中使用。當(dāng)使用AW_CENTER標(biāo)志時(shí),該標(biāo)志將被忽略 |
AW_VER_NEGATIVE | 0x00000008 | 自下向上顯示窗口。該標(biāo)志可以在滾動(dòng)動(dòng)畫和滑動(dòng)動(dòng)畫中使用。當(dāng)使用AW_CENTER標(biāo)志時(shí),該標(biāo)志將被忽略 |
(2)操作流程
1.首先,定義一個(gè)用于封裝AnimateWindow函數(shù)的類
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.在窗體類中使用這個(gè)方法
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.生成效果
在這個(gè)例子中,AW_HOR_POSITIVE 和 AW_VER_POSITIVE 標(biāo)志表示窗口將從左上角向右下角展開。
二、實(shí)例
本實(shí)例設(shè)計(jì)的是一個(gè)動(dòng)畫顯示的窗體,該程序運(yùn)行后,窗體是慢慢地以拉伸的效果顯示到用戶的面前;窗體關(guān)閉時(shí),也是一樣慢慢地消失。
(1)Resources.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運(yùn)行時(shí)版本:4.0.30319.42000
//
// 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果
// 重新生成代碼,這些更改將會(huì)丟失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace _189.Properties {
using System;
/// <summary>
/// 一個(gè)強(qiáng)類型的資源類,用于查找本地化的字符串等。
/// </summary>
// 此類是由 StronglyTypedResourceBuilder
// 類通過(guò)類似于 ResGen 或 Visual Studio 的工具自動(dòng)生成的。
// 若要添加或移除成員,請(qǐng)編輯 .ResX 文件,然后重新運(yùn)行 ResGen
// (以 /str 作為命令選項(xiàng)),或重新生成 VS 項(xiàng)目。
[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 實(shí)例。
/// </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>
/// 重寫當(dāng)前線程的 CurrentUICulture 屬性,對(duì)
/// 使用此強(qiáng)類型資源類的所有資源查找執(zhí)行重寫。
/// </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 = "動(dòng)畫窗體";
FormClosed += Form1_FormClosed;
Load += Form1_Load;
ResumeLayout(false);
}
#endregion
}
}(3)Form1.cs
// 動(dòng)畫窗體
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>
/// 開始窗體動(dòng)畫
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
AnimateWindow(Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE);
}
//重寫API函數(shù),用來(lái)執(zhí)行窗體動(dòng)畫顯示操作
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
/// <summary>
/// 結(jié)束窗體動(dòng)畫
/// </summary>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
AnimateWindow(Handle, 3000, AW_SLIDE + AW_VER_NEGATIVE + AW_HIDE);
}
}
}(4)生成的動(dòng)畫效果
打開和關(guān)閉窗體都會(huì)產(chǎn)生動(dòng)畫效果,可惜動(dòng)畫無(wú)法截圖,網(wǎng)友自己體驗(yàn)去吧。

以上就是C#使用AnimateWindow()實(shí)現(xiàn)動(dòng)畫窗體的方法的詳細(xì)內(nèi)容,更多關(guān)于C# AnimateWindow()動(dòng)畫窗體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Unity C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
openfiledialog讀取txt寫入數(shù)據(jù)庫(kù)示例
這篇文章主要介紹了openfiledialog讀取txt寫入數(shù)據(jù)庫(kù)示例,需要的朋友可以參考下2014-03-03
在C#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了在C#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
C# winform實(shí)現(xiàn)登陸次數(shù)限制
這篇文章主要介紹了C# winform實(shí)現(xiàn)登陸次數(shù)限制,相信大家都遇到過(guò)網(wǎng)站在用戶多次輸錯(cuò)密碼之后會(huì)自動(dòng)把賬戶凍結(jié)的情況,這種功能如何實(shí)現(xiàn),下面小編為大家分享實(shí)現(xiàn)方法2016-05-05
C#實(shí)現(xiàn)的SN快速輸入工具實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的SN快速輸入工具,以實(shí)例的形式詳細(xì)講述了C#實(shí)現(xiàn)序列號(hào)快速輸入的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法(深度復(fù)制)
這篇文章主要介紹了C#?中?List?與?List?多層嵌套不改變?cè)档膶?shí)現(xiàn)方法,使用?BinaryFormatter?將原始?List?序列化為字節(jié)流,然后再反序列化得到新的?List,實(shí)現(xiàn)了深度復(fù)制,需要的朋友可以參考下2024-03-03
C#使用FluentHttpClient實(shí)現(xiàn)請(qǐng)求WebApi
FluentHttpClient 是一個(gè)REST API 異步調(diào)用 HTTP 客戶端,調(diào)用過(guò)程非常便捷,下面我們就來(lái)學(xué)習(xí)一下C#如何使用FluentHttpClient實(shí)現(xiàn)請(qǐng)求WebApi吧2023-12-12
C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類,結(jié)合具體實(shí)例形式分析了C#針對(duì)UDP請(qǐng)求的監(jiān)聽(tīng)、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

