欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-13 11:00:22
这里主要利用api函数animate window实现窗体左右,上下,扩展,淡入滑动或滚动动画效果,步骤如下:1.新建窗体,使用2个groupbox控件。2.在控件1中添加...
这里主要利用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