C#: Set Desktop Wallpaper to a Solid Color

混江龙づ霸主 提交于 2020-01-21 12:02:41

问题


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.


回答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.



来源:https://stackoverflow.com/questions/7309943/c-set-desktop-wallpaper-to-a-solid-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!