WPF 跟随鼠标动画 by wgscd

WPF 跟随鼠标动画 by wgscd

<UserControl x:Class="WpfApplication1.Spark"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             mc:Ignorable="d" 

            x:Name="ctl" d:DesignHeight="60" d:DesignWidth="60">

    

    

    <UserControl.Resources>

 

        <Duration  x:Key="duration">0:0:0.8</Duration>

        

        <Storyboard x:Key="sb1" x:Name="sb1" Completed="Storyboard_Completed">

            <DoubleAnimation  From="1" To="0" Duration="0:0:0.8" Storyboard.TargetName="t1" Storyboard.TargetProperty="ScaleX"></DoubleAnimation>

            <DoubleAnimation  From="1" To="0" Duration="0:0:0.8" Storyboard.TargetName="t1" Storyboard.TargetProperty="ScaleY"></DoubleAnimation>

            <DoubleAnimation  From="0" To="-20" Duration="0:0:0.8" Storyboard.TargetName="t2" Storyboard.TargetProperty="X"></DoubleAnimation>

            <DoubleAnimation  From="1" To="0" Duration="0:0:0.8" Storyboard.TargetName="ctl" Storyboard.TargetProperty="Opacity"></DoubleAnimation>

        </Storyboard>

    </UserControl.Resources>

    

    <Grid>

        <Ellipse Fill="White" RenderTransformOrigin="0.5,0.5">

            <Ellipse.RenderTransform>

                <TransformGroup>

                    <ScaleTransform x:Name="t1" ScaleX="1" ScaleY="1"/>

                    <SkewTransform/>

                    <RotateTransform Angle="0"/>

                    <TranslateTransform x:Name="t2"  Y="0" X="0"/>

                   

                </TransformGroup>

                

            </Ellipse.RenderTransform>

        </Ellipse>

    </Grid>

</UserControl>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Spark.xaml 的交互逻辑
    /// </summary>
    public partial class Spark : UserControl
    {
        public Spark()
        {
            InitializeComponent();
            this.Width = this.Height = rnd.Next(23, 80);

        }
        Random rnd = new Random();
        public Spark(Point p)
        {
            InitializeComponent();
            this.Width = this.Height = rnd.Next(23,80);
            Canvas.SetLeft(this,p.X);
            Canvas.SetTop(this,p.Y);
           (Resources["sb1"] as Storyboard).Begin();
            
           
        }


        private void Storyboard_Completed(object sender, EventArgs e)
        {
            try
            {
                (Parent as Canvas).Children.Remove(this);
          
                //GC.Collect();
            }

            catch { }


        }









    }
}

  

 

 

 

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="384" Width="631">

    <Grid>

        <Canvas x:Name="myCanvas" Background="#FF61C1F7" ManipulationDelta="myCanvas_ManipulationDelta" MouseMove="myCanvas_MouseMove"></Canvas>

    </Grid>

</Window>

------------------------------------------------------------​

        private void myCanvas_MouseMove(object sender, MouseEventArgs e)

        {

            myCanvas.Children.Add(new Spark(e.GetPosition(myCanvas)));

           //  myCanvas.Children.Add(new Spark());

            Title = "" + myCanvas.Children.Count;

        }

 

转载于:https://www.cnblogs.com/wgscd/p/4469427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值