我正在制作一个简单的程序来更改计算机背景.我在网上发现一个*问题,该问题或多或少涵盖了我想做的事情.现在,我可以成功地将墙纸更改为从在线图像URL进行平铺,居中和拉伸.但是,在控制面板中,可以选择将墙纸放置在“适合”和“填充”位置.如何通过编程将墙纸设置为适合/填充模式?
相关代码:
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public class Wallpaper
{
public 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 void Set(string URL, Style style)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
System.Drawing.Image img = Image.FromStream(stream);
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);
}
}
适合/填充键不可用吗?我在网上搜索了一段时间,但只发现平铺,居中和拉伸.
解决方法:
这可能对您有帮助.
case WallpaperStyle.Fit: // (Windows 7 and later)
key.SetValue(@"WallpaperStyle", "6");
key.SetValue(@"TileWallpaper", "0");
break;
case WallpaperStyle.Fill: // (Windows 7 and later)
key.SetValue(@"WallpaperStyle", "10");
key.SetValue(@"TileWallpaper", "0");
break;
我相信您可以轻松地使其适应您的代码.