This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetDesktop.cs
More file actions
101 lines (90 loc) · 3.16 KB
/
SetDesktop.cs
File metadata and controls
101 lines (90 loc) · 3.16 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Mark König, 03/2022
//
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace LoadBingPicture
{
public class SetDesktop
{
#region windows stuff
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, String pvParam, uint fWinIni);
private const uint SPI_SETDESKWALLPAPER = 0x14;
private const uint SPIF_UPDATEINIFILE = 0x1;
private const uint SPIF_SENDWININICHANGE = 0x2;
#endregion
public enum DesktopStyle : int
{
Fill,
Fit,
Span,
Stretch,
Tile,
Center
}
public static void DisplayPicture(string file_name, bool update_registry)
{
try
{
// If we should update the registry,
// set the appropriate flags.
uint flags = 0;
if (update_registry)
flags = SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE;
// Set the desktop background to this file.
if (!SystemParametersInfo(SPI_SETDESKWALLPAPER,
0, file_name, flags))
{
MessageBox.Show("SystemParametersInfo failed.",
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show("Error displaying picture " +
file_name + ".\n" + ex.Message,
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
public static void SetKey(DesktopStyle style)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == DesktopStyle.Fill)
{
key.SetValue(@"WallpaperStyle", 10.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == DesktopStyle.Fit)
{
key.SetValue(@"WallpaperStyle", 6.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == DesktopStyle.Span) // Windows 8 or newer only!
{
key.SetValue(@"WallpaperStyle", 22.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == DesktopStyle.Stretch)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == DesktopStyle.Tile)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
if (style == DesktopStyle.Center)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
}
}
}