温馨提示×

利用C#实现子窗口的弹出与隐藏动画

c#
小樊
214
2024-09-04 16:13:17
栏目: 编程语言

在C#中,你可以使用Windows Forms或WPF来创建子窗口并实现弹出和隐藏动画。这里我将分别为这两种技术提供一个示例。

Windows Forms 示例:

首先,确保你已经添加了System.Windows.Forms引用。

  1. 创建一个新的Windows Forms应用程序项目。
  2. 在解决方案资源管理器中,双击Form1以打开设计器。
  3. 从工具箱中,将一个Button控件拖放到Form1上。
  4. 双击Button以创建button1_Click事件处理程序。
  5. Form1类中,添加以下代码:
using System; using System.Windows.Forms; public partial class Form1 : Form { private Form childForm; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (childForm == null) { childForm = new Form(); childForm.Size = new System.Drawing.Size(200, 200); childForm.StartPosition = FormStartPosition.Manual; childForm.Location = new System.Drawing.Point(this.Location.X + this.Width, this.Location.Y); childForm.FormClosed += ChildForm_FormClosed; } if (childForm.Visible) { HideChildForm(); } else { ShowChildForm(); } } private void ShowChildForm() { childForm.Show(); Timer timer = new Timer(); timer.Interval = 10; timer.Tick += (sender, e) => { if (childForm.Width < 200) { childForm.Width += 20; childForm.Left -= 10; } else { timer.Stop(); } }; timer.Start(); } private void HideChildForm() { Timer timer = new Timer(); timer.Interval = 10; timer.Tick += (sender, e) => { if (childForm.Width > 0) { childForm.Width -= 20; childForm.Left += 10; } else { childForm.Hide(); timer.Stop(); } }; timer.Start(); } private void ChildForm_FormClosed(object sender, FormClosedEventArgs e) { childForm = null; } } 

WPF 示例:

首先,确保你已经添加了PresentationFrameworkSystem.Windows引用。

  1. 创建一个新的WPF应用程序项目。
  2. 在解决方案资源管理器中,双击MainWindow.xaml以打开设计器。
  3. MainWindow.xaml中,添加以下代码:
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="200"> <Grid> <Button Content="Toggle Child Window" Click="Button_Click"/> </Grid> </Window> 
  1. MainWindow.xaml.cs中,添加以下代码:
using System; using System.Windows; using System.Windows.Media.Animation; namespace WpfApp { public partial class MainWindow : Window { private Window childWindow; public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { if (childWindow == null) { childWindow = new Window(); childWindow.Title = "Child Window"; childWindow.Width = 200; childWindow.Height = 200; childWindow.WindowStartupLocation = WindowStartupLocation.Manual; childWindow.Left = this.Left + this.Width; childWindow.Top = this.Top; childWindow.Closed += ChildWindow_Closed; } if (childWindow.Visibility == Visibility.Visible) { HideChildWindow(); } else { ShowChildWindow(); } } private void ShowChildWindow() { DoubleAnimation widthAnimation = new DoubleAnimation(0, 200, TimeSpan.FromMilliseconds(200)); DoubleAnimation leftAnimation = new DoubleAnimation(childWindow.Left - 200, childWindow.Left, TimeSpan.FromMilliseconds(200)); childWindow.BeginAnimation(Window.WidthProperty, widthAnimation); childWindow.BeginAnimation(Window.LeftProperty, leftAnimation); childWindow.Show(); } private void HideChildWindow() { DoubleAnimation widthAnimation = new DoubleAnimation(200, 0, TimeSpan.FromMilliseconds(200)); DoubleAnimation leftAnimation = new DoubleAnimation(childWindow.Left, childWindow.Left + 200, TimeSpan.FromMilliseconds(200)); childWindow.BeginAnimation(Window.WidthProperty, widthAnimation); childWindow.BeginAnimation(Window.LeftProperty, leftAnimation); childWindow.Visibility = Visibility.Hidden; } private void ChildWindow_Closed(object sender, EventArgs e) { childWindow = null; } } } 

这些示例将创建一个主窗口,其中包含一个按钮。当单击该按钮时,将显示或隐藏子窗口,同时使用动画效果展开或收起子窗口。

0