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

所属分类: 软件编程 / C#教程 阅读数: 135
收藏 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# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多