1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Wallpaper
{
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
public Wallpaper()
{
}
[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 void SetWallpaper(string path, Style style)
{
if (System.IO.File.Exists(path))
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
switch (style)
{
case Style.Stretched:
key.SetValue(@"WallpaperStyle", "2");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Centered:
key.SetValue(@"WallpaperStyle", "1");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Tiled:
key.SetValue(@"WallpaperStyle", "1");
key.SetValue(@"TileWallpaper", "1");
break;
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
else
{
throw new System.IO.FileNotFoundException();
}
}
}
|