【发布时间】:2011-09-05 15:18:31
【问题描述】:
我正在使用此代码删除当前壁纸并设置纯色:
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);
}
}
但是,如果启用了 Windows 7 幻灯片功能并且壁纸每 30 分钟更改一次,那么当我运行代码时,它会将壁纸更改为纯色,但当我重新启动计算机时,它会恢复壁纸和幻灯片功能。
我还需要做什么才能在重新启动计算机后保持纯色?
我正在使用 C#、Windows 窗体、Windows 7。
【问题讨论】:
-
您必须禁用幻灯片和任何其他也会更改背景的代码。
-
如何以编程方式禁用幻灯片?
-
我不知道。我只是指出任何桌面幻灯片程序都会覆盖您的更改。您需要担心的不仅仅是默认幻灯片。第三方幻灯片也会产生这个问题。无论如何,桌面壁纸是用户偏好设置,程序不应该乱用它。
标签: c# winforms windows-7 desktop wallpaper