当前位置: 代码网 > it编程>编程语言>C# > C#使用AnimateWindow()实现动画窗体的方法

C#使用AnimateWindow()实现动画窗体的方法

2024年05月26日 C# 我要评论
一.涉及到的知识点(1)animatewindow函数用api函数animatewindow函数来实现窗体的动画效果。在c#中,你可以使用p/invoke技术调用windows api中的animat

一.涉及到的知识点

(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:动画效果类型选项,其常量值及说明如表:

    

   

aw_slide

0x0004000

使用滑动类型。默认则为滚动动画类型。当使用aw_center标志时,这个标志就被忽略

aw_activate

0x00020000

激活窗口。在使用aw_hide标志后不要使用这个标志

aw_blend

0x00080000

使用淡入效果。只有当hwnd为顶层窗口时才可以使用此标志

aw_hide

0x00010000

隐藏窗口,默认则显示窗口

aw_center

0x00000010

若使用aw_hide标志,则使窗口向内重叠;若未使用aw_hide标志,则使窗口向外扩展

aw_hor_positive

0x00000001

自左向右显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用aw_center标志时,该标志将被忽略

aw_hor_negative

0x00000002

自右向左显示窗口。当使用aw_center标志时该标志被忽略

aw_ver_positive

0x00000004

自顶向下显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用aw_center标志时,该标志将被忽略

aw_ver_negative

0x00000008

自下向上显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用aw_center标志时,该标志将被忽略

(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)生成的动画效果

打开和关闭窗体都会产生动画效果,可惜动画无法截图,网友自己体验去吧。

以上就是c#使用animatewindow()实现动画窗体的方法的详细内容,更多关于c# animatewindow()动画窗体的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com