Click here to Skip to main content
15,878,814 members
Articles / Multimedia / GDI+
Article

Falling Snow on Your Desktop! The C# Version

Rate me:
Please Sign up or sign in to vote.
4.61/5 (26 votes)
27 Dec 20062 min read 119.6K   4.4K   69   22
This article explains how to create an application that makes it snow on the desktop.

FallingSnow

Introduction

I posted my article Falling Snow on Your Desktop! just a week ago, and received many requests regarding a C# version of the application. So, the C# version is here.

Every time the Christmas holidays come, I think about funny toys for my desktop.

<ps>Searching through the Internet, you can easily find many wonderful desktop gifts for any occasion, including Christmas time. Unfortunately, I couldn't find any source code or open source sample of such desktop animations.

So, I decided to create my own application that makes it snow on the desktop over other windows. As I can see from the Internet, many authors use different ways to display snow fall on the desktop. I tried to create my application as simple as possible, and decided to develop a system tray application with numerous child flake windows. I think this method is the simplest, but has a few limitations regarding memory and video usage.

General Steps

  1. Creating the main window.

    We need to create a hidden form and prevent more than one open 'Snow' window at the same time. I used a piece of code from the article 'Restricting Application to a Single Instance' by Vasudevan Deepak Kumar. The following code fragment shows how to do this:

    C#
    using System.Threading;
    ...
    private static Mutex m_Mutex;
    ...
    m_Mutex = new Mutex(true, "SnowFallMutex");
    
    if (m_Mutex.WaitOne(0, false))
         Application.Run(new MainForm());
  2. Installing the system tray icon.

    If you need step-by-step instructions on how you can do this, read the 'C# Tip: Placing Your C# Application in the System Tray' article by Tom Archer.

  3. Creating the array of snow flakes.

    In this application, each snow flake has been created as a non-modal window form, with its own, timer-dependent, movement function.

    C#
    private void OnLoad(object sender, EventArgs e)
    {
        Rectangle rcWorkArea = Screen.PrimaryScreen.WorkingArea;
        int nScreenWidth = rcWorkArea.Width;
    
        int nTimer = 5;
        int nPosX = 0;
    
        Random r = new Random();
        for (int i = 0; i < 10; i++)
        {
            nTimer = r.Next(50);
            nPosX = r.Next(nScreenWidth);
    
            FlakeDlg fd = new FlakeDlg(nTimer, nPosX);
            fd.Show();
        }
    }
  4. Creating a flake shape from the bitmap.

    Before running the window, we need to create a flake region that shows the window as a real snow flake. I used the 'BitmapRegion.CreateControlRegion' function from the article of Weiye Chen.

  5. The last step: start the timer and enjoy.

    The last thing to do is start the timer and see how the flakes move. I used a very simple algorithm below, but it is sufficient to show thr snow movement on the desktop.

    C#
    private void OnTimer(object sender, EventArgs e)
    {
        this.timerMove.Stop();
    
        m_nCurrentY += 5;
        m_nCounter++;
    
        Random r = new Random();
        if (m_nCounter == 15)
        {
            if ((r.Next(10) - 5) > 0) m_nIncrement = 1;
            else m_nIncrement = -1;
    
            m_nCounter = 0;
        }
    
        m_nCurrentX += m_nIncrement;
    
        if (m_nCurrentY > m_nScreenHeight)
        {
            m_nCurrentY = 0;
            m_nCurrentX = r.Next(m_nScreenWidth);
            m_nTimer = r.Next(50) + 10;
        }
    
        this.Left = m_nCurrentX;
        this.Top = m_nCurrentY;
    
        this.timerMove.Interval = m_nTimer;
        this.timerMove.Start();
    }

This example can be extended by drawing snowdrifts on the desktop, and displaying Santa, Snowman, and other objects. You can experiment with the flakes positioning (top, topmost, bottom), the number of flakes, the falling speed etc.

Anyway, I hope others find this code useful for the Christmas holidays. Please feel free to report errors, issues, or requests.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood Post Pin
Bhavesh Patel11-Jan-16 19:03
Bhavesh Patel11-Jan-16 19:03 
GeneralYou are a real smart guy! Pin
Member 113253493-Jan-15 23:01
Member 113253493-Jan-15 23:01 
Questionthnx Pin
Rohit Dubey from Hyderabad9-Mar-12 7:01
Rohit Dubey from Hyderabad9-Mar-12 7:01 
Questionokay if I convert this to Visual COBOL? Pin
Scot Nielsen16-Dec-10 3:32
Scot Nielsen16-Dec-10 3:32 
GeneralCPU Pin
blahxblitzed4-Oct-07 22:34
blahxblitzed4-Oct-07 22:34 
GeneralNew Dialogs & CPU/Memory Issues Pin
JurkMonkey230-Jan-07 18:45
JurkMonkey230-Jan-07 18:45 
GeneralRe: New Dialogs & CPU/Memory Issues Pin
Igor Tolmachev31-Jan-07 2:28
Igor Tolmachev31-Jan-07 2:28 
QuestionRe: New Dialogs & CPU/Memory Issues Pin
blahxblitzed4-Oct-07 22:36
blahxblitzed4-Oct-07 22:36 
QuestionRe: New Dialogs & CPU/Memory Issues Pin
Neo7772-Nov-07 23:23
Neo7772-Nov-07 23:23 
QuestionBehind icons? Pin
Soykaf4-Jan-07 5:47
Soykaf4-Jan-07 5:47 
GeneralThanks! Pin
PreciousPJ4-Jan-07 4:27
PreciousPJ4-Jan-07 4:27 
GeneralCPU load Pin
Simone Busoli3-Jan-07 2:37
Simone Busoli3-Jan-07 2:37 
GeneralRe: CPU load Pin
Igor Tolmachev3-Jan-07 5:41
Igor Tolmachev3-Jan-07 5:41 
GeneralMulit-monitor support Pin
Benjamin Liedblad2-Jan-07 16:31
Benjamin Liedblad2-Jan-07 16:31 
Cute. Mom will love it...

Recommendations:

1) Muli-monitor support.

2) Convert to a screen saver? Best example I've seen is: http://www.codeproject.com/csharp/scrframework.asp

Or user activity timeout that starts the snow / accumulation.

3) Modify settings via system tray icon - ("Light", "Heavy","Blizzard"), accumulation ON/OFF etc.

I'd love to help and can to convert to VB for all the VB folks. Let me know.
GeneralRe: Mulit-monitor support Pin
Igor Tolmachev3-Jan-07 5:24
Igor Tolmachev3-Jan-07 5:24 
GeneralRe: Mulit-monitor support Pin
UltronX14-Jan-09 0:43
UltronX14-Jan-09 0:43 
GeneralNice work Pin
--=A J E E S H=--27-Dec-06 22:35
--=A J E E S H=--27-Dec-06 22:35 
GeneralRe: Nice work Pin
Igor Tolmachev3-Jan-07 5:13
Igor Tolmachev3-Jan-07 5:13 
GeneralNice. Enhancement: reduce flicker Pin
Yoong Hor Meng27-Dec-06 17:34
Yoong Hor Meng27-Dec-06 17:34 
GeneralRe: Nice. Enhancement: reduce flicker Pin
Igor Tolmachev3-Jan-07 5:12
Igor Tolmachev3-Jan-07 5:12 
GeneralNice work Pin
Muhadeeb9927-Dec-06 9:55
Muhadeeb9927-Dec-06 9:55 
GeneralRe: Nice work Pin
Igor Tolmachev3-Jan-07 5:13
Igor Tolmachev3-Jan-07 5:13 

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.