From cf0a15acdded70968cada264aa4a34adc4e1ed87 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 27 Aug 2021 21:15:53 +1200 Subject: [PATCH] Add initial wallpaper changing code --- DisplayMagician/Program.cs | 1 + .../DisplayMagicianShared.csproj | 1 + DisplayMagicianShared/ProfileItem.cs | 23 +++++++ DisplayMagicianShared/ProfileRepository.cs | 38 +----------- DisplayMagicianShared/Wallpaper.cs | 62 +++++++++++++++++++ 5 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 DisplayMagicianShared/Wallpaper.cs diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index a505564..5e25320 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -35,6 +35,7 @@ namespace DisplayMagician { public static string AppIconPath = Path.Combine(Program.AppDataPath, $"Icons"); public static string AppProfilePath = Path.Combine(Program.AppDataPath, $"Profiles"); public static string AppShortcutPath = Path.Combine(Program.AppDataPath, $"Shortcuts"); + public static string AppWallpaperPath = Path.Combine(Program.AppDataPath, $"Wallpaper"); public static string AppLogPath = Path.Combine(Program.AppDataPath, $"Logs"); public static string AppDisplayMagicianIconFilename = Path.Combine(AppIconPath, @"DisplayMagician.ico"); public static string AppOriginIconFilename = Path.Combine(AppIconPath, @"Origin.ico"); diff --git a/DisplayMagicianShared/DisplayMagicianShared.csproj b/DisplayMagicianShared/DisplayMagicianShared.csproj index 29460cf..fef1de1 100644 --- a/DisplayMagicianShared/DisplayMagicianShared.csproj +++ b/DisplayMagicianShared/DisplayMagicianShared.csproj @@ -85,6 +85,7 @@ DisplayView.cs + diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index f8cda05..e881a09 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -21,11 +21,13 @@ namespace DisplayMagicianShared private List _profileDisplayIdentifiers = new List(); internal static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician"); + private static string AppWallpaperPath = Path.Combine(AppDataPath, $"Wallpaper"); private static readonly string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$"; private string _uuid = ""; private bool _isPossible = false; private Keys _hotkey = Keys.None; + private string _wallpaperBitmapFilename = ""; #region JsonConverterBitmap @@ -178,6 +180,19 @@ namespace DisplayMagicianShared public string SavedProfileIconCacheFilename { get; set; } + public bool SetWallpaper { get; set; } + + public string WallpaperBitmapFilename{ + get + { + return _wallpaperBitmapFilename; + } + set + { + _wallpaperBitmapFilename = value; + } + } + public List ProfileDisplayIdentifiers { get @@ -268,7 +283,13 @@ namespace DisplayMagicianShared ProfileBitmap is Bitmap && ProfileTightestBitmap is Bitmap && ProfileDisplayIdentifiers.Count > 0) + { + if (SetWallpaper && WallpaperBitmapFilename == "") + return false; + return true; + } + else return false; } @@ -291,6 +312,8 @@ namespace DisplayMagicianShared profile.ProfileBitmap = ProfileBitmap; profile.ProfileTightestBitmap = ProfileTightestBitmap; profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; + profile.SetWallpaper = SetWallpaper; + profile.WallpaperBitmapFilename = WallpaperBitmapFilename; return true; } diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index dae6197..6165ae7 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -212,6 +212,7 @@ namespace DisplayMagicianShared try { File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); + File.Delete(ProfileToRemove.WallpaperBitmapFilename); } catch (UnauthorizedAccessException ex) { @@ -263,6 +264,7 @@ namespace DisplayMagicianShared try { File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); + File.Delete(ProfileToRemove.WallpaperBitmapFilename); } catch (UnauthorizedAccessException ex) { @@ -312,6 +314,7 @@ namespace DisplayMagicianShared try { File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); + File.Delete(ProfileToRemove.WallpaperBitmapFilename); } catch (UnauthorizedAccessException ex) { @@ -498,41 +501,6 @@ namespace DisplayMagicianShared } - - /*public static void UpdateActiveProfile() - { - - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); - - ProfileItem activeProfile = new ProfileItem - { - Name = "Current Display Profile", - Paths = PathInfo.GetActivePaths().Select(info => new DisplayMagicianShared.Topology.Path(info)).ToArray() - }; - - activeProfile.ProfileIcon = new ProfileIcon(activeProfile); - activeProfile.ProfileBitmap = activeProfile.ProfileIcon.ToBitmap(256, 256); - - if (_profilesLoaded && _allProfiles.Count > 0) - { - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) - { - if (activeProfile.Paths.SequenceEqual(loadedProfile.Paths)) - { - _currentProfile = loadedProfile; - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The profile {loadedProfile.Name} is currently active (in use now)."); - return; - } - } - } - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); - _currentProfile = activeProfile; - - //IsPossibleRefresh(); - - }*/ - - public static void UpdateActiveProfile() { diff --git a/DisplayMagicianShared/Wallpaper.cs b/DisplayMagicianShared/Wallpaper.cs new file mode 100644 index 0000000..c2c8cdd --- /dev/null +++ b/DisplayMagicianShared/Wallpaper.cs @@ -0,0 +1,62 @@ +using Microsoft.Win32; +using System; +using System.Collections.Generic; +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 + { + Tiled, + Centered, + Stretched + } + + public static void SetAndSave(Uri uri, Style style, string filename) + { + System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString()); + + System.Drawing.Image img = System.Drawing.Image.FromStream(s); + img.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp); + + 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, + filename, + SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); + } + } +}