Silverlight4学习笔记--方块鼠标跟随
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();
}
}
}
完成了,貌似挺简单。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?