DisplayMagician/DisplayMagicianShared/Wallpaper.cs

119 lines
3.6 KiB
C#
Raw Normal View History

2021-08-27 09:15:53 +00:00
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
2021-08-27 09:15:53 +00:00
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace DisplayMagicianShared
{
public sealed class Wallpaper
{
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 enum Style : int
{
Fill = 0,
Fit = 1,
Stretch = 2,
Tile = 3,
Center = 4,
Span = 5
}
public enum Mode : int
{
DoNothing = 0,
Clear = 1,
Apply = 2
2021-08-27 09:15:53 +00:00
}
public static bool Set(String filename, Style style)
2021-08-27 09:15:53 +00:00
{
//System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());
2021-08-27 09:15:53 +00:00
Bitmap img = new Bitmap(filename);
2021-08-27 09:15:53 +00:00
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Fill)
2021-08-27 09:15:53 +00:00
{
key.SetValue(@"WallpaperStyle", 10.ToString());
2021-08-27 09:15:53 +00:00
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Fit)
2021-08-27 09:15:53 +00:00
{
key.SetValue(@"WallpaperStyle", 6.ToString());
2021-08-27 09:15:53 +00:00
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Span) // Windows 8 or newer only!
2021-08-27 09:15:53 +00:00
{
key.SetValue(@"WallpaperStyle", 22.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Stretch)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tile)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
2021-08-27 09:15:53 +00:00
key.SetValue(@"TileWallpaper", 1.ToString());
}
if (style == Style.Center)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
2021-08-27 09:15:53 +00:00
if (SystemParametersInfo(SPI_SETDESKWALLPAPER,
2021-08-27 09:15:53 +00:00
0,
filename,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE) > 0)
{
// applying desktop wallpaper worked!
return true;
}
else
{
// applying desktop wallpaper failed!
return false;
}
2021-08-27 09:15:53 +00:00
}
public static bool Clear()
{
RegistryKey desktopKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
desktopKey.SetValue(@"WallPaper", "");
RegistryKey explorerKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers", true);
explorerKey.SetValue(@"BackgroundType", 1);
if (SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
"",
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE) > 0)
{
// applying desktop wallpaper worked!
return true;
}
else
{
// applying desktop wallpaper failed!
return false;
}
}
2021-08-27 09:15:53 +00:00
}
}