Click here to Skip to main content
14,838,821 members
Articles / Programming Languages / C#
Posted 15 Dec 2007


54 bookmarked

Falling Snow on Your Desktop! Part II

Rate me:
Please Sign up or sign in to vote.
4.59/5 (9 votes)
16 Dec 2007CPOL1 min read
This article explains how to create an application that makes it snow on the desktop.
Screenshot - FallingSnow


Since my previous article was published, I received many user inquiries about how to improve the program. This article demonstrates how to create snow flakes that fall on your desktop, by using the draw functions directly in the desktop context. When the application starts, it creates an array of snow flakes and starts the timers for each flake. Manipulation of the RedrawWindow function allows the drawing of flakes behind and over the desktop icons, making the application more impressive.

General Steps

For better randomizing, the start coordinate, timer interval, flake position (over or behind the desktop icons) and size should be pre-defined as shown below:

for(int i=0; i<nArray; i++)
   // the snow flake movement timer
   nTimer = rand()*70/RAND_MAX+10;

   // snow flake start position
   nPosX = rand()*rcWorkArea.Width()/RAND_MAX;

   // draw the flake over or behind the icons
   nOver = rand()*100/RAND_MAX;

   // draw a big or small flake
   nBig = rand()*100/RAND_MAX;

Then, create the flake and start the timer:

CFlake* pFlake = new CFlake(nPosX, nBig, nOver);


When the timer event occurs, move the appropriate flake on the desktop:

void CMainFrame::OnTimer(UINT nIDEvent)
   if(nIDEvent >= 0 && nIDEvent <= (UINT)m_arFlakes.GetSize())
       CFlake* pFlake = m_arFlakes.GetAt(nIDEvent-1);

As shown above, class CFlake (Flake in C# code) is responsible for the desktop drawing.
Unfortunately, Windows Vista does not allow the use of the RedrawWindow function with the RDW_NOERASE flag for the desktop Window correctly. So, you cannot draw the flakes behind the icons in this case.

Most of the snow fall algorithm was taken from my previous article, except the drawing functions:

HDC hDC = GetDC(m_hWndDesktop);
   RECT rc;
   rc.left = m_nCurrentX; = m_nCurrentY;
   rc.right = m_nCurrentX+15;
   rc.bottom = m_nCurrentY+15;

   // 15 is for a little drift at the bottom of the desktop
   int nTestHeight = m_nScreenHeight - 15;

   // redraw the desktop window right away
   if(m_nCurrentY<nTestHeight) // snow drift here
       RedrawWindow(m_hWndDesktop, &rc, NULL, RDW_INVALIDATE
           | RDW_ERASE | RDW_UPDATENOW );

   m_nCurrentY += 3;

   if(m_nCounter == 15)
       if((rand()*10/RAND_MAX)>5) 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_bIsBigFlake = TRUE;
           m_bIsBigFlake = FALSE;

   // Store DC settings
   int storedDC = SaveDC(hDC);

   rc.left = m_nCurrentX; = m_nCurrentY;
   rc.right = m_nCurrentX+15;
   rc.bottom = m_nCurrentY+15;

   HPEN pOldPen = (HPEN)SelectObject(hDC, m_hFlakePen);

   if(m_bIsBigFlake) // draw big flake here
       MoveToEx(hDC, m_nCurrentX+7, m_nCurrentY, 0);
       LineTo(hDC, m_nCurrentX+7, m_nCurrentY+15);
   else // draw small flake here

   // specify RDW_NOERASE to keep the desktop from drawing the background
       if(!m_bOverIcons && m_nCurrentY<m_nScreenHeight)
           RedrawWindow(m_hWndDesktop, &rc, NULL, RDW_INVALIDATE
               | RDW_NOERASE | RDW_UPDATENOW);

   SelectObject(hDC, pOldPen);

   // Restore DC settings to their original values
   RestoreDC(hDC, storedDC);

   // Release the DC
   ReleaseDC(m_hWndDesktop, hDC);

Finally, when the application closes, clean up the desktop in the CFlake destructor (the current flake on screen position), and in the CMainFrame::OnClose function (snow drift at the bottom of the desktop):

   RedrawWindow(m_hWndDesktop, &rc, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW );

void CMainFrame::OnClose()
   // redraw the snow drifts area
       CRect rcWorkArea;
       SystemParametersInfo(SPI_GETWORKAREA,0,(LPVOID)&rcWorkArea,0); = rcWorkArea.bottom - 16;
       ::RedrawWindow(hWndDesktop, &rcWorkArea, NULL, RDW_INVALIDATE | RDW_ERASE |
           RDW_UPDATENOW );

Known Problems

  • Application does not work properly when the Active Desktop is enabled.
  • Drawing the snowflakes behind the icons with this code under Windows Vista is not implemented.

Any improvements, comments or suggestions to this code are welcome.


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


About the Author

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

Comments and Discussions

Questionhow to change the size of flake? Pin
eagle_chen27-Mar-12 2:29
Membereagle_chen27-Mar-12 2:29 
Rantgreat job! Pin
lanmuy26-Dec-10 20:35
Memberlanmuy26-Dec-10 20:35 
GeneralIt doesn't work on XP ! Pin
gndnet27-Apr-08 4:20
Membergndnet27-Apr-08 4:20 
GeneralVery festive Pin
_awatts17-Dec-07 22:29
Member_awatts17-Dec-07 22:29 
GeneralRe: Very festive, can flakes be randomly drawn in front of active windows Pin
Igor Tolmachev18-Dec-07 22:29
MemberIgor Tolmachev18-Dec-07 22:29 
QuestionNeat Pin
Hard Coder16-Dec-07 11:19
MemberHard Coder16-Dec-07 11:19 
GeneralRe: Neat, would it be hard to fix it so that the snow falls onto windows tops Pin
Igor Tolmachev18-Dec-07 22:20
MemberIgor Tolmachev18-Dec-07 22:20 
GeneralCool - but it hates 4 monitors :) Pin
Ray Cassick15-Dec-07 18:50
MemberRay Cassick15-Dec-07 18:50 
GeneralRe: Cool - but it hates 4 monitors :) Pin
Dave Cross16-Dec-07 4:29
professionalDave Cross16-Dec-07 4:29 
GeneralRe: Cool - I have got it working with 3 monitors [modified] Pin
MikeWilliams17-Dec-07 22:40
MemberMikeWilliams17-Dec-07 22:40 
GeneralRe: Cool - I have got it working with 3 monitors Pin
Igor Tolmachev18-Dec-07 22:25
MemberIgor Tolmachev18-Dec-07 22:25 
GeneralRe: Cool - I have got it working with 3 monitors Pin
MikeWilliams18-Dec-07 23:16
MemberMikeWilliams18-Dec-07 23:16 
GeneralRe: Cool - but it hates 4 monitors :) Pin
MikeWilliams18-Dec-07 23:19
MemberMikeWilliams18-Dec-07 23:19 
GeneralRe: Cool - but it hates 4 monitors :) Pin
Ray Cassick19-Dec-07 2:12
MemberRay Cassick19-Dec-07 2:12 

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.