<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace DemoGridAni
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private bool _ISACTIVE;
public Window1()
{
InitializeComponent();
}
private double rect = 10;
private int dotime = 0;
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_ISACTIVE)
{
_ISACTIVE = true;
Doani();
}
}
private void Doani()
{
Console.WriteLine("Doani");
DoubleAnimation da1 = new DoubleAnimation();
da1.From = this.Top;
da1.To = this.Top + rect;
da1.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 15));
da1.Completed += new EventHandler(da1_Completed);
this.BeginAnimation(Window.TopProperty, da1);
}
void da1_Completed(object sender, EventArgs e)
{
Console.WriteLine("da1_Completed");
DoubleAnimation da2 = new DoubleAnimation();
da2.From = this.Left;
da2.To = this.Left + rect;
da2.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 15));
da2.Completed += new EventHandler(da2_Completed);
this.BeginAnimation(Window.LeftProperty, da2);
}
void da2_Completed(object sender, EventArgs e)
{
DoubleAnimation da3 = new DoubleAnimation();
da3.From = this.Top;
da3.To = this.Top - rect;
da3.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 15));
da3.Completed += new EventHandler(da3_Completed);
this.BeginAnimation(Window.TopProperty, da3);
}
void da3_Completed(object sender, EventArgs e)
{
DoubleAnimation da4 = new DoubleAnimation();
da4.From = this.Left;
da4.To = this.Left - rect;
da4.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 15));
da4.Completed += new EventHandler(da4_Completed);
this.BeginAnimation(Window.LeftProperty, da4);
}
void da4_Completed(object sender, EventArgs e)
{
if (dotime > 7)
{
_ISACTIVE = false;
dotime = 0;
}
else
{
dotime++;
System.Threading.Thread.Sleep(50);
Doani();
}
}
}
}
</code>