我想使用 C# 为 Windows XP 设置壁纸。我已经开发了代码,因此它可以在 Windows 7 中完美运行,但显然对于 XP 来说并不相同。我将该壁纸添加为资源,将其编译操作设置为内容并始终复制。奇怪的是,它在桌面的属性对话框中设置了正确的壁纸名称。但是,壁纸未设置。我的代码如下所示:
public sealed class Wallpaper
{
Wallpaper() { }
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public static void Set(string wpaper, Style style)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
string tempPath = "Resources\\"+wpaper;
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
tempPath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
当调用Wallpaper.Set("wpapername")时,它从项目资源中获取壁纸。它适用于Win7,但不适用于WinXP。难道我做错了什么?