【问题标题】:C#: Set Desktop Wallpaper to a Solid ColorC#:将桌面壁纸设置为纯色
【发布时间】: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


【解决方案1】:

根据记忆(我做了这样的事情已经有一段时间了),如果您将桌面墙纸设置为 NOTHING(空字符串),然后将其设置为文件(如果您愿意,可以是透明像素,然后将其设置为没什么,我相信它会禁用幻灯片。

然后您可以设置桌面颜色,它应该会保持不变。至于关闭壁纸幻灯片的程序,我不知道 win api 调用的是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多