wpf中在地图上两点之间产生连接线动画

前些日子编写电子地图程序,要在各个城市中间产生连接线,各城市点是Path类,开发代码如下:


  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Linq;
  4 using  System.Text;
  5 using  System.Windows;
  6 using  System.Windows.Controls;
  7 using  System.Windows.Data;
  8 using  System.Windows.Documents;
  9 using  System.Windows.Input;
 10 using  System.Windows.Media;
 11 using  System.Windows.Media.Imaging;
 12 using  System.Windows.Navigation;
 13 using  System.Windows.Shapes;
 14 using  System.Windows.Media.Animation;
 15
 16 namespace  GetPoint
 17 ExpandedBlockStart.gifContractedBlock.gif {
 18    public partial class Window1 : Window
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 20        int i = 0;
 21        Storyboard myLineAnimatedButtonStoryboard = new Storyboard();
 22        public Window1()
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 24            NameScope.SetNameScope(thisnew NameScope());
 25        }

 26        Point GetPoint(Path path)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 28            Point OraginPoint = path.PointToScreen(new Point(00));
 29            Point FatherPoint = canvas.PointToScreen(new Point(00));
 30            return (new Point(OraginPoint.X-FatherPoint.X+path.ActualWidth/2,OraginPoint.Y-FatherPoint.Y+path.ActualHeight/2));
 31        }

 32        void GetLineAnimation(Path startPP, Path endPP)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 34            Point startP = GetPoint(startPP);
 35            Point endP = GetPoint(endPP);
 36            Line myLine = new Line();
 37            string name = "myLine" + startPP.Name+endPP.Name;
 38            i++;
 39            myLine.Name = name;
 40            myLine.X1 = startP.X;
 41            myLine.Y1 = startP.Y;
 42            myLine.X2 = startP.X;
 43            myLine.Y2 = startP.Y;
 44            myLine.Stroke = Brushes.LightSteelBlue;
 45            myLine.StrokeThickness = 2;
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            this.RegisterName(name, myLine);/**/////
 47            canvas.Children.Add(myLine);
 48            DoubleAnimation myLineXAnimation = new DoubleAnimation();
 49            myLineXAnimation.From = startP.X;
 50            myLineXAnimation.To = endP.X;
 51            myLineXAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
 52            Storyboard.SetTargetName(myLineXAnimation, name);
 53            Storyboard.SetTargetProperty(myLineXAnimation, new PropertyPath(Line.X2Property));
 54            DoubleAnimation myLineYAnimation = new DoubleAnimation();
 55            myLineYAnimation.From = startP.Y;
 56            myLineYAnimation.To = endP.Y;
 57            myLineYAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
 58            Storyboard.SetTargetName(myLineYAnimation, name);
 59            Storyboard.SetTargetProperty(myLineYAnimation, new PropertyPath(Line.Y2Property));
 60            myLineAnimatedButtonStoryboard.Children.Add(myLineXAnimation);
 61            myLineAnimatedButtonStoryboard.Children.Add(myLineYAnimation);
 62            myLineAnimatedButtonStoryboard.Begin(this);
 63        }

 64
 65        public void EventHandler(Object sender,EventArgs e)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 67            //Point startP = GetPoint(startPP);
 68            //Point endP = GetPoint(endPP);
 69            GetLineAnimation(startPP, endPP);
 70            //Point endP2 = GetPoint(endPP2);
 71            GetLineAnimation(startPP, endPP2);
 72        }

 73
 74        private void button1_Click(object sender, RoutedEventArgs e)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 76            Line myLine = this.FindName("myLinestartPPendPP"as Line;
 77            Point startP = GetPoint(startPP);
 78            Point endP = GetPoint(endPP);
 79            myLine.X1 = startP.X;
 80            myLine.Y1 = startP.Y;
 81            myLine.X2 = startP.X;
 82            myLine.Y2 = startP.Y;
 83            myLine.Stroke = Brushes.Red;
 84            myLine.StrokeThickness = 2;
 85            DoubleAnimation myLineXAnimation = new DoubleAnimation();
 86            myLineXAnimation.From = startP.X;
 87            myLineXAnimation.To = endP.X;
 88            myLineXAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
 89            Storyboard.SetTargetName(myLineXAnimation, "myLinestartPPendPP");
 90            Storyboard.SetTargetProperty(myLineXAnimation, new PropertyPath(Line.X2Property));
 91            DoubleAnimation myLineYAnimation = new DoubleAnimation();
 92            myLineYAnimation.From = startP.Y;
 93            myLineYAnimation.To = endP.Y;
 94            myLineYAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
 95            Storyboard.SetTargetName(myLineYAnimation, "myLinestartPPendPP");
 96            Storyboard.SetTargetProperty(myLineYAnimation, new PropertyPath(Line.Y2Property));
 97            myLineAnimatedButtonStoryboard.Children.Clear();
 98            myLineAnimatedButtonStoryboard.Children.Add(myLineXAnimation);
 99            myLineAnimatedButtonStoryboard.Children.Add(myLineYAnimation);
100            myLineAnimatedButtonStoryboard.Begin(this);
101        }

102    }

103}

104

转载于:https://www.cnblogs.com/luyingxue/archive/2008/08/30/1279923.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值