1. 程式人生 > >WPF的簡單關閉動畫特效

WPF的簡單關閉動畫特效

/// <summary>
 /// Window10.xaml 的互動邏輯
 /// </summary>
 public partial class Window10 : Window
 {
  public Window10()
  {
   this.InitializeComponent();
   
   // 在此點之下插入建立物件所需的程式碼。
  }
        bool _closinganimation = true;
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = _closinganimation;
            _closinganimation = false;
           // base.OnClosing(e);

            System.Windows.Media.Animation.Storyboard sb = new System.Windows.Media.Animation.Storyboard();
            System.Windows.Media.Animation.DoubleAnimation dh = new System.Windows.Media.Animation.DoubleAnimation();
            System.Windows.Media.Animation.DoubleAnimation dw = new System.Windows.Media.Animation.DoubleAnimation();
            System.Windows.Media.Animation.DoubleAnimation dop = new System.Windows.Media.Animation.DoubleAnimation();
            dop.Duration = dh.Duration = dw.Duration = sb.Duration = new Duration(new TimeSpan(0, 0, 2));
            dop.To = dh.To = dw.To = 0;
            System.Windows.Media.Animation.Storyboard.SetTarget(dop, this);
            System.Windows.Media.Animation.Storyboard.SetTarget(dh, this);
            System.Windows.Media.Animation.Storyboard.SetTarget(dw, this);
            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dop, new PropertyPath("Opacity", new object[] { }));
            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dh, new PropertyPath("Height", new object[] { }));
            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dw, new PropertyPath("Width", new object[] { }));
            sb.Children.Add(dh);
            sb.Children.Add(dw);
            sb.Children.Add(dop);
            sb.Completed += new EventHandler(sb_Completed); //(a, b) => { this.Close(); };
            sb.Begin();
           
        }

        void sb_Completed(object sender, EventArgs e)
        {
             this.Close();
        }
 }