XAMLCode:
========================================================== =====
<Usercontrol X: class = "test_sl.mainpage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400">
<Grid X: Name = "LR" background = "blue">
<Stackpanel horizontalalignment = "Left"> </stackpanel>
<Rectangle Height = "50" horizontalalignment = "Left" name = "rectangle1" stroke = "{X: null} "strokethickness =" 1 "verticalalignment =" TOP "width =" 50 "fill =" red "margin =" 10, 10, 0, 0 "radiusx =" 5 "radiusy =" 5 ">
<Rectangle. rendertransform>
<Translatetransform X: Name = "TT" x = "0" Y = "0"/>
</Rectangle. rendertransform>
</Rectangle>
</GRID>
</Usercontrol>
CS code:
========================================================== =====
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Namespace test_sl
{
Public partial class mainpage: usercontrol
{
Public mainpage ()
{
Initializecomponent ();
This. myinit ();
}
Private void myinit ()
{
This. LR. mouseleftbuttondown + = new mousebuttoneventhandler (lr_mouseleftbuttondown );
}
Void lr_mouseleftbuttondown (Object sender, mousebuttoneventargs E)
{
Storyboard sb = new storyboard ();
// Obtain the current mouse position.
Point P = E. getposition (this. lr );
// horizontal motion animation.
doubleanimation Dax = new doubleanimation ()
{< br> to = P. x,
duration = timespan. frommilliseconds (500)
};
storyboard. settargetproperty (Dax, new propertypath ("X");
storyboard. settarget (Dax, this. TT);
Sb. children. add (Dax);
// an animation of vertical motion.
doubleanimation day = new doubleanimation ()
{< br> to = P. y,
duration = timespan. frommilliseconds (100)
};
storyboard. settargetproperty (day, new propertypath ("Y");
storyboard. settarget (day, this. TT);
Sb. children. add (day);
// Start the animation.
SB. Begin ();
}
}
}
It seems quite simple.