Click here to Skip to main content
15,885,876 members
Articles / Desktop Programming / WPF
Tip/Trick

Snowfall in WPF Application

Rate me:
Please Sign up or sign in to vote.
4.89/5 (8 votes)
16 Dec 2014CPOL 28.3K   1K   12   10
Snowfall animation in WPF application. Bring some winter to your application!

Introduction

Nowadays, it's very hard to find snowfall animation for WPF like in Stone age, but what if you want to give your users some winter or Christmas mood? Here is one of the simplest ways to do it.

and a real example:

Background

The principle is very simple - the code uses CompositionTarget.Rendering event and redraws snowflakes in new position with simple and understandable algorithm.

Using the Code

The usage is very simple too:

C#
private readonly SnowEngine snow = null;

public MainWindow()
{
    InitializeComponent();
    snow = new SnowEngine(canvas, "pack://application:,,,/Graphics/snow1.png",
       "pack://application:,,,/Graphics/snow2.png",
       "pack://application:,,,/Graphics/snow3.png",
       "pack://application:,,,/Graphics/snow4.png",
       "pack://application:,,,/Graphics/snow5.png",
       "pack://application:,,,/Graphics/snow6.png",
       "pack://application:,,,/Graphics/snow7.png",
       "pack://application:,,,/Graphics/snow8.png",
       "pack://application:,,,/Graphics/snow9.png");
    snow.Start();
}

Window or another control must have Canvas you pass to SnowEngine and images of flakes. It can be resources as shown above.

XML
<Grid>
    <Canvas Name="canvas"
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>

In addition, you can change some properties in the class:

  • Horizontal and vertical snow speed
  • Minimum and maximum radius of flakes to make them different
  • Percentage of snow coverage

Points of Interest

Pros

  • Easy to use
  • Pretty looking

Cons

  • Not stable animation speed because WPF renders it by his own mind depending on computer load and video card power

History

v1.0.0.1

  • Fixed SnowCoverage property don't set value
  • canvas.IsHitTestVisible = false sets in SnowEngine constructor

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAll snowflakes at top Pin
izSoftware3-Dec-16 1:30
izSoftware3-Dec-16 1:30 
PraiseGreat work, the only one that really Works fine. Pin
Member 121798881-Dec-15 16:59
Member 121798881-Dec-15 16:59 
QuestionClick Through Flakes and SnowCoverage Fix. Pin
Kim Honkaniemi18-Dec-14 1:15
Kim Honkaniemi18-Dec-14 1:15 
AnswerRe: Click Through Flakes and SnowCoverage Fix. Pin
RumataEstorish18-Dec-14 1:36
RumataEstorish18-Dec-14 1:36 
QuestionTurn it into a game Pin
Joel Palmer17-Dec-14 9:06
Joel Palmer17-Dec-14 9:06 
QuestionI'd be concerned... Pin
dandy7217-Dec-14 7:28
dandy7217-Dec-14 7:28 
AnswerRe: I'd be concerned... Pin
RumataEstorish17-Dec-14 7:31
RumataEstorish17-Dec-14 7:31 
GeneralRe: I'd be concerned... Pin
dandy7217-Dec-14 7:39
dandy7217-Dec-14 7:39 
Answercontrols under canvas do get mouse events Pin
Irina Pykhova17-Dec-14 1:42
professionalIrina Pykhova17-Dec-14 1:42 
GeneralRe: controls under canvas do get mouse events Pin
RumataEstorish17-Dec-14 1:49
RumataEstorish17-Dec-14 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.