
Introduction
I really don’t know what to say about this article but I'd like to stress on "It only took me one hour to code this application so be nice and don’t beat me so hard with your comments".
Well, I just read a nice article this morning by Igor Tolmachev called Falling Snow on Your Desktop! here in the CP and said Merry X-Mas to everyone, but I thought there are good muslems here in the forum who would like to hear Happy Eid because it's about time too they have their sheeps slaughtered!
Explanation
Im sorry but I think there's nothing to explain about such an easy piece of application,
but here's what I would call the recipé:
Ingredients
Three Image objects are created for the three faces of the good sheep.
Sheep1 = Image.FromFile("Sheeps\weird_sheep1.gif");
Sheep2 = Image.FromFile("Sheeps\weird_sheep2.gif");
Sheep3 = Image.FromFile("Sheeps\weird_sheep3.gif");
Two timers to control the sheep 3 faces, the sheep movement on the desktop.
One Notify icon to control the application.
One Context menu poped from the notify icon.
Start Cooking
The form on which is the sheep is set to transparent using the transparency key property, form borders are removed, and a picture box control is added and docked to the parent container form.
The timers are started when the form is loaded with the interactivity of adjusting the sheep speed, this's done by using a variable sheepSpeed which's modified during run-time using the notify icon's context menu.
The Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Sheep
{
public partial class Form1 : Form
{
int flipper, screenW, screenH;
int sheepX, sheepSpeed;
Image Sheep1, Sheep2, Sheep3;
public Form1()
{
InitializeComponent();
Sheep1 = Image.FromFile(@"Sheeps\weird_sheep1.gif");
Sheep2 = Image.FromFile(@"Sheeps\weird_sheep2.gif");
Sheep3 = Image.FromFile(@"Sheeps\weird_sheep3.gif");
flipper = 1;
screenW
= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
screenH
= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
sheepX = screenW;
sheepSpeed = 2;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(screenW, screenH - this.Height);
BoxSheep.Image = Sheep1;
tmrSheeper.Start();
tmrMoveit.Start();
}
private void tmrSheeper_Tick(object sender, EventArgs e)
{
switch (flipper)
{
case 1:
BoxSheep.Image = Sheep1;
flipper++;
break;
case 2:
BoxSheep.Image = Sheep2;
flipper++;
break;
case 3:
BoxSheep.Image = Sheep3;
flipper = 1;
break;
}
}
private void tmrMoveit_Tick(object sender, EventArgs e)
{
screenH = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
if (sheepX >= this.Width * -1)
{
this.Location = new Point(sheepX, (screenH - this.Height)-22);
sheepX -= sheepSpeed;
}
else
sheepX = screenW;
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
notifyIcon1.Dispose();
this.Close();
}
private void fastToolStripMenuItem_Click(object sender, EventArgs e)
{
sheepSpeed = 3;
}
private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
{
sheepSpeed = 2;
}
private void slowToolStripMenuItem_Click(object sender, EventArgs e)
{
sheepSpeed = 1;
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
}
}
Points of interest
Notice that I've used System.Windows.Forms.Screen.PrimaryScreen.Bounds to get the primary screen boundries nevertheless; I endup with adding 22 pixle to fix
the Form Y location. Now im using:
System.Windows.Forms.Screen.PrimaryScreen.WorkingArea
Only now I can remove the 22 tweaking pixles. Thanx to cokeman's feedback!
Notes
Because CP doesn’t allow EXEs in article submision, I've renamed the .exe demo file
to .zip, so it's actually not a zip file! Please rename it first to .exe before running it.