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 // 類通過類似于 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)文章
c#中DataTable轉(zhuǎn)List的2種方法示例
這篇文章主要給大家介紹了關(guān)于c#中DataTable轉(zhuǎn)List的2種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04C#讀取數(shù)據(jù)庫(kù)返回泛型集合詳解(DataSetToList)
本篇文章主要是對(duì)C#讀取數(shù)據(jù)庫(kù)返回泛型集合(DataSetToList)進(jìn)行了介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#創(chuàng)建、部署、調(diào)用WebService圖文實(shí)例詳解
本文主要用詳細(xì)的圖文給大家介紹C#創(chuàng)建、部署、調(diào)用WebService的全部過程以及中間需要避免的問題。2017-11-11C#實(shí)現(xiàn)通過winmm.dll控制聲音播放的方法
這篇文章主要介紹了C#實(shí)現(xiàn)通過winmm.dll控制聲音播放的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08