Click here to Skip to main content
11,435,451 members (53,631 online)
Click here to Skip to main content

Falling Snow on Your Desktop!

, 20 Dec 2006
Rate this:
Please Sign up or sign in to vote.
This article explains how to create an application that makes it snow on the desktop.

FallingSnow

Introduction

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");
        if(hWnd) 
           return FALSE;
    
        CMainFrame* pFrame = new CMainFrame;
        m_pMainWnd = pFrame;
    
        pFrame->LoadFrame(IDR_TRAYMENU, 
                WS_OVERLAPPEDWINDOW, NULL, NULL);
    
        pFrame->ShowWindow(SW_HIDE);
    ...
    
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    ...
        if(cs.hInstance)
        {
            static const TCHAR szClassName[] = 
                        _T("SnowParentWindow");
            WNDCLASS wc;
            GetClassInfo(cs.hInstance, cs.lpszClass, &wc);
            wc.lpszClassName = szClassName;
            AfxRegisterClass(&wc);
            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;
    
        NOTIFYICONDATA tnd;
    
        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; 
    
        if(pszTip) 
           lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
        else
           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;
    SystemParametersInfo(SPI_GETWORKAREA,0,(LPVOID)&rcWorkArea,0);
    int nScreenWidth = rcWorkArea.Width();
    
    int nTimer = 5;
    int nPosX = 0;
    
    srand((unsigned)time(NULL));
    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);
        if(fd->Create(IDD_FLAKE,this))
        m_arSnowFlakes.Add(fd);
    }
  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)
        {
            KillTimer(1);
    
            m_nCurrentY += 5;
            m_nCounter++;
    
            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;
    
            if(m_nCurrentY>m_nScreenHeight)
            {
                m_nCurrentY = 0;
                m_nCurrentX = abs(rand()*m_nScreenWidth/RAND_MAX);
                m_nTimer = abs(rand()*50/RAND_MAX);
            }
    
            MoveWindow(m_nCurrentX,m_nCurrentY,17,17);
            SetTimer(1,m_nTimer,0);
        }
    
        CDialog::OnTimer(nIDEvent);
    }
    

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.

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

Share

About the Author

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

Comments and Discussions

 
Generalhelp me out in doing in windows Pin
mittyk12-Nov-09 23:54
membermittyk12-Nov-09 23:54 
GeneralCPU %100 Pin
csf3lih20-May-07 17:42
membercsf3lih20-May-07 17:42 
GeneralFlicker problem with Java window Pin
x-bay2-Jan-07 7:46
memberx-bay2-Jan-07 7:46 
GeneralRe: Flicker problem with Java window Pin
Igor Tolmachev3-Jan-07 5:56
memberIgor Tolmachev3-Jan-07 5:56 
Generalfeature Pin
Nick Alexeev30-Dec-06 12:56
memberNick Alexeev30-Dec-06 12:56 
GeneralRe: feature Pin
Igor Tolmachev3-Jan-07 6:00
memberIgor Tolmachev3-Jan-07 6:00 
GeneralGood Pin
A.N.Zayed28-Dec-06 5:54
memberA.N.Zayed28-Dec-06 5:54 
GeneralRe: Good Pin
Igor Tolmachev3-Jan-07 6:01
memberIgor Tolmachev3-Jan-07 6:01 
GeneralMy C# Article is here Pin
Igor Tolmachev27-Dec-06 7:39
memberIgor Tolmachev27-Dec-06 7:39 
GeneralFlicker problem with Active desktop Pin
dustin_26-Dec-06 5:36
memberdustin_26-Dec-06 5:36 
GeneralAlso Happy Eid:) Pin
Muammar©26-Dec-06 4:52
memberMuammar©26-Dec-06 4:52 
GeneralMerry X-Mas!! Pin
Muammar©25-Dec-06 20:53
memberMuammar©25-Dec-06 20:53 
GeneralVery nice snow :) Pin
Ezz Khayyat25-Dec-06 15:37
memberEzz Khayyat25-Dec-06 15:37 
GeneralRe: Very nice snow :) Pin
Igor Tolmachev3-Jan-07 6:02
memberIgor Tolmachev3-Jan-07 6:02 
GeneralIts snowing on a sunny day. Pin
Mr.Prakash25-Dec-06 3:06
memberMr.Prakash25-Dec-06 3:06 
GeneralVery Nice Work (and one Suggestion) Pin
Jeffrey Walton24-Dec-06 13:11
memberJeffrey Walton24-Dec-06 13:11 
GeneralRe: Very Nice Work (and one Suggestion) Pin
Igor Tolmachev25-Dec-06 3:00
memberIgor Tolmachev25-Dec-06 3:00 
GeneralRe: Very Nice Work (and one Suggestion) Pin
Jeffrey Walton25-Dec-06 19:17
memberJeffrey Walton25-Dec-06 19:17 
GeneralCool Pin
truongcongda22-Dec-06 23:40
membertruongcongda22-Dec-06 23:40 
GeneralRe: Cool Pin
Igor Tolmachev25-Dec-06 3:01
memberIgor Tolmachev25-Dec-06 3:01 
GeneralC# code is here [modified] Pin
Igor Tolmachev22-Dec-06 10:36
memberIgor Tolmachev22-Dec-06 10:36 
QuestionTerrific snowflakes! :) Pin
Raven#37722-Dec-06 8:46
memberRaven#37722-Dec-06 8:46 
AnswerRe: Terrific snowflakes! :) Pin
Igor Tolmachev22-Dec-06 10:33
memberIgor Tolmachev22-Dec-06 10:33 
QuestionGood job! Pin
sb_summer22-Dec-06 7:17
membersb_summer22-Dec-06 7:17 
GeneralFalling Snow Pin
Tigrisan21-Dec-06 11:23
memberTigrisan21-Dec-06 11:23 
GeneralRe: Falling Snow Pin
Igor Tolmachev21-Dec-06 23:44
memberIgor Tolmachev21-Dec-06 23:44 
GeneralProblem of Compil under VS2002 and later Pin
df49421-Dec-06 7:07
memberdf49421-Dec-06 7:07 
GeneralRe: Problem of Compil under VS2002 and later Pin
Igor Tolmachev21-Dec-06 7:23
memberIgor Tolmachev21-Dec-06 7:23 
GeneralRe: Problem of Compil under VS2002 and later Pin
df49421-Dec-06 7:26
memberdf49421-Dec-06 7:26 
GeneralRe: Problem of Compil under VS2002 and later Pin
Igor Tolmachev21-Dec-06 7:28
memberIgor Tolmachev21-Dec-06 7:28 
GeneralRe: Problem of Compil under VS2002 and later Pin
df49421-Dec-06 7:25
memberdf49421-Dec-06 7:25 
JokeHi Pin
Hugo.Pereira21-Dec-06 1:52
memberHugo.Pereira21-Dec-06 1:52 
GeneralRe: Hi Pin
Igor Tolmachev21-Dec-06 7:24
memberIgor Tolmachev21-Dec-06 7:24 
Generalhelp me pls Pin
kinnuP20-Dec-06 23:26
memberkinnuP20-Dec-06 23:26 
GeneralRe: help me pls Pin
Igor Tolmachev21-Dec-06 7:26
memberIgor Tolmachev21-Dec-06 7:26 
GeneralRe: help me pls - C# code is here Pin
Igor Tolmachev22-Dec-06 10:38
memberIgor Tolmachev22-Dec-06 10:38 
QuestionCan I develop C# version of this... Pin
M Aamir Maniar20-Dec-06 21:23
memberM Aamir Maniar20-Dec-06 21:23 
AnswerRe: Can I develop C# version of this... Pin
Igor Tolmachev20-Dec-06 22:14
memberIgor Tolmachev20-Dec-06 22:14 
GeneralRe: Can I develop C# version of this... Pin
tickko0621-Dec-06 7:14
membertickko0621-Dec-06 7:14 
GeneralRe: Can I develop C# version of this... Pin
Heywood22-Dec-06 5:51
memberHeywood22-Dec-06 5:51 
AnswerRe: Can I develop C# version of this... - sure, C# code is here Pin
Igor Tolmachev22-Dec-06 10:40
memberIgor Tolmachev22-Dec-06 10:40 
GeneralRe: Can I develop C# version of this... - sure, C# code is here Pin
M Aamir Maniar24-Dec-06 16:48
memberM Aamir Maniar24-Dec-06 16:48 
GeneralNice again! Pin
Zhefu Zhang20-Dec-06 18:26
memberZhefu Zhang20-Dec-06 18:26 
GeneralCool Pin
Jason Henderson20-Dec-06 18:08
memberJason Henderson20-Dec-06 18:08 
GeneralRe: Cool Pin
PJ Arends21-Dec-06 1:23
memberPJ Arends21-Dec-06 1:23 
GeneralNice Pin
Coolboy12320-Dec-06 17:43
memberCoolboy12320-Dec-06 17:43 
Generalnice Pin
Nick Hodapp20-Dec-06 13:09
sitebuilderNick Hodapp20-Dec-06 13:09 
GeneralRe: nice Pin
Nick Hodapp20-Dec-06 13:25
sitebuilderNick Hodapp20-Dec-06 13:25 
GeneralRe: nice Pin
Igor Tolmachev20-Dec-06 22:16
memberIgor Tolmachev20-Dec-06 22:16 
GeneralRe: nice Pin
bobhall10622-Dec-06 0:31
memberbobhall10622-Dec-06 0:31 

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
Web01 | 2.8.150428.2 | Last Updated 20 Dec 2006
Article Copyright 2006 by Igor Tolmachev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid