我希望使用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。我做错了吗?
答案 0 :(得分:12)
嗯,这有点尴尬,但我会回答我自己的问题。
我不得不重复使用接受的答案here中的更多代码。 基本上XP中的问题是它需要使用bmp文件,所以我设法使用前面的例子和一些调整将项目资源转换为bmp文件。 Set方法以这种方式完美运行:
public static void Set(string wpaper, Style style)
{
using(System.Drawing.Image img = System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)))
{
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
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());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
tempPath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
重要的部分是在这段代码的第三行(System.Drawing.Image.FromFile(Path.GetFullPath(wpaper));
)。
答案 1 :(得分:8)
获得良好可靠的解决方案。
将foillowing类添加到项目中
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace XXXNAMESPACEXXX
{
public class Wallpaper
{
public enum Style : int
{
Tiled,
Centered,
Stretched
}
[DllImport("user32.dll")]
public static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);
public static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
public static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
public static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;
public static bool Set(string filePath, Style style)
{
bool Success = false;
try
{
Image i = System.Drawing.Image.FromFile(Path.GetFullPath(filePath));
Set(i, style);
Success = true;
}
catch //(Exception ex)
{
//ex.HandleException();
}
return Success;
}
public static bool Set(Image image, Style style)
{
bool Success = false;
try
{
string TempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
image.Save(TempPath, ImageFormat.Bmp);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
switch (style)
{
case Style.Stretched:
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
break;
case Style.Centered:
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
break;
default:
case Style.Tiled:
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
break;
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, TempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
Success = true;
}
catch //(Exception ex)
{
//ex.HandleException();
}
return Success;
}
}
}
注意:将XXXNAMESPACEXXX替换为项目的默认命名空间。
然后它可以像下面一样使用:
string FilePath = TxtFilePath.Text;
Wallpaper.Set(FilePath, Wallpaper.Style.Centered);
它也可以像这样使用:
if(Wallpaper.Set(FilePath, Wallpaper.Style.Centered))
{
MessageBox.Show("Your wallpaper has been set to " + FilePath);
}
else
{
MessageBox.Show("There was a problem setting the wallpaper.");
}
这适用于Windows XP,7,8,8.1和Windows 10。
注意值得注意的是,此方法将绕过网络管理员应用的任何桌面壁纸安全限制。