C#: Set Desktop Wallpaper to a Solid Color
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.
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.
精彩评论