From 0a9921fb8b11ace5022a538eae306d61cfa52e74 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Tue, 24 Mar 2020 14:39:43 +0100 Subject: [PATCH] [Feature] CodeBuity&Refactor #49, [Feature] Code review tool #10 --- Controls/AboutBox.cs | 23 ++++++++++------------- Controls/AppContextMenu.cs | 2 +- Controls/AppNotifyIcon.cs | 1 + Controls/AskHotKeyForm.cs | 7 ++++--- Controls/Menu.cs | 3 +-- Controls/RowData.cs | 2 ++ Handler/KeyboardInput.cs | 1 + Helper/BringWindowToTop.cs | 2 +- Helper/FadeForm.cs | 1 + Helper/File/FileIni.cs | 6 ++++-- Helper/File/FileUrl.cs | 2 +- Helper/File/IconReader.cs | 20 +++++++++++--------- Properties/AssemblyInfo.cs | 4 ++++ SystemTrayMenu.cs | 3 +-- 14 files changed, 43 insertions(+), 34 deletions(-) diff --git a/Controls/AboutBox.cs b/Controls/AboutBox.cs index 95e1e04..48511d7 100644 --- a/Controls/AboutBox.cs +++ b/Controls/AboutBox.cs @@ -251,7 +251,7 @@ namespace SystemTrayMenu.Controls // Description = AssemblyDescription string // Title = AssemblyTitle string // - private NameValueCollection AssemblyAttribs(Assembly a) + private static NameValueCollection AssemblyAttribs(Assembly a) { string TypeName; string Name; @@ -379,18 +379,17 @@ namespace SystemTrayMenu.Controls // // reads an HKLM Windows Registry key value // - private string RegistryHklmValue(string KeyName, string SubKeyRef) + private static string RegistryHklmValue(string KeyName, string SubKeyRef) { - RegistryKey rk; + string strSysInfoPath = string.Empty; try { - rk = Registry.LocalMachine.OpenSubKey(KeyName); - return (string)rk.GetValue(SubKeyRef, ""); - } - catch (Exception) - { - return ""; + RegistryKey rk = Registry.LocalMachine.OpenSubKey(KeyName); + strSysInfoPath = (string)rk.GetValue(SubKeyRef, string.Empty); } + catch (Exception) { } + + return strSysInfoPath; } // @@ -398,9 +397,7 @@ namespace SystemTrayMenu.Controls // private void ShowSysInfo() { - string strSysInfoPath = ""; - - strSysInfoPath = RegistryHklmValue(@"SOFTWARE\Microsoft\Shared Tools Location", "MSINFO"); + string strSysInfoPath = RegistryHklmValue(@"SOFTWARE\Microsoft\Shared Tools Location", "MSINFO"); if (string.IsNullOrEmpty(strSysInfoPath)) { strSysInfoPath = RegistryHklmValue(@"SOFTWARE\Microsoft\Shared Tools\MSINFO", "PATH"); @@ -434,7 +431,7 @@ namespace SystemTrayMenu.Controls // // populate a listview with the specified key and value strings // - private void Populate(ListView lvw, string Key, string Value) + private static void Populate(ListView lvw, string Key, string Value) { if (!string.IsNullOrEmpty(Value)) { diff --git a/Controls/AppContextMenu.cs b/Controls/AppContextMenu.cs index df1984a..7d37ceb 100644 --- a/Controls/AppContextMenu.cs +++ b/Controls/AppContextMenu.cs @@ -227,7 +227,7 @@ namespace SystemTrayMenu.Controls /// https://www.codeproject.com/Tips/744914/ /// Sorted-list-of-available-cultures-in-NET /// - public IEnumerable GetCultureList( + private static IEnumerable GetCultureList( CultureTypes cultureType = CultureTypes.SpecificCultures) { List cultureList = CultureInfo.GetCultures(cultureType).ToList(); diff --git a/Controls/AppNotifyIcon.cs b/Controls/AppNotifyIcon.cs index 21e95de..9c21572 100644 --- a/Controls/AppNotifyIcon.cs +++ b/Controls/AppNotifyIcon.cs @@ -86,6 +86,7 @@ namespace SystemTrayMenu { notifyIcon.Icon = null; notifyIcon.Dispose(); + load.Dispose(); } public void LoadingStart() diff --git a/Controls/AskHotKeyForm.cs b/Controls/AskHotKeyForm.cs index 995091a..327384f 100644 --- a/Controls/AskHotKeyForm.cs +++ b/Controls/AskHotKeyForm.cs @@ -6,7 +6,8 @@ namespace SystemTrayMenu.Controls { public partial class AskHotKeyForm : Form { - public string NewHotKey; + public string NewHotKey => newHotKey; + private string newHotKey = string.Empty; public AskHotKeyForm() { @@ -102,13 +103,13 @@ namespace SystemTrayMenu.Controls case Keys.F22: case Keys.F23: case Keys.F24: - NewHotKey = keys.ToString(); + newHotKey = keys.ToString(); DialogResult = DialogResult.OK; Close(); break; case Keys.Back: case Keys.Delete: - NewHotKey = string.Empty; + newHotKey = string.Empty; DialogResult = DialogResult.OK; Close(); break; diff --git a/Controls/Menu.cs b/Controls/Menu.cs index 400f418..0dce6da 100644 --- a/Controls/Menu.cs +++ b/Controls/Menu.cs @@ -174,8 +174,7 @@ namespace SystemTrayMenu Location = new Point(x, y); } - public void AdjustLocationAndSize(int heightMax, int widthPredecessors, - Menu menuPredecessor) + public void AdjustLocationAndSize(int heightMax, Menu menuPredecessor) { DataGridViewElementStates states = DataGridViewElementStates.None; if (!autoResizeRowsDone) diff --git a/Controls/RowData.cs b/Controls/RowData.cs index 3ef5573..58e8e66 100644 --- a/Controls/RowData.cs +++ b/Controls/RowData.cs @@ -413,7 +413,9 @@ namespace SystemTrayMenu.Controls if (!isDisposed) { icon?.Dispose(); + Icon?.Dispose(); waitMenuOpen.Dispose(); + Reading.Dispose(); } isDisposed = true; } diff --git a/Handler/KeyboardInput.cs b/Handler/KeyboardInput.cs index 35cf39a..d50a45d 100644 --- a/Handler/KeyboardInput.cs +++ b/Handler/KeyboardInput.cs @@ -40,6 +40,7 @@ namespace SystemTrayMenu.Handler public void Dispose() { hook.Dispose(); + timerKeySearch.Dispose(); } internal void RegisterHotKey() diff --git a/Helper/BringWindowToTop.cs b/Helper/BringWindowToTop.cs index 1484a90..49ccc7f 100644 --- a/Helper/BringWindowToTop.cs +++ b/Helper/BringWindowToTop.cs @@ -4,7 +4,7 @@ using System.Runtime.InteropServices; namespace SystemTrayMenu.Helper { - public class WindowToTop + public static class WindowToTop { [DllImport("user32.dll")] private static extern bool IsIconic(IntPtr hWnd); diff --git a/Helper/FadeForm.cs b/Helper/FadeForm.cs index c77226c..2c3c373 100644 --- a/Helper/FadeForm.cs +++ b/Helper/FadeForm.cs @@ -39,6 +39,7 @@ namespace SystemTrayMenu timerFadeIn.Dispose(); timerFadeOut.Dispose(); timerFadeHalf.Dispose(); + form.Dispose(); } } diff --git a/Helper/File/FileIni.cs b/Helper/File/FileIni.cs index 94a9ad9..851bfdd 100644 --- a/Helper/File/FileIni.cs +++ b/Helper/File/FileIni.cs @@ -10,9 +10,11 @@ namespace SystemTrayMenu.Helper public FileIni(string path) { values = File.ReadLines(path) - .Where(line => (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))) + .Where(line => !string.IsNullOrWhiteSpace(line) && + !line.StartsWith("#", System.StringComparison.InvariantCulture)) .Select(line => line.Split(new char[] { '=' }, 2, 0)) - .ToDictionary(parts => parts[0].Trim(), parts => parts.Length > 1 ? parts[1].Trim() : null); + .ToDictionary(parts => parts[0].Trim(), parts => + parts.Length > 1 ? parts[1].Trim() : null); } public string Value(string name, string value = null) { diff --git a/Helper/File/FileUrl.cs b/Helper/File/FileUrl.cs index d3d5a32..8daf3b1 100644 --- a/Helper/File/FileUrl.cs +++ b/Helper/File/FileUrl.cs @@ -2,7 +2,7 @@ namespace SystemTrayMenu.Helper { - public class FileUrl + public static class FileUrl { private static string browserPath = string.Empty; public static string GetDefaultBrowserPath() diff --git a/Helper/File/IconReader.cs b/Helper/File/IconReader.cs index fadff68..058c32e 100644 --- a/Helper/File/IconReader.cs +++ b/Helper/File/IconReader.cs @@ -17,7 +17,7 @@ namespace SystemTrayMenu.Helper /// /// IconReader.GetFileIcon("c:\\general.xls"); /// - public class IconReader + public static class IconReader { private static readonly ConcurrentDictionary dictIconCache = new ConcurrentDictionary(); @@ -144,10 +144,10 @@ namespace SystemTrayMenu.Helper // Cleanup if (!linkOverlay) { - User32.DestroyIcon(hIcon); + NativeMethods.DestroyIco(hIcon); } - User32.DestroyIcon(shfi.hIcon); + NativeMethods.DestroyIco(shfi.hIcon); } return icon; @@ -207,7 +207,7 @@ namespace SystemTrayMenu.Helper } // Cleanup - User32.DestroyIcon(shfi.hIcon); + NativeMethods.DestroyIco(shfi.hIcon); } return icon; } @@ -231,7 +231,7 @@ namespace SystemTrayMenu.Helper // This code has been left largely untouched from that in the CRC example. The main changes have been moving // the icon reading code over to the IconReader type. - public class Shell32 + public static class Shell32 { public const int MAX_PATH = 256; @@ -336,7 +336,7 @@ namespace SystemTrayMenu.Helper /// /// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example. /// - public class User32 + public static class NativeMethods { /// /// Provides access to function required to delete handle. This method is used internally @@ -345,8 +345,10 @@ namespace SystemTrayMenu.Helper /// Pointer to icon handle. /// N/A [DllImport("User32.dll")] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", - "CA1401:P/Invokes should not be visible", Justification = "")] - public static extern int DestroyIcon(IntPtr hIcon); + private static extern int DestroyIcon(IntPtr hIcon); + public static void DestroyIco(IntPtr hIcon) + { + _ = DestroyIcon(hIcon); + } } } \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index c7942d7..f34317f 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Resources; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -21,6 +22,9 @@ using System.Runtime.InteropServices; // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("116c8741-a9b0-4560-8e82-7cf412894340")] +//https://stackoverflow.com/questions/4068360/c-sharp-warning-mark-assemblies-with-neutralresourceslanguageattribute +[assembly: NeutralResourcesLanguage("en")] + // Version information for an assembly consists of the following four values: // // Major Version diff --git a/SystemTrayMenu.cs b/SystemTrayMenu.cs index 9093458..68025c0 100644 --- a/SystemTrayMenu.cs +++ b/SystemTrayMenu.cs @@ -276,8 +276,7 @@ namespace SystemTrayMenu widthPredecessors -= newWith; } - menu.AdjustLocationAndSize(heightMax, - widthPredecessors, menuPredecessor); + menu.AdjustLocationAndSize(heightMax, menuPredecessor); widthPredecessors += menu.Width - menu.Padding.Left; menuPredecessor = menu; }