c#之利用API函数实现动画窗体的方法详解

所属分类: 软件编程 / C#教程 阅读数: 120
收藏 0 赞 0 分享
这里主要利用API函数Animate Window实现窗体左右,上下,扩展,淡入滑动或滚动动画效果,步骤如下:
1.新建窗体,使用2个GroupBox控件。
2.在控件1中添加2个RadioButton控件,并设置Text分别为“滚动窗体”,“滑动窗体”,并使前者Checked设置为True。
3.在空间2中添加6个按钮,Text分别为“自左向右动画”,“自右向左动画”,“自上向下动画”,“自下向上动画”,“向外扩展动画”,“淡入动画窗体”。
4.添加一新的Window窗体,设置Text为“动画窗体”。设置其“BackgroundImage”属性,导入一张要加载的图像,然后设置其“BackgroundImageLayout”属性为“Stretch”。
5.各按钮事件主要代码如下:
复制代码 代码如下:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            if (radioButton1.Checked == true)
            {
                myf.Text = "自左向右滚动窗体动画效果";
            }
            else
            {
                myf.Text = "自左向右滑动窗体动画效果";
            }
            myf.Show();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            if (radioButton1.Checked == true)
            {
                myf.Text = "自右向左滚动窗体动画效果";
            }
            else
            {
                myf.Text = "自右向左滑动窗体动画效果";
            }
            myf.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            if (radioButton1.Checked == true)
            {
                myf.Text = "自上向下滚动窗体动画效果";
            }
            else
            {
                myf.Text = "自上向下滑动窗体动画效果";
            }
            myf.Show();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            if (radioButton1.Checked == true)
            {
                myf.Text = "自下向上滚动窗体动画效果";
            }
            else
            {
                myf.Text = "自下向上滑动窗体动画效果";
            }
            myf.Show();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            myf.Text = "向外扩展窗体动画效果";
            myf.Show();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            myf.Text = "淡入窗体动画效果";
            myf.Show();
        }

6.双击Form2窗体,进入代码视图。首先定义公用变量,具体代码如下:
复制代码 代码如下:

        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;
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);

7.下面为Form2窗体添加加载事件代码,具体如下:
复制代码 代码如下:

        private void Form2_Load(object sender, EventArgs e)
        {
            if (this.Text == "自左向右滚动窗体动画效果")
            {
                AnimateWindow(this.Handle,2000,AW_HOR_POSITIVE);
            }
            if (this.Text == "自左向右滑动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_SLIDE+AW_HOR_POSITIVE);
            }
            if (this.Text == "自右向左滚动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_HOR_NEGATIVE);
            }
            if (this.Text == "自右向左滑动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_HOR_NEGATIVE);
            }
            if (this.Text == "自上向下滚动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_VER_POSITIVE);
            }
            if (this.Text == "自上向下滑动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_VER_POSITIVE);
            }
            if (this.Text == "自下向上滚动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_VER_NEGATIVE);
            }
            if (this.Text == "自下向上滑动窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_VER_NEGATIVE);
            }
            if (this.Text == "向外扩展窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_SLIDE + AW_CENTER);
            }
            if (this.Text == "淡入窗体动画效果")
            {
                AnimateWindow(this.Handle, 2000, AW_BLEND);
            }
        }//yinyiniao's Blog

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多