利用API函数实现动画窗体

 

 

此列中用到了

System.Runtime.InteropServices空间  互操作服务”。System.Runtime.InteropServices这个名称空间提供了一系列的类来对COM对象进行操作

 

具体代代码如下:

 

Form1中 :

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Case01_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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 button2_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 button3_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            if (radioButton1.Checked == true)
            {
                myf.Text = "自下向上滚动窗体动画效果";
            }
            else
            {
                myf.Text = "自下向上滑动窗体动画效果";
            }
            myf.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            myf.Text = "向外扩展窗体动画效果";
            myf.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();
            myf.Text = "淡入窗体动画效果";
            myf.Show();
        }
    }
}

 

Form2中:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Case01_3
{
    public partial class Form2 : 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;           //使用淡入效果

        //声明AnimateWindow函数
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

        public Form2()
        {
            InitializeComponent();
        }

        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);
           }
      
        }
    }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值