Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming
Tip/Trick

Setting The Virtual Desktop Background (In Windows 10)

Rate me:
Please Sign up or sign in to vote.
4.22/5 (13 votes)
15 Sep 2015CPOL2 min read 134.2K   11.9K   17   42
An application which allows you to set a different wallpaper for each desktop

Set Wallpapers

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).

C++
private int GetDesktopIndex()
{
    for (int i = 0; i < Desktop.Count; ++i)
    {
        if (Desktop.Current.Equals(Desktop.FromIndex(i)))
        {
            return i;
        }
    }

    return -1;
}

// Each tick it checks if the desktop has changed and if so - sets the background

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;
}

//I call the SystemParametersInfo function using native API calls</p>

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
            internal static extern int SystemParametersInfo(
                int uAction,
                int uParam,
                String lpvParam,
                int fuWinIni);

//This is how we set the desktop background (Using the Registry and the API)

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

// Fill
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.

License

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


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626328-Oct-16 2:45
Member 1268626328-Oct-16 2:45 
Questionhow do ya use it? Pin
Member 1263139512-Jul-16 6:16
Member 1263139512-Jul-16 6:16 
AnswerRe: how do ya use it? Pin
David J16-Sep-16 17:59
David J16-Sep-16 17:59 
AnswerRe: how do ya use it? Pin
Member 1419740726-Mar-19 1:04
Member 1419740726-Mar-19 1:04 
GeneralMy vote of 5 Pin
Member 125101658-May-16 6:03
Member 125101658-May-16 6:03 
QuestionDesktop amount Pin
Member 1247013819-Apr-16 0:28
Member 1247013819-Apr-16 0:28 
Questiondifferent image for each monitor Pin
Member 1205002011-Oct-15 5:35
Member 1205002011-Oct-15 5:35 
AnswerRe: different image for each monitor Pin
Blondy31426-Feb-16 6:28
Blondy31426-Feb-16 6:28 
You can change the code to get the feature u wanted - u can give the user an option to choose multiple files instead of just one for each user.
Regarding the hook - I guess u can find the event that switches the windows but then the solution will be more difficult Smile | :)
SuggestionCool and useful Pin
Jawz-X16-Sep-15 8:53
Jawz-X16-Sep-15 8:53 
GeneralRe: Cool and useful Pin
Blondy31422-Sep-15 21:16
Blondy31422-Sep-15 21:16 

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.