Wpf: a simple object movement Animation

Source: Internet
Author: User

[Html]
<Window x: Class = "Wpfdemo1.MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525" Loaded = "Window_Loaded" MouseLeftButtonDown = "Window_MouseLeftButtonDown">
<Canvas x: Name = "body">



</Canvas>
</Window>

[Csharp]
/// <Summary>
/// Interaction logic of MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Ellipse ell;
 
Public MainWindow ()
{
InitializeComponent ();
 
Ell = new Ellipse ();
 
Ell. Fill = new SolidColorBrush (Colors. Red );
Ell. Width = 50;
Ell. Height = 50;
 
Body. Children. Add (ell );
 
Canvas. SetLeft (well, 100 );
Canvas. SetTop (well, 100 );
 
}
 
Private void Window_Loaded (object sender, RoutedEventArgs e)
{

}
 
Private void Window_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
MoveTo (e. GetPosition (body ));
}
 
 
Private void moveTo (Point region Point)
{
// Point p = e. GetPosition (body );
 
Point curPoint = new Point ();
CurPoint. X = Canvas. GetLeft (ell );
CurPoint. Y = Canvas. GetTop (ell );
 
Double _ s = System. math. sqrt (Math. pow (vertex point. x-curPoint. x), 2) + Math. pow (vertex point. y-curPoint. y), 2 ));
 
Double _ secNumber = (_ s/1000) * 500;
 
Storyboard storyboard = new Storyboard ();
 
// Create an animation in the X axis direction
 
DoubleAnimation doubleAnimation = new DoubleAnimation (
 
Canvas. GetLeft (ell ),
 
Vertex point. X,
 
New Duration (TimeSpan. FromMilliseconds (_ secNumber ))
 
);
Storyboard. SetTarget (doubleAnimation, ell );
Storyboard. SetTargetProperty (doubleAnimation, new PropertyPath ("(Canvas. Left )"));
Storyboard. Children. Add (doubleAnimation );
 
// Create an animation in the Y axis
 
DoubleAnimation = new DoubleAnimation (
Canvas. GetTop (ell ),
Vertex point. Y,
New Duration (TimeSpan. FromMilliseconds (_ secNumber ))
);
Storyboard. SetTarget (doubleAnimation, ell );
Storyboard. SetTargetProperty (doubleAnimation, new PropertyPath ("(Canvas. Top )"));
Storyboard. Children. Add (doubleAnimation );
 
 
 
// Play the animation
 
Storyboard. Begin ();
}
}

The storyboard animation function allows you to move elements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.