我正在制作一个简单的程序来改变我的计算机背景。我发现在线的stackoverflow问题或多或少地涵盖了我想做的事情。我现在可以成功地将我的壁纸更改为平铺,居中和从在线图像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);
}
}
适合/填充的键是否不可用?我在网上搜索了一会儿,只发现了平铺,居中和拉伸。
答案 0 :(得分:4)
这可能会对你有帮助。
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;
我相信您可以轻松地将其改编为您的代码。