Desktop Snow Effect

Source: Internet
Author: User

A simple snow effect


1. The main interface generates snowflakes for the grid via DispatcherTimer

2. Snowflakes fall behind and say snowflakes removed from the grid container

  Public Partial classMainWindow { PublicMainWindow () {InitializeComponent (); Closing+ = (s, e) = =Viewmodellocator.cleanup (); Loaded+=mainwindow_loaded; }        Private voidMainwindow_loaded (Objectsender, RoutedEventArgs e) {            varTimer =NewDispatcherTimer {Interval= Timespan.frommilliseconds ( -)            }; Timer. Tick+=Timer_tick; Timer.        Start (); }        //Add Snowflakes        Private voidTimer_tick (Objectsender, EventArgs e)        {LayoutRoot.Children.Add (Createsnowflower ()); }        //Generate Snowflakes        Private Staticsnowflower Createsnowflower () {varSnowflower =NewSnowflower (); Snowflower.complete+ = args =            {                //Remove snowflakes from the parent container                varFlower = args asSnowflower; if(Flower = =NULL)return; varParent =visualtreehelper.getparent (flower); varPanel = Parent asPanel; if(Panel! =NULL&&panel. Children.contains (flower)) {panel.                Children.remove (flower);            }            }; returnSnowflower; }    }


1. Falling animation of snowflakes and rotating animation

2. Increase the animation completion event

3. Random starting point

4. Random rotation speed and drop speed

  /// <summary>    ///Snowflakes/// </summary>     Public classSnowflower:image {#regionField//full random number generator        Private ReadOnlyRandom _random =Randomhelper.getrandom (); #endregion        #regionProperty/// <summary>        ///Drop Speed/// </summary>         Public intGravityspeed {Get{return_random. Next (5,Ten); }        }        /// <summary>        ///rotational speed/// </summary>         Public intAnglespeed {Get{return_random. Next (1,5); }        }        /// <summary>        ///The x-coordinate of the start/// </summary>         Public intStartX {Get            {                return_random. Next (0, (int) systemparameters.primaryscreenwidth); }        }        #endregion         PublicSnowflower () {Opacity=getopacity (); Source=GetSource (); Height=GetSize (); Width=Height; Rendertransformorigin=NewPoint (0.5,0.5); Margin=NewThickness (StartX,0,0,0); HorizontalAlignment=HorizontalAlignment.Left;; RenderTransform=Getrendertransform (); Loaded+=snowflower_loaded; }        #regionAnimationPrivate voidSnowflower_loaded (Objectsender, RoutedEventArgs e) {Getfallingstoryborad ().        Begin (); }        //get information about a transformation        Private StaticTransformGroup getrendertransform () {varresult =NewTransformGroup (); Result. Children.add (NewRotateTransform ()); Result. Children.add (NewTranslateTransform ()); returnresult; }        //get the Falling animated story version        PrivateStoryboard Getfallingstoryborad () {varresult =NewStoryboard (); //Add rotate Animationresult.            Children.add (Getangleanimation ()); //Add drop Animation            varFallinganimation =getfallinganimation (); Result. Duration=fallinganimation.duration; //triggers the completion event when the drop is completeresult.completed + = (sender, args) =Raisecomplete (); Result.            Children.add (fallinganimation); returnresult; }        //attribute Chain        Private ReadOnly Object[] _propertychain ={rendertransformproperty, Transformgroup.childrenproperty, ROTATETRANSF Orm.        Angleproperty, Translatetransform.yproperty}; //Get rotation animation        PrivateDoubleAnimation getangleanimation () {varDa =NewDoubleAnimation { from=0, to= the, Duration=timespan.fromseconds (anglespeed), RepeatBehavior=Repeatbehavior.forever}; //Storyboard.settargetproperty (DA, New PropertyPath ("(Uielement.rendertransform)." ( TransformGroup.Children) [0]. (Rotatetransform.angle) "));Storyboard.settargetproperty (DA,NewPropertyPath ("(0). (1) [0]. (2)", _propertychain)); Storyboard.settarget (DA, This); returnda; }        //get the Falling animation        PrivateDoubleAnimation getfallinganimation () {varDa =NewDoubleAnimation { from=0, to=Systemparameters.primaryscreenheight, Duration=timespan.fromseconds (Gravityspeed)}; Storyboard.settargetproperty (DA,NewPropertyPath ("(Uielement.rendertransform). (TransformGroup.Children) [1]. (TRANSLATETRANSFORM.Y)")); Storyboard.settarget (DA, This); returnda; }        #endregion        //get the initial transparency        Private Static Doublegetopacity () {varresult = (Double)NewRandom (). Next (1,5); Result= result/Ten; returnresult; }        //Get Image source        Private StaticBitmapImage GetSource () {varresult =NewBitmapImage (); Result.            BeginInit (); Result. UriSource=NewUri ("/snoweffect;component/resource/snowflower.png", Urikind.relativeorabsolute); Result.            EndInit (); returnresult; }        //Get Dimensions        Private Static DoubleGetSize () {varresult = (Double)NewRandom (). Next ( -, -); returnresult; }        #regionCompletion events/// <summary>        ///Finish Processing Delegate/// </summary>        /// <param name= "Sender" ></param>         Public Delegate voidCompletehandler (Objectsender); /// <summary>        ///Completion Events/// </summary>         Public EventCompletehandler complete; /// <summary>        ///actions when event triggering is complete/// </summary>        protected Virtual voidOnComplete () {if(Complete! =NULL) {Complete ( This); }        }        /// <summary>        ///Raise Completion Event/// </summary>         Public voidRaisecomplete () {oncomplete (); }        #endregion    }


Desktop Snow Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.