C#:将桌面壁纸设置为纯色

发布于 2024-12-03 04:25:42 字数 1671 浏览 3 评论 0原文

我使用此代码删除当前壁纸并设置纯色:

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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-12-10 04:25:42

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

然后您只需设置桌面颜色,它应该会保留,至于关闭壁纸幻灯片的程序,我不知道 win api 的要求是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文