溫馨提示×

利用C#實現(xiàn)子窗口的彈出與隱藏動畫

c#
小樊
121
2024-09-04 16:13:17
欄目: 編程語言

在C#中,你可以使用Windows Forms或WPF來創(chuàng)建子窗口并實現(xiàn)彈出和隱藏動畫。這里我將分別為這兩種技術(shù)提供一個示例。

Windows Forms 示例:

首先,確保你已經(jīng)添加了System.Windows.Forms引用。

  1. 創(chuàng)建一個新的Windows Forms應用程序項目。
  2. 在解決方案資源管理器中,雙擊Form1以打開設計器。
  3. 從工具箱中,將一個Button控件拖放到Form1上。
  4. 雙擊Button以創(chuàng)建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 示例:

首先,確保你已經(jīng)添加了PresentationFrameworkSystem.Windows引用。

  1. 創(chuàng)建一個新的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;
        }
    }
}

這些示例將創(chuàng)建一個主窗口,其中包含一個按鈕。當單擊該按鈕時,將顯示或隱藏子窗口,同時使用動畫效果展開或收起子窗口。

0