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;

        }

 

你可能感兴趣的:(WPF)