01-3 利用API函数实现动画窗体【*】

本文介绍了一种使用 C# 在 Windows Forms 应用程序中实现不同方向的滚动和滑动窗体动画效果的方法。通过调用 AnimateWindow 函数并设置不同的标志来达到自定义动画的目的,还展示了如何实现向外扩展和淡入效果。

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


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; 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(); } private void Form1_Load(object sender, EventArgs e) { } } }


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; 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); } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值