我正在创建一个屏幕共享应用程序。当开始进行屏幕共享时,我将桌面壁纸颜色更改为黑色。
问题 如何恢复以前的壁纸或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);
}
}
C:\Users\vikas\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper
。当我尝试使用您的代码设置主题时,它无法正常工作,但如果我设置一张图片,则可以正常工作。 - Vikas BansalTranscodedWallpaper
是一张图片。Windows只是将其重命名。您可以将其复制到其他位置,然后在需要时将其传递给您的函数,然后删除它。 - Thanh Nguyen