C# 使用c更改墙纸样式#
我正在开发一个基本的winform应用程序,我想用c#更改壁纸,问题是图像没有拉伸到适合屏幕大小。C# 使用c更改墙纸样式#,c#,.net,winforms,C#,.net,Winforms,我正在开发一个基本的winform应用程序,我想用c#更改壁纸,问题是图像没有拉伸到适合屏幕大小。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace DailyWallpaper { class Wallpape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DailyWallpaper
{
class WallpaperChanger
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(
UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);
private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;
public void SetWallpaper(String path)
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
}
有谁能告诉我如何纠正此错误,请提前感谢。尝试执行以下代码:
public void SetWallpaper(String path)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
编辑: 您还可以修改函数并向其传递样式:
public void SetWallpaper(String path, 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());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
谢谢你的帮助answer@SijoJose不客气!让我知道你是否需要这些:)这是我真正想要的,谢谢你的帮助。@SijoJose如果有帖子回答了你的问题,请单击旁边的复选标记接受它。你这样做会赢得声誉。