wpf中在地图上两点之间产生连接线动画
前些日子编写电子地图程序,要在各个城市中间产生连接线,各城市点是Path类,开发代码如下:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14using System.Windows.Media.Animation;
15
16namespace GetPoint
17{
18 public partial class Window1 : Window
19 {
20 int i = 0;
21 Storyboard myLineAnimatedButtonStoryboard = new Storyboard();
22 public Window1()
23 {
24 NameScope.SetNameScope(this, new NameScope());
25 }
26 Point GetPoint(Path path)
27 {
28 Point OraginPoint = path.PointToScreen(new Point(0, 0));
29 Point FatherPoint = canvas.PointToScreen(new Point(0, 0));
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)
33 {
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;
46 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)
66 {
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)
75 {
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
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14using System.Windows.Media.Animation;
15
16namespace GetPoint
17{
18 public partial class Window1 : Window
19 {
20 int i = 0;
21 Storyboard myLineAnimatedButtonStoryboard = new Storyboard();
22 public Window1()
23 {
24 NameScope.SetNameScope(this, new NameScope());
25 }
26 Point GetPoint(Path path)
27 {
28 Point OraginPoint = path.PointToScreen(new Point(0, 0));
29 Point FatherPoint = canvas.PointToScreen(new Point(0, 0));
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)
33 {
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;
46 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)
66 {
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)
75 {
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