【发布时间】:2012-01-14 22:10:46
【问题描述】:
我希望使用 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。我做错了吗?
【问题讨论】:
-
这个类似的question 可能正是你所需要的。
-
我的代码基于该代码,但我的壁纸需要使用该应用程序进行部署。
-
可能是XP没有处理相对路径,你可能需要指定壁纸的整个路径。
标签: c# settings windows-xp desktop wallpaper