mirror of
https://github.com/terrymacdonald/DisplayMagician.git
synced 2024-08-30 18:32:20 +00:00
This is the initial windows desktop functionality, and may work. I've added it to the earlier version of DisplayMagician, but I'm not sure I'll release it. This will instead probably be part of v2.0.0.
88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using Microsoft.Win32;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
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
|
|
{
|
|
Tile,
|
|
Center,
|
|
Stretch,
|
|
Fill,
|
|
Fit,
|
|
Span
|
|
}
|
|
|
|
public static bool Set(String filename, Style style)
|
|
{
|
|
//System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());
|
|
|
|
Bitmap img = new Bitmap(filename);
|
|
|
|
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
|
|
if (style == Style.Fill)
|
|
{
|
|
key.SetValue(@"WallpaperStyle", 10.ToString());
|
|
key.SetValue(@"TileWallpaper", 0.ToString());
|
|
}
|
|
if (style == Style.Fit)
|
|
{
|
|
key.SetValue(@"WallpaperStyle", 6.ToString());
|
|
key.SetValue(@"TileWallpaper", 0.ToString());
|
|
}
|
|
if (style == Style.Span) // Windows 8 or newer only!
|
|
{
|
|
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());
|
|
key.SetValue(@"TileWallpaper", 1.ToString());
|
|
}
|
|
if (style == Style.Center)
|
|
{
|
|
key.SetValue(@"WallpaperStyle", 0.ToString());
|
|
key.SetValue(@"TileWallpaper", 0.ToString());
|
|
}
|
|
|
|
if (SystemParametersInfo(SPI_SETDESKWALLPAPER,
|
|
0,
|
|
filename,
|
|
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE) > 0)
|
|
{
|
|
// applying desktop wallpaper worked!
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// applying desktop wallpaper failed!
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|