From 58c41cf5f78e45e87e2d1782fe40c6b6869fedbd Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 21 May 2021 09:37:45 +1200 Subject: [PATCH] Added Hotkey Already Registered checks Added to alert the user and record when a hotkey is unable to be registered due to it already being used. --- DisplayMagician/ImageUtils.cs | 33 ++++++++++-- DisplayMagician/Program.cs | 11 +++- DisplayMagician/UIForms/MainForm.cs | 78 +++++++++++++++++++++++++---- 3 files changed, 106 insertions(+), 16 deletions(-) diff --git a/DisplayMagician/ImageUtils.cs b/DisplayMagician/ImageUtils.cs index 8f437ff..996a61a 100644 --- a/DisplayMagician/ImageUtils.cs +++ b/DisplayMagician/ImageUtils.cs @@ -174,7 +174,11 @@ namespace DisplayMagician if (bm.Width > bmToReturn.Width && bm.Height > bmToReturn.Height) { bmToReturn = bm; - logger.Trace($"ShortcutItem/GetMeABitmapFromFile: This new bitmap from the icon file {fileNameAndPath} is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the icon file {fileNameAndPath} using standard Icon access method is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + } + else + { + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the icon file {fileNameAndPath} using standard Icon access method is smaller or the same size as the previous one at {bm.Width} x {bm.Height}, so using that instead."); } } catch (Exception ex) @@ -196,7 +200,11 @@ namespace DisplayMagician if (bm.Width > bmToReturn.Width && bm.Height > bmToReturn.Height) { bmToReturn = bm; - logger.Trace($"ShortcutItem/GetMeABitmapFromFile: This new bitmap from the icon file {fileNameAndPath} is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the icon file {fileNameAndPath} using MultiIcon access method is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + } + else + { + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the icon file {fileNameAndPath} using MultiIcon access method is smaller or the same size as the previous one at {bm.Width} x {bm.Height}, so using that instead."); } } } @@ -241,7 +249,11 @@ namespace DisplayMagician if (bm.Width > bmToReturn.Width && bm.Height > bmToReturn.Height) { bmToReturn = bm; - logger.Trace($"ShortcutItem/GetMeABitmapFromFile: This new bitmap from the icon file {fileNameAndPath} is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the exe file {fileNameAndPath} using TsudaKageyu.IconExtractor access method is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + } + else + { + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the exe file {fileNameAndPath} using TsudaKageyu.IconExtractor access method is smaller or the same size as the previous one at {bm.Width} x {bm.Height}, so using that instead."); } } } @@ -277,8 +289,16 @@ namespace DisplayMagician if (bm.Width > bmToReturn.Width && bm.Height > bmToReturn.Height) { bmToReturn = bm; - logger.Trace($"ShortcutItem/GetMeABitmapFromFile: This new bitmap from the icon file {fileNameAndPath} is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the file {fileNameAndPath} using MintPlayer.IconUtils.IconExtractor access method is larger than the previous one at {bm.Width} x {bm.Height}, so using that instead."); } + else + { + logger.Trace($"ShortcutItem/GetMeABitmapFromFile: New bitmap from the file {fileNameAndPath} using MintPlayer.IconUtils.IconExtractor access method is smaller or the same size as the previous one at {bm.Width} x {bm.Height}, so using that instead."); + } + } + else + { + logger.Warn($"ShortcutItem/GetMeABitmapFromFile: Couldn't extract an Icon from the file {fileNameAndPath} using MintPlayer.IconUtils.IconExtractor access method, so can't try to get the Icon using IconUtils.TryGetIcon."); } } @@ -318,8 +338,10 @@ namespace DisplayMagician logger.Warn($"ShortcutItem/GetMeABitmapFromFile2: The fileNamesAndPaths list is empty! Can't get the bitmap from the files."); return null; } + logger.Trace($"ShortcutItem/GetMeABitmapFromFile2: We have {fileNamesAndPaths.Count} files to try and extract a bitmap from."); foreach (string fileNameAndPath in fileNamesAndPaths) { + logger.Trace($"ShortcutItem/GetMeABitmapFromFile2: Getting a bitmap from {fileNameAndPath} by running GetMeABitmapFromFile."); Bitmap bm = GetMeABitmapFromFile(fileNameAndPath); if (bmToReturn == null) @@ -330,10 +352,11 @@ namespace DisplayMagician { bmToReturn = bm; } + logger.Trace($"ShortcutItem/GetMeABitmapFromFile2: The biggest bitmap we could get from {fileNameAndPath} was {bm.Width}x{bm.Height}."); } // Now we check if the icon is still too small. - + logger.Trace($"ShortcutItem/GetMeABitmapFromFile2: The biggest bitmap we could get from the {fileNamesAndPaths.Count} files was {bmToReturn.Width}x{bmToReturn.Height}."); return bmToReturn; } diff --git a/DisplayMagician/Program.cs b/DisplayMagician/Program.cs index 6fce682..43111e2 100644 --- a/DisplayMagician/Program.cs +++ b/DisplayMagician/Program.cs @@ -911,12 +911,21 @@ namespace DisplayMagician { // We only want the icon location that the GameLibrary told us to use // Note: This may be an icon file, or an exe file. // This function tries to get a 256x256 Vista sized bitmap from the file + logger.Trace($"Program/LoadGamesInBackground: Attempting to get game bitmaps from {game.Name}."); bm = ImageUtils.GetMeABitmapFromFile(game.IconPath); + if (bm != null && bm.GetType() == typeof(Bitmap)) + { + logger.Trace($"Program/LoadGamesInBackground: Got game bitmaps from {game.Name}."); + } + else + { + logger.Trace($"Program/LoadGamesInBackground: Couldn't get game bitmaps from {game.Name} for some reason."); + } } catch (Exception ex) { - logger.Error(ex, $"Program/LoadGamesInBackground: Exception building game bitmaps during load"); + logger.Error(ex, $"Program/LoadGamesInBackground: Exception building game bitmaps for {game.Name} during load"); } if (bm == null) diff --git a/DisplayMagician/UIForms/MainForm.cs b/DisplayMagician/UIForms/MainForm.cs index a59e3fc..696fe01 100644 --- a/DisplayMagician/UIForms/MainForm.cs +++ b/DisplayMagician/UIForms/MainForm.cs @@ -42,21 +42,68 @@ namespace DisplayMagician.UIForms notifyIcon.ContextMenuStrip = mainContextMenuStrip; RefreshNotifyIconMenus(); - if (Program.AppProgramSettings.HotkeyMainWindow != Keys.None) - HotkeyManager.Current.AddOrReplace("HotkeyMainWindow", Program.AppProgramSettings.HotkeyMainWindow, OnWindowHotkeyPressed); - if (Program.AppProgramSettings.HotkeyDisplayProfileWindow != Keys.None) - HotkeyManager.Current.AddOrReplace("HotkeyDisplayProfileWindow", Program.AppProgramSettings.HotkeyDisplayProfileWindow, OnWindowHotkeyPressed); - if (Program.AppProgramSettings.HotkeyShortcutLibraryWindow != Keys.None) + try + { + if (Program.AppProgramSettings.HotkeyMainWindow != Keys.None) + HotkeyManager.Current.AddOrReplace("HotkeyMainWindow", Program.AppProgramSettings.HotkeyMainWindow, OnWindowHotkeyPressed); + } + catch (HotkeyAlreadyRegisteredException ex) + { + logger.Warn(ex, $"MainForm/MainForm: The Hotkey to open the Main Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it."); + MessageBox.Show( + $"The '{Program.AppProgramSettings.HotkeyMainWindow}' Hotkey you set to open the Main Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it.", @"Can't set Main Window Hotkey", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + try + { + if (Program.AppProgramSettings.HotkeyDisplayProfileWindow != Keys.None) + HotkeyManager.Current.AddOrReplace("HotkeyDisplayProfileWindow", Program.AppProgramSettings.HotkeyDisplayProfileWindow, OnWindowHotkeyPressed); + + } + catch (HotkeyAlreadyRegisteredException ex) + { + logger.Warn(ex, $"MainForm/MainForm: The Hotkey to open the Display Profile Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it."); + MessageBox.Show( + $"The '{Program.AppProgramSettings.HotkeyDisplayProfileWindow}' Hotkey you set to open the Display Profile Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it.", @"Can't set Display Profile Window Hotkey", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + try + { + if (Program.AppProgramSettings.HotkeyShortcutLibraryWindow != Keys.None) HotkeyManager.Current.AddOrReplace("HotkeyShortcutLibraryWindow", Program.AppProgramSettings.HotkeyShortcutLibraryWindow, OnWindowHotkeyPressed); - + + } + catch (HotkeyAlreadyRegisteredException ex) + { + logger.Warn(ex, $"MainForm/MainForm: The Hotkey to open the Shortcut Library Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it."); + MessageBox.Show( + $"The '{Program.AppProgramSettings.HotkeyShortcutLibraryWindow}' Hotkey you set to open the Shortcut Library Window is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it.", @"Can't set Shortcut Library Window Hotkey", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } // Add all the Profile Hotkeys that are set foreach (ProfileItem myProfile in ProfileRepository.AllProfiles) { if (myProfile.Hotkey != Keys.None) { - hotkeyDisplayProfiles.Add(myProfile.UUID); - HotkeyManager.Current.AddOrReplace(myProfile.UUID, myProfile.Hotkey, OnWindowHotkeyPressed); + try + { + HotkeyManager.Current.AddOrReplace(myProfile.UUID, myProfile.Hotkey, OnWindowHotkeyPressed); + hotkeyDisplayProfiles.Add(myProfile.UUID); + } + catch (HotkeyAlreadyRegisteredException ex) + { + logger.Warn(ex, $"MainForm/MainForm: The '{myProfile.Hotkey}' Hotkey you set to run the {myProfile.Name} Display Profile is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it."); + MessageBox.Show( + $"The '{myProfile.Hotkey}' Hotkey you set to run the {myProfile.Name} Display Profile is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it.", $"Can't set {myProfile.Name} Display Profile Hotkey", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } } } @@ -65,8 +112,19 @@ namespace DisplayMagician.UIForms { if (myShortcut.Hotkey != Keys.None) { - hotkeyShortcuts.Add(myShortcut.UUID); - HotkeyManager.Current.AddOrReplace(myShortcut.UUID, myShortcut.Hotkey, OnWindowHotkeyPressed); + try + { + HotkeyManager.Current.AddOrReplace(myShortcut.UUID, myShortcut.Hotkey, OnWindowHotkeyPressed); + hotkeyShortcuts.Add(myShortcut.UUID); + } + catch (HotkeyAlreadyRegisteredException ex) + { + logger.Warn(ex, $"MainForm/MainForm: The '{myShortcut.Hotkey}' Hotkey you set to run the {myShortcut.Name} Shortcut is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it."); + MessageBox.Show( + $"The '{myShortcut.Hotkey}' Hotkey you set to run the {myShortcut.Name} Shortcut is already registered by something else! We cannot use that Hotkey. Please choose another Hotkey, or stop the other application from using it.", $"Can't set {myShortcut.Name} Shortcut Hotkey", + MessageBoxButtons.OK, + MessageBoxIcon.Error); + } } } // And now connect up our processing function