我自己写的代码,实现的功能是鼠标点到哪,圆就走到哪,但是,问题是 我不知道怎么定义圆的位置,我写的代码如下,高手帮我看看啊:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
Ellipse yuan;
public Window1()
{
InitializeComponent();
yuan = new Ellipse();
yuan.Fill = new SolidColorBrush(Colors.Red);
yuan.Width = 30;
yuan.Height = 30;
circle.Children.Add(yuan);
Canvas.SetLeft(yuan, 100);
Canvas.SetTop(yuan, 100);
}
private void circle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(yuan);
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleanimation = new DoubleAnimation(
Canvas.GetLeft(yuan),
p.X,
new Duration(TimeSpan.FromMilliseconds(50)));
Storyboard.SetTarget(doubleanimation, yuan);
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(doubleanimation);
doubleanimation = new DoubleAnimation(
Canvas.GetTop(yuan),
p.Y,
new Duration(TimeSpan.FromMilliseconds(50)));
Storyboard.SetTarget(doubleanimation, yuan);
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(doubleanimation);
if (!Resources.Contains("circleAnimation"))
{
Resources.Add("circleAnimation", storyboard);
}
//动画播放
storyboard.Begin();
}
}
}