Add initial wallpaper changing code

This commit is contained in:
Terry MacDonald 2021-08-27 21:15:53 +12:00
parent fd98c2e5ca
commit cf0a15acdd
5 changed files with 90 additions and 35 deletions

View File

@ -35,6 +35,7 @@ namespace DisplayMagician {
public static string AppIconPath = Path.Combine(Program.AppDataPath, $"Icons"); public static string AppIconPath = Path.Combine(Program.AppDataPath, $"Icons");
public static string AppProfilePath = Path.Combine(Program.AppDataPath, $"Profiles"); public static string AppProfilePath = Path.Combine(Program.AppDataPath, $"Profiles");
public static string AppShortcutPath = Path.Combine(Program.AppDataPath, $"Shortcuts"); 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 AppLogPath = Path.Combine(Program.AppDataPath, $"Logs");
public static string AppDisplayMagicianIconFilename = Path.Combine(AppIconPath, @"DisplayMagician.ico"); public static string AppDisplayMagicianIconFilename = Path.Combine(AppIconPath, @"DisplayMagician.ico");
public static string AppOriginIconFilename = Path.Combine(AppIconPath, @"Origin.ico"); public static string AppOriginIconFilename = Path.Combine(AppIconPath, @"Origin.ico");

View File

@ -85,6 +85,7 @@
<Compile Include="UserControls\DisplayView.Designer.cs"> <Compile Include="UserControls\DisplayView.Designer.cs">
<DependentUpon>DisplayView.cs</DependentUpon> <DependentUpon>DisplayView.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Wallpaper.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">

View File

@ -21,11 +21,13 @@ namespace DisplayMagicianShared
private List<string> _profileDisplayIdentifiers = new List<string>(); private List<string> _profileDisplayIdentifiers = new List<string>();
internal static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician"); 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 static readonly string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$";
private string _uuid = ""; private string _uuid = "";
private bool _isPossible = false; private bool _isPossible = false;
private Keys _hotkey = Keys.None; private Keys _hotkey = Keys.None;
private string _wallpaperBitmapFilename = "";
#region JsonConverterBitmap #region JsonConverterBitmap
@ -178,6 +180,19 @@ namespace DisplayMagicianShared
public string SavedProfileIconCacheFilename { get; set; } public string SavedProfileIconCacheFilename { get; set; }
public bool SetWallpaper { get; set; }
public string WallpaperBitmapFilename{
get
{
return _wallpaperBitmapFilename;
}
set
{
_wallpaperBitmapFilename = value;
}
}
public List<string> ProfileDisplayIdentifiers public List<string> ProfileDisplayIdentifiers
{ {
get get
@ -268,7 +283,13 @@ namespace DisplayMagicianShared
ProfileBitmap is Bitmap && ProfileBitmap is Bitmap &&
ProfileTightestBitmap is Bitmap && ProfileTightestBitmap is Bitmap &&
ProfileDisplayIdentifiers.Count > 0) ProfileDisplayIdentifiers.Count > 0)
{
if (SetWallpaper && WallpaperBitmapFilename == "")
return false;
return true; return true;
}
else else
return false; return false;
} }
@ -291,6 +312,8 @@ namespace DisplayMagicianShared
profile.ProfileBitmap = ProfileBitmap; profile.ProfileBitmap = ProfileBitmap;
profile.ProfileTightestBitmap = ProfileTightestBitmap; profile.ProfileTightestBitmap = ProfileTightestBitmap;
profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers;
profile.SetWallpaper = SetWallpaper;
profile.WallpaperBitmapFilename = WallpaperBitmapFilename;
return true; return true;
} }

View File

@ -212,6 +212,7 @@ namespace DisplayMagicianShared
try try
{ {
File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); File.Delete(ProfileToRemove.SavedProfileIconCacheFilename);
File.Delete(ProfileToRemove.WallpaperBitmapFilename);
} }
catch (UnauthorizedAccessException ex) catch (UnauthorizedAccessException ex)
{ {
@ -263,6 +264,7 @@ namespace DisplayMagicianShared
try try
{ {
File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); File.Delete(ProfileToRemove.SavedProfileIconCacheFilename);
File.Delete(ProfileToRemove.WallpaperBitmapFilename);
} }
catch (UnauthorizedAccessException ex) catch (UnauthorizedAccessException ex)
{ {
@ -312,6 +314,7 @@ namespace DisplayMagicianShared
try try
{ {
File.Delete(ProfileToRemove.SavedProfileIconCacheFilename); File.Delete(ProfileToRemove.SavedProfileIconCacheFilename);
File.Delete(ProfileToRemove.WallpaperBitmapFilename);
} }
catch (UnauthorizedAccessException ex) 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() public static void UpdateActiveProfile()
{ {

View File

@ -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);
}
}
}