C# winform窗体动画实现

这里主要是用到win32的一个方法 AnimateWindow(),详细介绍可以搜索AnimateWindow。

代码:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace AnimationForm
{
    public class Win32
    {
        /// 
        /// 执行动画
        /// 
        /// 控件句柄
        /// 动画时间
        /// 动画组合名称
        /// bool值,动画是否成功
        [DllImport("user32.dll")]
        public static extern bool AnimateWindow(IntPtr whnd, int dwtime, int dwflag);

        /// 
        /// 从左到右显示
        /// 
        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;
        /// 
        /// 若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
        /// 
        public const Int32 AW_CENTER = 0x00000010;
        /// 
        /// 隐藏窗口,缺省则显示窗口
        /// 
        public const Int32 AW_HIDE = 0x00010000;
        /// 
        /// 激活窗口。在使用了AW_HIDE标志后不能使用这个标志
        /// 
        public const Int32 AW_ACTIVATE = 0x00020000;
        /// 
        /// 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
        /// 
        public const Int32 AW_SLIDE = 0x00040000;
        /// 
        /// 透明度从高到低
        /// 
        public const Int32 AW_BLEND = 0x00080000;
    }
}

使用:

        /// 
        /// 淡入淡出特效
        /// 
        /// 
        /// 
        private void btnJb_Click(object sender, EventArgs e) {
            //淡出特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_BLEND | Win32.AW_HIDE)) {
                //淡入特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_BLEND | Win32.AW_ACTIVATE);
            }
        }

        /// 
        /// 右到左特效
        /// 
        /// 
        /// 
        private void btnYdz_Click(object sender, EventArgs e) {
            //右到左特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_HOR_NEGATIVE | Win32.AW_HIDE | Win32.AW_SLIDE)) {
                //右到左特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_HOR_NEGATIVE | Win32.AW_ACTIVATE | Win32.AW_SLIDE);
            }
        }

        /// 
        /// 左到右特效
        /// 
        /// 
        /// 
        private void btnZdy_Click(object sender, EventArgs e) {
            //左到右特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_HOR_POSITIVE | Win32.AW_HIDE | Win32.AW_SLIDE)) {
                //左到右特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_HOR_POSITIVE | Win32.AW_ACTIVATE | Win32.AW_SLIDE);
            }
        }

        /// 
        /// 收缩扩张特效
        /// 
        /// 
        /// 
        private void btnKz_Click(object sender, EventArgs e) {
            //收缩特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_CENTER | Win32.AW_HIDE | Win32.AW_SLIDE)) {
                //扩张特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_CENTER | Win32.AW_ACTIVATE | Win32.AW_SLIDE);
            }
        }

        /// 
        /// 上到下特效
        /// 
        /// 
        /// 
        private void btnSx_Click(object sender, EventArgs e) {
            //上到下特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_HIDE | Win32.AW_VER_POSITIVE | Win32.AW_SLIDE)) {
                //上到下特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_ACTIVATE | Win32.AW_VER_POSITIVE | Win32.AW_SLIDE);
            }
        }

        /// 
        /// 下到上特效
        /// 
        /// 
        /// 
        private void btnXs_Click(object sender, EventArgs e) {
            //下到上特效隐藏
            if (Win32.AnimateWindow(this.Handle, 300, Win32.AW_HIDE | Win32.AW_VER_NEGATIVE | Win32.AW_SLIDE)) {
                //下到上特效显示
                Win32.AnimateWindow(this.Handle, 300, Win32.AW_ACTIVATE | Win32.AW_VER_NEGATIVE | Win32.AW_SLIDE);
            }
        }


你可能感兴趣的:(C#)