Introduction
After upgrading to Windows 10, I started to use the virtual desktops feature. There is more than one person who uses the computer so we decided each one will have a desktop of his own.
We wanted to set a different wallpaper for each desktop, so we could distinguish them easily. I searched the web to see how to do it and I was amazed to discover that there is no way to do such a thing (!)
That's how I got the idea to write this small application which allows you to set a different wallpaper for each desktop.
Basically, it uses APIs to get the current desktop and checks every small period of time to see if it has changed. If it detects a change, it sets the wallpaper to the appropriate one (configured by the user in the main screen).
For convenience, I set the app to start at the system tray so you can add it to the startup menu and run it when the computer starts.
In the main screen, you can set the wallpaper for each desktop (supports up to 3 desktops for the time being). The app remembers the last configured settings (in an INI file) and loads them the next time it starts.
The code for the interaction with the desktop is taken from several places on the web, the main one is - http://stackoverflow.com/questions/32416843/altering-win10-virtual-desktop-behavior.
Using the Code
This function demonstrated how to get the current desktop index (the first one is 0 of course).
private int GetDesktopIndex()
{
for (int i = 0; i < Desktop.Count; ++i)
{
if (Desktop.Current.Equals(Desktop.FromIndex(i)))
{
return i;
}
}
return -1;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtPic1.Text) || string.IsNullOrEmpty(txtPic2.Text))
{
return;
}
var index = GetDesktopIndex();
if(index == -1)
{
return;
}
if (_lastDesktop == index)
{
return;
}
SetBackground(index);
_lastDesktop = index;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SystemParametersInfo(
int uAction,
int uParam,
String lpvParam,
int fuWinIni);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
key.SetValue(@"PicturePosition", "10");
key.SetValue(@"TileWallpaper", "0");
key.SetValue(@"WallpaperStyle", "6");
key.Close();
const int setDesktopBackground = 20;
const int updateIniFile = 1;
const int sendWindowsIniChange = 2;
NativeMethods.SystemParametersInfo(
setDesktopBackground,
0,
pic,
updateIniFile | sendWindowsIniChange);
Points of Interest
I discovered how to interact with virtual desktops and use the relevant APIs for Windows 10.
Also, this was the first time I've created an app that has a system tray icon.