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.
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
- 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:
using System.Threading;
...
private static Mutex m_Mutex;
...
m_Mutex = new Mutex(true, "SnowFallMutex");
if (m_Mutex.WaitOne(0, false))
Application.Run(new MainForm());
- 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.
- 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.
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();
}
}
- 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.
- 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.
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.