Click here to Skip to main content
14,766,428 members
Articles » Languages » C# » Applications
Tip/Trick
Posted 15 Sep 2015

Stats

86.8K views
8.6K downloads
16 bookmarked

Setting The Virtual Desktop Background (In Windows 10)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (10 votes)
15 Sep 2015CPOL
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).

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)

Share

About the Author

Blondy314
Israel Israel
No Biography provided

Comments and Discussions

 
QuestionDid not work for me... threw exception error when I added 2nd image of three Pin
Member 1491571417-Aug-20 22:03
MemberMember 1491571417-Aug-20 22:03 
BugRe: Did not work for me... threw exception error when I added 2nd image of three Pin
Member 1491571417-Aug-20 23:15
MemberMember 1491571417-Aug-20 23:15 
QuestionThe program is not working, MIDL_INTERFACE changed. Pin
Member 111315765-Jun-19 7:53
MemberMember 111315765-Jun-19 7:53 
AnswerRe: The program is not working, MIDL_INTERFACE changed. Pin
Alexander Hudson10-Sep-19 6:29
MemberAlexander Hudson10-Sep-19 6:29 
QuestionVery slow program startup Pin
Tom Toolman5-Dec-18 14:20
MemberTom Toolman5-Dec-18 14:20 
QuestionUse event trigger instead of timer? Pin
AntonioKatim31-Mar-18 4:49
MemberAntonioKatim31-Mar-18 4:49 
AnswerRe: Use event trigger instead of timer? Pin
AntonioKatim31-Mar-18 5:30
MemberAntonioKatim31-Mar-18 5:30 
QuestionNot working on windows 10 anniversary update Pin
Member 1269023317-Aug-16 8:01
MemberMember 1269023317-Aug-16 8:01 
AnswerRe: Not working on windows 10 anniversary update Pin
Member 1268626319-Aug-16 5:05
MemberMember 1268626319-Aug-16 5:05 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1269490220-Aug-16 8:33
MemberMember 1269490220-Aug-16 8:33 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626321-Aug-16 12:15
MemberMember 1268626321-Aug-16 12:15 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1269490221-Aug-16 18:16
MemberMember 1269490221-Aug-16 18:16 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626322-Aug-16 3:13
MemberMember 1268626322-Aug-16 3:13 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1269490222-Aug-16 6:34
MemberMember 1269490222-Aug-16 6:34 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626322-Aug-16 7:40
MemberMember 1268626322-Aug-16 7:40 
GeneralRe: Not working on windows 10 anniversary update Pin
acegas29-Aug-16 11:41
Memberacegas29-Aug-16 11:41 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626329-Aug-16 11:59
MemberMember 1268626329-Aug-16 11:59 
GeneralRe: Not working on windows 10 anniversary update Pin
Blondy3141-Sep-16 23:10
MemberBlondy3141-Sep-16 23:10 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 126862632-Sep-16 3:09
MemberMember 126862632-Sep-16 3:09 
GeneralRe: Not working on windows 10 anniversary update Pin
dharmaturtle27-Oct-16 23:33
Memberdharmaturtle27-Oct-16 23:33 
GeneralRe: Not working on windows 10 anniversary update Pin
Member 1268626328-Oct-16 3:45
MemberMember 1268626328-Oct-16 3:45 
Questionhow do ya use it? Pin
Member 1263139512-Jul-16 7:16
MemberMember 1263139512-Jul-16 7:16 
AnswerRe: how do ya use it? Pin
David J16-Sep-16 18:59
MemberDavid J16-Sep-16 18:59 
AnswerRe: how do ya use it? Pin
Member 1419740726-Mar-19 2:04
MemberMember 1419740726-Mar-19 2:04 
GeneralMy vote of 5 Pin
Member 125101658-May-16 7:03
MemberMember 125101658-May-16 7:03 

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.