From e3d78d3dc17cf4e39d9cc7c5ec3e664a8a98575e Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Mon, 1 Nov 2021 21:48:40 +1300 Subject: [PATCH] Fixed saving shortcut icon --- DisplayMagician/Properties/AssemblyInfo.cs | 4 +- DisplayMagician/ShortcutItem.cs | 53 +++++++++------------- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index 282977e..8487dd3 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.1.0.83")] -[assembly: AssemblyFileVersion("2.1.0.83")] +[assembly: AssemblyVersion("2.1.0.89")] +[assembly: AssemblyFileVersion("2.1.0.89")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagician/ShortcutItem.cs b/DisplayMagician/ShortcutItem.cs index 6f871b5..dfefe58 100644 --- a/DisplayMagician/ShortcutItem.cs +++ b/DisplayMagician/ShortcutItem.cs @@ -1006,30 +1006,21 @@ namespace DisplayMagician public void ReplaceShortcutIconInCache() { - string newShortcutIconFilename; - // Work out the name of the shortcut we'll save. - newShortcutIconFilename = Path.Combine(Program.AppShortcutPath, $"{UUID}.ico"); - logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename is {newShortcutIconFilename}."); - - // If the new shortcut icon should be named differently - // then change the name of it - if (!newShortcutIconFilename.Equals(_savedShortcutIconCacheFilename)) + // Figure out if we need to remove the old file + if (_savedShortcutIconCacheFilename != null) { - logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename {newShortcutIconFilename} is different to the old shortcut Icon filename {_savedShortcutIconCacheFilename}."); - if (System.IO.File.Exists(_savedShortcutIconCacheFilename)) + // Work out the name of the shortcut we'll save. + string oldShortcutIconFilename = _savedShortcutIconCacheFilename; + logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Old shortcut Icon filename is {oldShortcutIconFilename}."); + if (System.IO.File.Exists(oldShortcutIconFilename)) { - logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Deleting old shortcut Icon filename {_savedShortcutIconCacheFilename}."); - System.IO.File.Delete(_savedShortcutIconCacheFilename); + logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Deleting old shortcut Icon filename {oldShortcutIconFilename}."); + System.IO.File.Delete(oldShortcutIconFilename); } - logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: Creating the new shortcut Icon filename {newShortcutIconFilename} (calling SaveShortcutIconToCache)."); - SaveShortcutIconToCache(); } - else - { - logger.Trace($"ShortcutItem/ReplaceShortcutIconInCache: New shortcut Icon filename {newShortcutIconFilename} matches old shortcut Icon filename {_savedShortcutIconCacheFilename} so skipping the rename."); - } - + // Now we save the new file + SaveShortcutIconToCache(); } @@ -1039,29 +1030,29 @@ namespace DisplayMagician // Work out the name of the shortcut we'll save. _savedShortcutIconCacheFilename = Path.Combine(Program.AppShortcutPath, $"{UUID}.ico"); logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Planning on saving shortcut icon to cache as {_savedShortcutIconCacheFilename}."); - Icon shortcutIcon; + MultiIcon shortcutIcon = new MultiIcon(); try { logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Creating Icon from Shortcut bitmap."); - // Get an Hicon for the shortcutBitmap - IntPtr Hicon = _shortcutBitmap.GetHicon(); - // Create a new icon from the handle. - shortcutIcon = Icon.FromHandle(Hicon); + // Create a new + SingleIcon si = shortcutIcon.Add("icon"); + si.Add(_shortcutBitmap); + shortcutIcon.SelectedIndex = 0; logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving shortcut icon to cache with {_savedShortcutIconCacheFilename} as the name."); - using (FileStream fs = new FileStream(_savedShortcutIconCacheFilename, FileMode.Create)) - shortcutIcon.Save(fs); - + shortcutIcon.Save(_savedShortcutIconCacheFilename, MultiIconFormat.ICO); + } catch (Exception ex) { logger.Warn(ex, $"ShortcutItem/SaveShortcutIconToCache: Exception while trying to save the Shortcut icon."); - + shortcutIcon.Clear(); // If we fail to create an icon any other way, then we use the default profile icon logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Using the Display Profile icon for {_profileToUse.Name} as the icon instead."); - shortcutIcon = Properties.Resources.DisplayMagician; + SingleIcon si = shortcutIcon.Add("icon2"); + si.Add(Properties.Resources.DisplayMagician); + shortcutIcon.SelectedIndex = 0; logger.Trace($"ShortcutItem/SaveShortcutIconToCache: Saving the Display Profile icon for {_profileToUse.Name} to {_savedShortcutIconCacheFilename}."); - using (FileStream fs = new FileStream(_savedShortcutIconCacheFilename, FileMode.Create)) - shortcutIcon.Save(fs); + shortcutIcon.Save(_savedShortcutIconCacheFilename, MultiIconFormat.ICO); } }