Click here to Skip to main content
11,489,693 members (70,043 online)
Click here to Skip to main content

(Desktop) Falling Snowflakes

, 3 Oct 2007 CPOL 35.3K 1.3K 38
Rate this:
Please Sign up or sign in to vote.
Using GDI+ and a transparent form
Screenshot - snow.jpg

Introduction

First of all, I have to say that this isn't my idea, it is more a portation of a C++ example to C# using a different way. If you want to get to the original article, click here. (Thanks to Igor Tolmachev for the nice idea.) I changed the way it works (from multiple dialogs to a single form) and added some new features (e.g. Rotation, Scaling, etc.).

Using the Program

Simply run the executable. To close the program, double click on the tray icon representing a snow flake.

Using the Code

The program itself uses a non-bordered form (which is resized to match screen resolution) with a transparent background color for drawing the snow flakes. Instead of using several forms/dialogs (I hate this idea), I just use one form on which I draw using GDI+.

 public MainForm() {
    InitializeComponent();

    //We paint our control ourself 
    //and need a double buffer to prevent flimmering
    SetStyle(
        ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
        ControlStyles.DoubleBuffer, true);

    //Resize and relocate form to match Screen settings
    Location = new Point(Screen.PrimaryScreen.Bounds.X, 
                 Screen.PrimaryScreen.Bounds.Y);
    Width = Screen.PrimaryScreen.Bounds.Width;
    Height = Screen.PrimaryScreen.Bounds.Height;
 }

The snowflakes themselves are saved in a List<SnowFlake>. Snowflake is a container class containing the current Data/Positions of the snowflake (X, Y, Rot, Scale) and the Velocities/Changes (XVel, YVel, RotVel)

The main function is the OnTick method, which is divided into several steps:

  1. Increasing the tick count (e.g. Adding snow flakes every tick would be a bit too much)
  2. Spawning new flakes (If appropriate conditions are given)
  3. Moving all flakes (by adding the velocities to the data values)
  4. Deleting flakes which are below the screen/taskbar
  5. Rendering the flakes

The flakes are drawn by defining a userpaint method MainForm_Paint and calling Refresh() to cause the form to invalidate itself. Refresh() is required, as it supports double buffers, which MainForm_Paint(CreateGraphics()) would not. In this case, I used the Matrix capabilities of GDI+ to rotate and scale the snowflakes.

 foreach (SnowFlake s in SnowFlakes) {
    g.ResetTransform();
    g.TranslateTransform(-16, -16, MatrixOrder.Append); //Align our flakes to the center
    g.ScaleTransform(s.Scale, s.Scale, MatrixOrder.Append); //Scale them..
    g.RotateTransform(s.Rotation, MatrixOrder.Append); //Rotate them..
    //Move them to their appropriate location    
    g.TranslateTransform(s.X, s.Y, MatrixOrder.Append); 
    g.DrawImage(Snow, 0, 0); //draw them
 }

The snowflake image is drawn by using the GDI+ functions and then cached at a 32x32 pixel size for performance reasons. (GDI+ is slow when you draw lots of lines.)

Customize!

You could get some pretty nice results by just increasing the snow flake spawn count/the velocities. But note that GDI+ is not as fast at it may look and it increases CPU usage (especially when using matrices).

History

  • 3rd October, 2007: Initial post

License

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

Share

About the Author

]Metty[

Germany Germany
No Biography provided

Comments and Discussions

 
GeneralHey, Question... Pin
two1frets28-Sep-08 5:21
membertwo1frets28-Sep-08 5:21 
Generalgreat work :) Pin
paskud23-Jan-08 5:00
memberpaskud23-Jan-08 5:00 
Questiondeleting snowflakes Pin
jblanchet8110-Oct-07 7:46
memberjblanchet8110-Oct-07 7:46 
AnswerRe: deleting snowflakes Pin
]Metty[10-Oct-07 10:55
member]Metty[10-Oct-07 10:55 
GeneralThanks for the continue Pin
Igor Tolmachev10-Oct-07 6:12
memberIgor Tolmachev10-Oct-07 6:12 
GeneralCPU load [modified] Pin
zhaojicheng4-Oct-07 4:45
memberzhaojicheng4-Oct-07 4:45 
GeneralRe: CPU load Pin
.andy.b.6-Dec-08 3:29
member.andy.b.6-Dec-08 3:29 
GeneralNicely done. Pin
James Kolpack3-Oct-07 3:47
memberJames Kolpack3-Oct-07 3:47 
GeneralChristmas is coming early this year... Pin
ESTANNY3-Oct-07 2:45
memberESTANNY3-Oct-07 2:45 
GeneralRe: Christmas is coming early this year... Pin
]Metty[3-Oct-07 3:13
member]Metty[3-Oct-07 3:13 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 3 Oct 2007
Article Copyright 2007 by ]Metty[
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid