html5教程-Wpf一个简单的物体移动动画

发布时间:2018-12-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了html5教程-Wpf一个简单的物体移动动画脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

[html]
<Window x:Class="Wpfdemo1.MainWindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://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> 
    /// 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(ell, 100); 
            Canvas.SetTop(ell,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 deskPoint) 
        { 
            //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((deskPoint.X - curPoint.X), 2) + Math.Pow((deskPoint.Y - curPoint.Y), 2)); 
 
            double _secNumber = (_s / 1000) * 500; 
 
            Storyboard storyboard = new Storyboard(); 
 
            //创建X轴方向动画 
 
            DoubleAnimation doubleAnimation = new DoubleAnimation( 
 
              Canvas.GetLeft(ell), 
 
              deskPoint.X, 
 
              new Duration(TimeSpan.FromMilliseconds(_secNumber)) 
 
            ); 
            Storyboard.SetTarget(doubleAnimation, ell); 
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)")); 
            storyboard.Children.Add(doubleAnimation); 
 
            //创建Y轴方向动画 
 
            doubleAnimation = new DoubleAnimation( 
              Canvas.GetTop(ell), 
              deskPoint.Y, 
              new Duration(TimeSpan.FromMilliseconds(_secNumber)) 
            ); 
            Storyboard.SetTarget(doubleAnimation, ell); 
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)")); 
            storyboard.Children.Add(doubleAnimation); 
 
 
 
            //动画播放 
 
            storyboard.Begin(); 
        } 
    } 

通过动画析storyboard实现元素移动功能。

[html]
<Window x:Class="Wpfdemo1.MainWindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://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> 
    /// 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(ell, 100); 
            Canvas.SetTop(ell,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 deskPoint) 
        { 
            //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((deskPoint.X - curPoint.X), 2) + Math.Pow((deskPoint.Y - curPoint.Y), 2)); 
 
            double _secNumber = (_s / 1000) * 500; 
 
            Storyboard storyboard = new Storyboard(); 
 
            //创建X轴方向动画 
 
            DoubleAnimation doubleAnimation = new DoubleAnimation( 
 
              Canvas.GetLeft(ell), 
 
              deskPoint.X, 
 
              new Duration(TimeSpan.FromMilliseconds(_secNumber)) 
 
            ); 
            Storyboard.SetTarget(doubleAnimation, ell); 
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)")); 
            storyboard.Children.Add(doubleAnimation); 
 
            //创建Y轴方向动画 
 
            doubleAnimation = new DoubleAnimation( 
              Canvas.GetTop(ell), 
              deskPoint.Y, 
              new Duration(TimeSpan.FromMilliseconds(_secNumber)) 
            ); 
            Storyboard.SetTarget(doubleAnimation, ell); 
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)")); 
            storyboard.Children.Add(doubleAnimation); 
 
 
 
            //动画播放 
 
            storyboard.Begin(); 
        } 
    } 

通过动画析storyboard实现元素移动功能。

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的html5教程-Wpf一个简单的物体移动动画全部内容,希望文章能够帮你解决html5教程-Wpf一个简单的物体移动动画所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:HTML