Silverlight4学习笔记--方块鼠标跟随
2010-03-26 17:02
274 查看
XAML代码:
=============================================
<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代码:
=============================================
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();
//获取当前鼠标位置。
Point p = e.GetPosition(this.lr);
//横向运动的动画。
DoubleAnimation daX = new DoubleAnimation()
{
To = p.X,
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTargetProperty(daX, new PropertyPath("X"));
Storyboard.SetTarget(daX, this.tt);
sb.Children.Add(daX);
//纵向运动的动画。
DoubleAnimation daY = new DoubleAnimation()
{
To = p.Y,
Duration = TimeSpan.FromMilliseconds(100)
};
Storyboard.SetTargetProperty(daY, new PropertyPath("Y"));
Storyboard.SetTarget(daY, this.tt);
sb.Children.Add(daY);
//开始动画。
sb.Begin();
}
}
}
完成了,貌似挺简单。
=============================================
<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代码:
=============================================
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();
//获取当前鼠标位置。
Point p = e.GetPosition(this.lr);
//横向运动的动画。
DoubleAnimation daX = new DoubleAnimation()
{
To = p.X,
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTargetProperty(daX, new PropertyPath("X"));
Storyboard.SetTarget(daX, this.tt);
sb.Children.Add(daX);
//纵向运动的动画。
DoubleAnimation daY = new DoubleAnimation()
{
To = p.Y,
Duration = TimeSpan.FromMilliseconds(100)
};
Storyboard.SetTargetProperty(daY, new PropertyPath("Y"));
Storyboard.SetTarget(daY, this.tt);
sb.Children.Add(daY);
//开始动画。
sb.Begin();
}
}
}
完成了,貌似挺简单。
相关文章推荐
- opencv Python学习笔记(三)调色板加跟随鼠标绘图
- Flash As3.0 学习笔记 第三课-鼠标事件-鼠标跟随
- Ferris教程学习笔记:js示例5.6 跟随鼠标移动(大图展示)
- 20171012学习笔记Selenium 2 第四章WebDriver API 4.2控制浏览器 4.3简单元素操作4.4鼠标事件4.5键盘事件
- css学习笔记(3)图文混排 分级 鼠标
- easyUI学习笔记之tab组件的鼠标事件
- 根据鼠标移动画线无法画出(学习笔记)
- Python学习笔记(十七)——用GUI自动化控制键盘鼠标
- [Hook]个人学习笔记——挂个简单的钩子(全局鼠标Hook)(.NET)
- 基于《Selenium 2自动化测试实战》的学习笔记(8)—— 鼠标事件
- jquery UI 跟随学习笔记——拖拽(Draggable)
- 蓝鸥Unity入门鼠标事件学习笔记
- MFC学习笔记之:Windows如何区分鼠标双击和两次单击
- unity3d 学习笔记___虚拟轴、鼠标事件、双击事件的应用处理
- C++学习笔记之鼠标绘图
- 【Visual C++】游戏编程学习笔记之八:鼠标输入消息(小demo)
- (源码实例)通过层DIV实现,当鼠标放在链接上面,显示图片及文字 - 流星絮语 JAVA学习笔记 - CSDNBlog
- Silverlight4学习笔记1--浏览器外运行(Out Of Browser)及更新检查
- Baxter学习笔记6-鼠标点动控制baxter机械臂--实战篇
- OpenCV学习笔记-用鼠标在窗口中画矩形