Click here to Skip to main content
14,872,533 members
Articles / Multimedia / GDI
Posted 20 Dec 2006


101 bookmarked

Falling Snow on Your Desktop!

Rate me:
Please Sign up or sign in to vote.
4.91/5 (53 votes)
20 Dec 20062 min read
This article explains how to create an application that makes it snow on the desktop.



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

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 samples 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 frame window and prevent more than one open ‘Snow’ window at the same time. The following code fragment shows how to do this:

    BOOL CSnowApp::InitInstance()
        HWND hWnd = ::FindWindow("SnowParentWindow","Snow");
           return FALSE;
        CMainFrame* pFrame = new CMainFrame;
        m_pMainWnd = pFrame;
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
            static const TCHAR szClassName[] = 
            WNDCLASS wc;
            GetClassInfo(cs.hInstance, cs.lpszClass, &wc);
            wc.lpszClassName = szClassName;
            cs.lpszClass = szClassName;
  2. Installing the system tray icon.
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
        SetTrayIcon(NIM_ADD, m_hIcon, "Desktop Snow");
    BOOL CMainFrame::SetTrayIcon(DWORD dwMessage, 
                     HICON hIcon, PSTR pszTip)
        BOOL bResult = FALSE;
        tnd.cbSize = sizeof(NOTIFYICONDATA);
        tnd.hWnd = m_hWnd;
        tnd.uID = NULL;
        tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
        tnd.uCallbackMessage = WM_NOTIFYTRAY;
        tnd.hIcon = hIcon; 
           lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
           tnd.szTip[0] = '\0'; 
        bResult = Shell_NotifyIcon(dwMessage, &tnd); 
        return bResult;
  3. Creating the array of snow flakes.

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

    CRect rcWorkArea;
    int nScreenWidth = rcWorkArea.Width();
    int nTimer = 5;
    int nPosX = 0;
    for(int i=0;i<10;i++) // 10 snow flakes
        nTimer = abs(rand()*50/RAND_MAX);
        nPosX = abs(rand()*nScreenWidth/RAND_MAX);
        CFlakeDlg* fd = new CFlakeDlg(nTimer,nPosX,this);
  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 'BitmapToRegion' function from the article of Jean-Edouard Lachand-Robert, posted here.

  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 snow movement on the desktop.

    void CFlakeDlg::OnTimer(UINT nIDEvent) 
        if(nIDEvent == 1)
            m_nCurrentY += 5;
            if(m_nCounter == 15)
                if(((rand()*10/RAND_MAX)-5)>0) m_nIncrement = 1;
                else m_nIncrement = -1;
                m_nCounter = 0;
            m_nCurrentX += m_nIncrement;
                m_nCurrentY = 0;
                m_nCurrentX = abs(rand()*m_nScreenWidth/RAND_MAX);
                m_nTimer = abs(rand()*50/RAND_MAX);

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

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


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


About the Author

Igor Tolmachev
Software Developer (Senior)
Ukraine Ukraine
No Biography provided

Comments and Discussions

GeneralFalling Snow Pin
Tigrisan21-Dec-06 10:23
MemberTigrisan21-Dec-06 10:23 
GeneralRe: Falling Snow Pin
Igor Tolmachev21-Dec-06 22:44
MemberIgor Tolmachev21-Dec-06 22:44 
GeneralProblem of Compil under VS2002 and later Pin
df49421-Dec-06 6:07
Memberdf49421-Dec-06 6:07 
GeneralRe: Problem of Compil under VS2002 and later Pin
Igor Tolmachev21-Dec-06 6:23
MemberIgor Tolmachev21-Dec-06 6:23 
GeneralRe: Problem of Compil under VS2002 and later Pin
df49421-Dec-06 6:26
Memberdf49421-Dec-06 6:26 
GeneralRe: Problem of Compil under VS2002 and later Pin
Igor Tolmachev21-Dec-06 6:28
MemberIgor Tolmachev21-Dec-06 6:28 
GeneralRe: Problem of Compil under VS2002 and later Pin
df49421-Dec-06 6:25
Memberdf49421-Dec-06 6:25 
JokeHi Pin
Hugo PEREIRA21-Dec-06 0:52
MemberHugo PEREIRA21-Dec-06 0:52 
GeneralRe: Hi Pin
Igor Tolmachev21-Dec-06 6:24
MemberIgor Tolmachev21-Dec-06 6:24 
Generalhelp me pls Pin
kinnuP20-Dec-06 22:26
MemberkinnuP20-Dec-06 22:26 
GeneralRe: help me pls Pin
Igor Tolmachev21-Dec-06 6:26
MemberIgor Tolmachev21-Dec-06 6:26 
GeneralRe: help me pls - C# code is here Pin
Igor Tolmachev22-Dec-06 9:38
MemberIgor Tolmachev22-Dec-06 9:38 
QuestionCan I develop C# version of this... Pin
aamironline20-Dec-06 20:23
Memberaamironline20-Dec-06 20:23 
AnswerRe: Can I develop C# version of this... Pin
Igor Tolmachev20-Dec-06 21:14
MemberIgor Tolmachev20-Dec-06 21:14 
GeneralRe: Can I develop C# version of this... Pin
tickko0621-Dec-06 6:14
Membertickko0621-Dec-06 6:14 
GeneralRe: Can I develop C# version of this... Pin
Heywood22-Dec-06 4:51
MemberHeywood22-Dec-06 4:51 
AnswerRe: Can I develop C# version of this... - sure, C# code is here Pin
Igor Tolmachev22-Dec-06 9:40
MemberIgor Tolmachev22-Dec-06 9:40 
GeneralRe: Can I develop C# version of this... - sure, C# code is here Pin
aamironline24-Dec-06 15:48
Memberaamironline24-Dec-06 15:48 
GeneralNice again! Pin
Zhefu Zhang20-Dec-06 17:26
MemberZhefu Zhang20-Dec-06 17:26 
GeneralCool Pin
Jason Henderson20-Dec-06 17:08
MemberJason Henderson20-Dec-06 17:08 
GeneralRe: Cool Pin
PJ Arends21-Dec-06 0:23
professionalPJ Arends21-Dec-06 0:23 
GeneralNice Pin
Coolcute20-Dec-06 16:43
MemberCoolcute20-Dec-06 16:43 
Generalnice Pin
Nick Hodapp20-Dec-06 12:09
sitebuilderNick Hodapp20-Dec-06 12:09 
GeneralRe: nice Pin
Nick Hodapp20-Dec-06 12:25
sitebuilderNick Hodapp20-Dec-06 12:25 
GeneralRe: nice Pin
Igor Tolmachev20-Dec-06 21:16
MemberIgor Tolmachev20-Dec-06 21:16 

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.