如何在更改后恢复/撤消桌面壁纸?

3

我正在创建一个屏幕共享应用程序。当开始进行屏幕共享时,我将桌面壁纸颜色更改为黑色。

问题 如何恢复以前的壁纸或Windows主题?

我正在使用下面的代码将背景更改为纯色。

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (LPARAM)wallpaperPath, SPIF_UPDATEINIFILE)

此外,这段代码存在问题,一旦使用此代码更改了背景,我就无法将图像设置为壁纸,但我可以应用主题。

public class wallpaperHelper
    {
        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);
        }
    }
1个回答

5

在更改壁纸之前,您可以获取当前的壁纸:

int SPI_GETDESKWALLPAPER = 0x73;
int MAX_PATH = 260;
string wallpaper = new string('\0', (int)MAX_PATH);
NativeMethods.SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, wallpaper, 0);

wallpaper = wallpaper.Substring(0, wallpaper.IndexOf('\0'));

当恢复旧的壁纸时,只需将其传递给SystemParametersInfo函数。
NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    wallpaper,
    NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);

此外,如果您不想永久更改壁纸,请更改:
NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    Newwallpaper,
    NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);

致:

NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    Newwallpaper,
    0);

这将防止Windows保存当前更改。当您关闭计算机并再次打开时,旧的壁纸将被恢复。如果在午夜更改了壁纸并忘记恢复回来,这非常有用。 注意: 如果当前壁纸是一个主题,您可以从以下位置复制它:
C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper

将它保存在某个地方。需要时,将壁纸设置为此文件,然后删除它。

对于 '.theme' 文件,请复制。

C:\Users\<User-Name>\AppData\Local\Microsoft\Windows\Themes\Custom.theme

将它保存在某个地方。需要时,将壁纸设置为此文件,然后删除它。

rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:"C:\pathtoYourTheme.theme"

如果您确定需要旧主题,请从以下位置选择:

C:\Windows\Resources\Themes


对于设置颜色的问题,您是否想要去掉壁纸后面的黑色?其中一种选择是进入桌面/个性化/桌面背景,将位置更改为填充。这个选项会将图像缩放以适应屏幕的水平和垂直方向。


用户当前的壁纸是一个Windows主题C:\Users\vikas\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper。当我尝试使用您的代码设置主题时,它无法正常工作,但如果我设置一张图片,则可以正常工作。 - Vikas Bansal
@VikasBansal,TranscodedWallpaper是一张图片。Windows只是将其重命名。您可以将其复制到其他位置,然后在需要时将其传递给您的函数,然后删除它。 - Thanh Nguyen
1
@VikasBansal 如果我的编辑(在底部)有任何帮助,请查看! - Thanh Nguyen
1
@VikasBansal 谢谢!很高兴我的回答解决了你的问题。 - Thanh Nguyen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接