4

I'm using this code to remove the current wallpaper and set a solid color:

public static class WallpaperColorChanger
{

    public static void SetColor(Color color)
    {

        // Remove the current wallpaper
        NativeMethods.SystemParametersInfo(
            NativeMethods.SPI_SETDESKWALLPAPER,
            0,
            "",
            NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);

        // Set the new desktop solid color for the current session
        int[] elements = { NativeMethods.COLOR_DESKTOP };
        int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) };
        NativeMethods.SetSysColors(elements.Length, elements, colors);

        // Save value in registry so that it will persist
        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
        key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));
    }

    private static class NativeMethods
    {
        public const int COLOR_DESKTOP = 1;
        public const int SPI_SETDESKWALLPAPER = 20;
        public const int SPIF_UPDATEINIFILE = 0x01;
        public const int SPIF_SENDWININICHANGE = 0x02;

        [DllImport("user32.dll")]
        public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
    }
}

However, if the Windows 7 SlideShow feature is enabled and the wallpaper changes every 30 minutes for example, when I run the code it changes the wallpaper to a solid color, but when I restart the computer it brings back the wallpaper and the SlideShow feature.

What else must I do in order to get the solid color to remain even after restarting the computer?

I'm using C#, Windows Forms, Windows 7.

TechAurelian
  • 5,561
  • 5
  • 50
  • 65
  • 1
    You have to disable the slideshow and any other code that also changes the background. – Raymond Chen Sep 05 '11 at 15:23
  • 1
    How do you programmatically disable the slideshow? – TechAurelian Sep 05 '11 at 15:26
  • I don't know. I'm just pointing out that any desktop slideshow program is going to overwrite your changes. It's not just the default slideshow that you need to worry about. Third party slideshows will create this problem too. At any rate, the desktop wallpaper is a user preference setting and programs shouldn't be messing with it. – Raymond Chen Sep 06 '11 at 21:01

1 Answers1

1

From memory (Been a while since I did anything like this), if you set the desktop wallpaper to NOTHING (Empty string), then set it to a file (Can be a transparent pixel if you like, then set it to nothing again, I believe that it disables the slideshow.

You can then just set a desktop colour, and it should stick. As for program turning off the wallpaper slideshow I've no idea what the win api call for that is.

shawty
  • 5,729
  • 2
  • 37
  • 71