As I previously mentioned, I like to make surprises for the people I value. You can find some of my unique geeks' gifts ideas in my - Geeks Gifts Series. In this article, I will share with you an idea of mine that I developed to surprise a girl for 8th of March. I knew that she likes cats, so I decided to create an app that changes her desktop's wallpaper with a random HD cat image.
What Is the Gift's Idea?
So, as I told you it was 7th of March, and I wanted to create an original gift for a girl, let's name her Amara. So I knew that this Amara likes cats and as every woman - flowers. Also, she didn't have an IT background so I thought that I could impress her with a little app. The idea was that there are two icons- one of a flower and one of a cat. Once she clicks the cat, a random cool cat is displayed as a wallpaper. The same happens with the flower button. This way, she can be surprised every day with different beautiful images reminding her of the app's creator.
Cats or Flowers Wallpaper Changing App Code
The app that you have seen above is a WPF application. It contains a grid with two images that has an implemented
MouseDown event. I downloaded the icons from Flaticon.com, there you can find lots of other free icons. Below, you can find the code of the only page of the app.
Title="Flo Flower or Cat?" Height="200"
MinWidth="400" MaxWidth="400" >
<Image Source="catIcon.ico" Margin="-86,0,86,0"
<Image Source="flowerIcon.ico" Margin="197,0,0,0"
public static class WallpaperChangingService
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo
(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Set(Bitmap fileName, WallpaperStyle style)
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
RegistryKey key = Registry.CurrentUser.OpenSubKey
(@"Control Panel\Desktop", true);
if (style == WallpaperStyle.Stretched)
if (style == WallpaperStyle.Centered)
if (style == WallpaperStyle.Tiled)
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
This is the place where most of the magic happens. First, you need
SystemParamersInfo which sets the wallpaper. Also, before it is called, you need to set a new value in the Control
PanelDesktop registry key. The value varies from the wallpaper's style-
Tiled. We will use
Centered. This is a utility class because of that, it is marked as
static as all methods inside it.
Add Wallpaper Images as Resources
You can find lots of high definition wallpapers for free if you just Google. I have downloaded 40 cats and 40 flowers images. Since we want the app to be portable (not to be installed), this means that all of the images should be embedded in the executable file. To do that, you need to add all of the pictures as resources.
- Open the Properties of the WPF project.
- Then navigate to the Resources section.
- After that, you need to click on the Add Resource button.
- Choose Add Existing File and select all of the cats and flowers that you have downloaded.
- To be able to recognise which images are cats and which are flowers, we use the
flower_ prefix in the files names.
Main Window Code Behind
Below, you can find the code of the Window XAML page. Here I implemented the
MouseDown event. Also, we load the images from the resources.
public partial class MainWindow : Window
List<Bitmap> flowers = new List<Bitmap>();
List<Bitmap> cats = new List<Bitmap>();
Random randCat = new Random();
Random randFlower = new Random();
bool canBeClickedAgain = true;
private void Cat_MouseDown(object sender, MouseButtonEventArgs e)
canBeClickedAgain = false;
int randCatIndex = randCat.Next(0, cats.Count - 1);
canBeClickedAgain = true;
private void Flower_MouseDown(object sender, MouseButtonEventArgs e)
canBeClickedAgain = false;
int randFlowerIndex = randFlower.Next(0, flowers.Count - 1);
canBeClickedAgain = true;
private void Window_Loaded(object sender, RoutedEventArgs e)
ResourceSet resourceSet =
Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
cats.Add(resource as Bitmap);
else if (resourceKey.Contains("flowers"))
flowers.Add(resource as Bitmap);
Since the wallpaper change is not instant, it can take a couple of seconds depending on the machine. I added special safety check with the
canBeClickedAgain variable. We use the .NET built-in random generator to get a random flower or cat from the bitmaps' lists.
These bitmap collections are loaded through the
ResourceManager. We use its
GetResourceSet method. It returns a dictionary of the names of the files and objects. Then we cast the object to bitmap and add it to the appropriate list.
Finish the App- Add an Icon
In order for your surprise to look more complete, you need to change the icon for your app. You will find lots of sites for free icons. For example - FlatIcon.com. After you download the most appropriate one, open the properties of the project file and change the icon from the Application tab (Resources section).
Here I chose a magician hat since the app always 'throws' a new cat or flower.
So Far in the "Geek Gifts Series" Series
- Geeks Gifts: Surprise a Girl with ASCII Art Flowers Program
The post Geeks Gifts: Surprise a Girl with Cats or Flowers Wallpaper Changing App appeared first on Automate The Planet.
All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.