3

I am running a powershell script to change the background to a certain set of colors. I would like to do this without rebooting, but unfortunately cannot get the changes to take effect immediately on a windows 7/8 platform. I've found many solutions online but I cannot find one that works for me. I think it may have something to do with setting the SystemParametersInfo, but I don't know for sure. I've seen a few solutions and tried them out for myself, but I can't get them to work either. The registry keys update just find but the changes don't take effect until after I reboot. Below is what I have so far, if anyone seeing anything I could do different I would appreciate the help!

backgroundtest.ps1

Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 20; <# following examples online to set parameters #>

        public static void SetBackground() {
            SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
            key.SetValue(@"WallPaper", 0); <#remove wallpaper#>
            RegistryKey key2 = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key2.SetValue(@"Background", "0 118 163"); <#set background to new color>
            key.Close();
            key2.Close();
         }

    }


}

"@

[Background.Setter]::SetBackground() 
4
  • You could try moving the SystemParametersInfo call to after you update the registry keys rather than before, as that might cause the system to update its internal state. Sep 10, 2014 at 22:13
  • @JonathanPotter Hi Jonathan, thanks for the reply. I found out that I don't need to mes with the SystemParametersInfo. I need to figure out how to use SetSysColors. Editing my question now. Sep 10, 2014 at 22:22
  • You would still need SystemParametersInfo to clear out any wallpaper image if you want to end up with a solid color. Sep 10, 2014 at 23:21
  • @JonathanPotter Yeah Definitely, this code works for clearing/setting the background. Sep 11, 2014 at 15:25

3 Answers 3

7

The documented way to change the system colours is the SetSysColors function.

This sends the WM_SYSCOLORCHANGE message to all top-level windows to notify them of the change.

I've updated your class to clear the background and set the colour to purple. It would need copying into your PowerShell stuff. Note that the way I've declared SetSysColors you can only change one colour at a time.

public class Setter {
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SetSysColors(int count, [In] ref int index, [In] ref int colour);

    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    public const int SetDesktopBackground = 20;
    public const int COLOR_BACKGROUND = 1;

    public static void SetBackground() {
        SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
        int index = COLOR_BACKGROUND;
        int colour = 0xFF00FF;
        SetSysColors(1, ref index, ref colour);
    }
}
3
  • Yeah, I have been working on figuring that out. Is there an external library I need to install to get the System.Drawing.Color imported? I keep getting an error that it can't be found. Sep 11, 2014 at 15:26
  • @wheatfairies I've added some code. It successfully updates the desktop.
    – arx
    Sep 11, 2014 at 16:35
  • Thanks Arx! That helps for sure. I actually just figured it out so I will post my solution :) most credit goes to the link here gallery.technet.microsoft.com/scriptcenter/… Sep 11, 2014 at 16:43
7

Yesterday was my first Powershell experience and I was pretty lost with what I needed to do. In order to change the desktop background to a solid color, you first need to remove the wall paper and then you can use the SetSysColors function to immediately change the desktop background. This link helped me out tremendously. http://gallery.technet.microsoft.com/scriptcenter/Change-window-borderdesktop-609a6fb2

Hopefully this helps someone the same way it helped me.

Updated Code

$code = @'
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace Background 
{
    public class Setter {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =true)]
        private static extern int SetSysColors(int cElements, int[] lpaElements, int[] lpRgbValues);
        public const int UpdateIniFile = 0x01;
        public const int SendWinIniChange = 0x02;
        public const int SetDesktopBackground = 0x0014;
        public const int COLOR_DESKTOP = 1;
        public int[] first = {COLOR_DESKTOP};


        public static void RemoveWallPaper() {
        SystemParametersInfo( SetDesktopBackground, 0, "", SendWinIniChange | UpdateIniFile );
        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        key.SetValue(@"WallPaper", 0);
        key.Close();
        }

        public static void SetBackground(byte r, byte g, byte b) {
            RemoveWallPaper();
            System.Drawing.Color color= System.Drawing.Color.FromArgb(r,g,b);
            int[] elements = {COLOR_DESKTOP};
            int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) }; 
            SetSysColors(elements.Length, elements, colors);
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
            key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));
            key.Close();

        }

    }


}

'@
Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing.dll -PassThru
Function Set-OSCDesktopColor
{
    <# Powershell function to remove desktop background and set background to colors we want #>
    Process
    {
        [Background.Setter]::SetBackground(0,118,163)
        return
    }
}

Set-OSCDesktopColor
4

I grabbed Wheatfairies code, updated it and published in (with Attribution) to the PowerShell Gallery at: https://www.powershellgallery.com/packages/Set-DesktopBackGround/1.0.0.0/DisplayScript

So now everyone can just type: Install-Script -Name Set-DesktopBackGround

to get it.

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.