调用AnimateWindow API来实现弹出效果

本文介绍了一种使用C#实现窗体动画的方法,通过调用Windows API中的AnimateWindow函数来控制窗体的显示和关闭动画,包括上下左右及中心缩放等效果,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是实例的cs代码

    public partial class frm_Main : Form
    {
        //使用Windows Api AnimateWindow
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        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;//若使用了AW_HIDE标志,则使窗口向内重叠,即收缩窗口;否则使窗口向外扩展,即展开窗口
        public const Int32 AW_HIDE = 0x00010000;//隐藏窗口,缺省则显示窗口
        public const Int32 AW_ACTIVATE = 0x00020000;//激活窗口。在使用了AW_HIDE标志后不能使用这个标志
        public const Int32 AW_SLIDE = 0x00040000;//使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
        public const Int32 AW_BLEND = 0x00080000;//透明度从高到低
        public frm_Main()
        {
           InitializeComponent();
            //设置窗体的样式为无边框
           this.FormBorderStyle = FormBorderStyle.None;
            //获取屏幕工作区域
           Rectangle rectWork=Screen.GetWorkingArea(this);
            //设置窗体的位置为右下角
           this.Location = new Point(rectWork.Width - this.Width,rectWork.Height - this.Height);
           //窗体的位置由Location属性确定
            //this.StartPosition = FormStartPosition.Manual;
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            AnimateWindow(this.Handle, 2000, AW_VER_POSITIVE);// 自上而下。
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void frm_Main_Load(object sender, EventArgs e)
        {
            //调用方法实现显示效果
            AnimateWindow(this.Handle, 2000, AW_VER_NEGATIVE + AW_VER_POSITIVE); // 自下向上。
        }
    }

 效果,实现一个桌面右下角往上升的一个窗体

个人感觉用一个定时器来控制窗体的location或许效果更爽!

转载于:https://www.cnblogs.com/xiexingen/p/3289978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值