zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }

    完成了,貌似挺简单。

  • 相关阅读:
    章节1:SQL语言简易入门
    章节0:MySQl学前知识储备
    iOS 设置导航栏全透明
    IOS修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
    iOS import导入时没有提示的解决办法
    iOSAPP开发项目搭建
    如何搭建iOS项目基本框架
    UIWebView中JS与OC交互 WebViewJavascriptBridge的使用
    iOS概念之KVO(Key-Value Observing)
    oc调javascript方法(evaluateJavaScript:)&&js给oc发通知
  • 原文地址:https://www.cnblogs.com/mrhgw/p/1697168.html
Copyright © 2011-2022 走看看