在C#中,你可以使用Windows Forms或WPF來創(chuàng)建子窗口并實現(xiàn)彈出和隱藏動畫。這里我將分別為這兩種技術(shù)提供一個示例。
Windows Forms 示例:
首先,確保你已經(jīng)添加了System.Windows.Forms
引用。
Form1
以打開設計器。Button
控件拖放到Form1
上。Button
以創(chuàng)建button1_Click
事件處理程序。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 示例:
首先,確保你已經(jīng)添加了PresentationFramework
和System.Windows
引用。
MainWindow.xaml
以打開設計器。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>
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;
}
}
}
這些示例將創(chuàng)建一個主窗口,其中包含一個按鈕。當單擊該按鈕時,將顯示或隱藏子窗口,同時使用動畫效果展開或收起子窗口。