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
{
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
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 GetPoint17
{18
public partial class Window1 : Window19
{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



浙公网安备 33010602011771号