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

Vik*_*sal 3 c#

我正在创建一个屏幕共享应用程序.启动屏幕共享时,我将桌面墙纸颜色更改为黑色.

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

我使用代码将背景更改为纯色,如下所示

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

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Tuy*_*ham 5

在更改为其他之前,您可以获取当前的WallPaper:

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'));
Run Code Online (Sandbox Code Playgroud)

恢复旧壁纸时,只需将其传递给SystemParametersInfo.

NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    wallpaper,
    NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);
Run Code Online (Sandbox Code Playgroud)

如果您不想永久更换壁纸,请更改:

NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    Newwallpaper,
    NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);
Run Code Online (Sandbox Code Playgroud)

至:

NativeMethods.SystemParametersInfo(
    NativeMethods.SPI_SETDESKWALLPAPER,
    0,
    Newwallpaper,
    0);
Run Code Online (Sandbox Code Playgroud)

这将阻止Window保存当前更改.关闭计算机并再次打开时,将恢复旧的壁纸.非常有用,如果你在午夜将WallPaper更改为敏感的东西并忘记恢复:)

注意:

如果当前壁纸是主题,您可以从以下位置复制:

C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper
Run Code Online (Sandbox Code Playgroud)

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

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

C:\Users\<User-Name>\AppData\Local\Microsoft\Windows\Themes\Custom.theme
Run Code Online (Sandbox Code Playgroud)

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

rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:"C:\pathtoYourTheme.theme"
Run Code Online (Sandbox Code Playgroud)

如果你确定旧的主题是什么,你可以从以下地方选择:

C:\Windows\Resources\Themes
Run Code Online (Sandbox Code Playgroud)


对于设定的颜色问题,你想摆脱壁纸背后的黑色吗?一个选择是去Desktop/Personalize/Desktop background改变PositionFill.此选项将缩放图像以适合水平和垂直屏幕.