From 3ead0424d2e6b93511042a97ec186f52f35909d3 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Mon, 23 Mar 2020 12:35:10 +0100 Subject: [PATCH] [Feature] CodeBuity&Refactor #49, [Feature] Code review tool #10 --- Controls/AboutBox.cs | 4 +-- Controls/AppNotifyIcon.cs | 9 +++--- Controls/Menu.cs | 2 +- Controls/RowData.cs | 4 +-- Handler/KeyboardInput.cs | 6 ++-- Handler/WaitFastLeave.cs | 2 +- Handler/WaitMenuOpen.cs | 2 +- Helper/FadeForm.cs | 8 +++--- Helper/File/FileIni.cs | 4 +-- Helper/File/IconReader.cs | 2 +- Helper/KeyboardHook.cs | 8 +++--- Helper/Log.cs | 2 +- Helper/ShellContextMenu.cs | 52 +++++++++++++++++------------------ Helper/WindowsExplorerSort.cs | 2 +- SystemTrayMenu.cs | 14 +++++----- 15 files changed, 61 insertions(+), 60 deletions(-) diff --git a/Controls/AboutBox.cs b/Controls/AboutBox.cs index 4c91206..3940c06 100644 --- a/Controls/AboutBox.cs +++ b/Controls/AboutBox.cs @@ -801,8 +801,8 @@ namespace SystemTrayMenu.Controls // private class ListViewItemComparer : System.Collections.IComparer { - private int _intCol; - private bool _IsAscending = true; + private readonly int _intCol; + private readonly bool _IsAscending = true; public ListViewItemComparer() { diff --git a/Controls/AppNotifyIcon.cs b/Controls/AppNotifyIcon.cs index c001415..21e95de 100644 --- a/Controls/AppNotifyIcon.cs +++ b/Controls/AppNotifyIcon.cs @@ -17,12 +17,13 @@ namespace SystemTrayMenu public event EventHandler Restart; public event EventHandler Exit; - private NotifyIcon notifyIcon = new NotifyIcon(); + private readonly NotifyIcon notifyIcon = new NotifyIcon(); private DateTime timeLoadingStart; private int threadsLoading = 0; - private Timer load = new Timer(); - private int loadCount = 0, indexLoad = 0; - private List bitmapsLoading = new List() { R.L010, R.L020, R.L030, + private readonly Timer load = new Timer(); + private int loadCount = 0; + private readonly int indexLoad = 0; + private readonly List bitmapsLoading = new List() { R.L010, R.L020, R.L030, R.L040, R.L050, R.L060, R.L070, R.L080, R.L090, R.L100, R.L110, R.L120, R.L130, R.L140, R.L150, R.L160, R.L170, R.L180}; diff --git a/Controls/Menu.cs b/Controls/Menu.cs index c4fd9fa..967722d 100644 --- a/Controls/Menu.cs +++ b/Controls/Menu.cs @@ -42,7 +42,7 @@ namespace SystemTrayMenu } public int Level = 0; - private FadeForm FadeForm = null; + private readonly FadeForm FadeForm = null; private bool autoResizeRowsDone = false; public enum MenuType { Default, DisposedFake }; diff --git a/Controls/RowData.cs b/Controls/RowData.cs index 3aafd97..9bf8262 100644 --- a/Controls/RowData.cs +++ b/Controls/RowData.cs @@ -37,8 +37,8 @@ namespace SystemTrayMenu.Controls public string TargetFilePathOrig; public string Text; public int RowIndex; - private WaitMenuOpen waitMenuOpen = new WaitMenuOpen(); - private Icon icon = null; + private readonly WaitMenuOpen waitMenuOpen = new WaitMenuOpen(); + private readonly Icon icon = null; private bool isDisposed = false; public RowData() diff --git a/Handler/KeyboardInput.cs b/Handler/KeyboardInput.cs index 85d8636..9d313c4 100644 --- a/Handler/KeyboardInput.cs +++ b/Handler/KeyboardInput.cs @@ -14,9 +14,9 @@ namespace SystemTrayMenu.Handler public Action RowDeselected; public event EventHandler Cleared; - private Menu[] menus; - private KeyboardHook hook = new KeyboardHook(); - private Timer timerKeySearch = new Timer(); + private readonly Menu[] menus; + private readonly KeyboardHook hook = new KeyboardHook(); + private readonly Timer timerKeySearch = new Timer(); public int iRowKey = -1; public int iMenuKey = 0; private string KeySearchString = string.Empty; diff --git a/Handler/WaitFastLeave.cs b/Handler/WaitFastLeave.cs index 2c2a27d..1bb7d05 100644 --- a/Handler/WaitFastLeave.cs +++ b/Handler/WaitFastLeave.cs @@ -7,7 +7,7 @@ namespace SystemTrayMenu.Handler { public event EventHandler Leave; - private Timer timerSecondLeaveCheck = new Timer(); + private readonly Timer timerSecondLeaveCheck = new Timer(); public WaitFastLeave() { diff --git a/Handler/WaitMenuOpen.cs b/Handler/WaitMenuOpen.cs index 75f1ed5..4eb36ad 100644 --- a/Handler/WaitMenuOpen.cs +++ b/Handler/WaitMenuOpen.cs @@ -7,7 +7,7 @@ namespace SystemTrayMenu.Handler { public event EventHandler DoOpen; - private Timer waitOpen = new Timer(); + private readonly Timer waitOpen = new Timer(); private bool waitedDone = false; private bool clicked = false; private bool menuLoaded = false; diff --git a/Helper/FadeForm.cs b/Helper/FadeForm.cs index 26ee74f..1fd4522 100644 --- a/Helper/FadeForm.cs +++ b/Helper/FadeForm.cs @@ -22,10 +22,10 @@ namespace SystemTrayMenu } } - private Timer timerFadeIn = new Timer(); - private Timer timerFadeOut = new Timer(); - private Timer timerFadeHalf = new Timer(); - private Form form = null; + private readonly Timer timerFadeIn = new Timer(); + private readonly Timer timerFadeOut = new Timer(); + private readonly Timer timerFadeHalf = new Timer(); + private readonly Form form = null; private bool stopFadeInByHalf = false; public FadeForm(Form form) diff --git a/Helper/File/FileIni.cs b/Helper/File/FileIni.cs index bc89a3d..632cf62 100644 --- a/Helper/File/FileIni.cs +++ b/Helper/File/FileIni.cs @@ -7,11 +7,11 @@ namespace SystemTrayMenu.Helper { public class FileIni { - private Dictionary values; + private readonly Dictionary values; public FileIni(string path) { values = File.ReadLines(path) - .Where(line => (!String.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))) + .Where(line => (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))) .Select(line => line.Split(new char[] { '=' }, 2, 0)) .ToDictionary(parts => parts[0].Trim(), parts => parts.Length > 1 ? parts[1].Trim() : null); } diff --git a/Helper/File/IconReader.cs b/Helper/File/IconReader.cs index e5d1155..2bebda5 100644 --- a/Helper/File/IconReader.cs +++ b/Helper/File/IconReader.cs @@ -19,7 +19,7 @@ namespace SystemTrayMenu.Helper /// public class IconReader { - private static ConcurrentDictionary dictIconCache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary dictIconCache = new ConcurrentDictionary(); /// /// Options to specify the size of icons to return. diff --git a/Helper/KeyboardHook.cs b/Helper/KeyboardHook.cs index 5aa21dc..6636f68 100644 --- a/Helper/KeyboardHook.cs +++ b/Helper/KeyboardHook.cs @@ -18,7 +18,7 @@ namespace SystemTrayMenu.Helper /// private class Window : NativeWindow, IDisposable { - private static int WM_HOTKEY = 0x0312; + private static readonly int WM_HOTKEY = 0x0312; public Window() { @@ -61,7 +61,7 @@ namespace SystemTrayMenu.Helper #endregion } - private Window _window = new Window(); + private readonly Window _window = new Window(); private int _currentId; public KeyboardHook() @@ -120,8 +120,8 @@ namespace SystemTrayMenu.Helper /// public class KeyPressedEventArgs : EventArgs { - private KeyboardHookModifierKeys _modifier; - private Keys _key; + private readonly KeyboardHookModifierKeys _modifier; + private readonly Keys _key; internal KeyPressedEventArgs(KeyboardHookModifierKeys modifier, Keys key) { diff --git a/Helper/Log.cs b/Helper/Log.cs index 4f28d8c..d92f353 100644 --- a/Helper/Log.cs +++ b/Helper/Log.cs @@ -8,7 +8,7 @@ namespace SystemTrayMenu.Helper { internal static class Log { - private static Logger log = new Logger(""); + private static readonly Logger log = new Logger(""); internal static void Initialize() { Logger.Start(new FileInfo(GetLogFilePath())); diff --git a/Helper/ShellContextMenu.cs b/Helper/ShellContextMenu.cs index fe14d61..fb041b4 100644 --- a/Helper/ShellContextMenu.cs +++ b/Helper/ShellContextMenu.cs @@ -630,8 +630,8 @@ namespace SystemTrayMenu.Helper private const int S_OK = 0; private const int S_FALSE = 1; - private static int cbMenuItemInfo = Marshal.SizeOf(typeof(MENUITEMINFO)); - private static int cbInvokeCommand = Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX)); + private static readonly int cbMenuItemInfo = Marshal.SizeOf(typeof(MENUITEMINFO)); + private static readonly int cbInvokeCommand = Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX)); #endregion @@ -639,11 +639,11 @@ namespace SystemTrayMenu.Helper // Retrieves the IShellFolder interface for the desktop folder, which is the root of the Shell's namespace. [DllImport("shell32.dll")] - private static extern Int32 SHGetDesktopFolder(out IntPtr ppshf); + private static extern int SHGetDesktopFolder(out IntPtr ppshf); // Takes a STRRET structure returned by IShellFolder::GetDisplayNameOf, converts it to a string, and places the result in a buffer. [DllImport("shlwapi.dll", EntryPoint = "StrRetToBuf", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] - private static extern Int32 StrRetToBuf(IntPtr pstr, IntPtr pidl, StringBuilder pszBuf, int cchBuf); + private static extern int StrRetToBuf(IntPtr pstr, IntPtr pidl, StringBuilder pszBuf, int cchBuf); // The TrackPopupMenuEx function displays a shortcut menu at the specified location and tracks the selection of items on the shortcut menu. The shortcut menu can appear anywhere on the screen. [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] @@ -1227,7 +1227,7 @@ namespace SystemTrayMenu.Helper // Translates a file object's or folder's display name into an item identifier list. // Return value: error code, if any [PreserveSig] - Int32 ParseDisplayName( + int ParseDisplayName( IntPtr hwnd, IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] @@ -1240,7 +1240,7 @@ namespace SystemTrayMenu.Helper // identifier enumeration object and returning its IEnumIDList interface. // Return value: error code, if any [PreserveSig] - Int32 EnumObjects( + int EnumObjects( IntPtr hwnd, SHCONTF grfFlags, out IntPtr enumIDList); @@ -1248,7 +1248,7 @@ namespace SystemTrayMenu.Helper // Retrieves an IShellFolder object for a subfolder. // Return value: error code, if any [PreserveSig] - Int32 BindToObject( + int BindToObject( IntPtr pidl, IntPtr pbc, ref Guid riid, @@ -1257,7 +1257,7 @@ namespace SystemTrayMenu.Helper // Requests a pointer to an object's storage interface. // Return value: error code, if any [PreserveSig] - Int32 BindToStorage( + int BindToStorage( IntPtr pidl, IntPtr pbc, ref Guid riid, @@ -1274,7 +1274,7 @@ namespace SystemTrayMenu.Helper // follow the second (pidl1 > pidl2). Zero A return value of zero // indicates that the two items are the same (pidl1 = pidl2). [PreserveSig] - Int32 CompareIDs( + int CompareIDs( IntPtr lParam, IntPtr pidl1, IntPtr pidl2); @@ -1283,7 +1283,7 @@ namespace SystemTrayMenu.Helper // with a folder object. // Return value: error code, if any [PreserveSig] - Int32 CreateViewObject( + int CreateViewObject( IntPtr hwndOwner, Guid riid, out IntPtr ppv); @@ -1291,7 +1291,7 @@ namespace SystemTrayMenu.Helper // Retrieves the attributes of one or more file objects or subfolders. // Return value: error code, if any [PreserveSig] - Int32 GetAttributesOf( + int GetAttributesOf( uint cidl, [MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, @@ -1301,7 +1301,7 @@ namespace SystemTrayMenu.Helper // specified file objects or folders. // Return value: error code, if any [PreserveSig] - Int32 GetUIObjectOf( + int GetUIObjectOf( IntPtr hwndOwner, uint cidl, [MarshalAs(UnmanagedType.LPArray)] @@ -1313,7 +1313,7 @@ namespace SystemTrayMenu.Helper // Retrieves the display name for the specified file object or subfolder. // Return value: error code, if any [PreserveSig()] - Int32 GetDisplayNameOf( + int GetDisplayNameOf( IntPtr pidl, SHGNO uFlags, IntPtr lpName); @@ -1322,7 +1322,7 @@ namespace SystemTrayMenu.Helper // identifier in the process. // Return value: error code, if any [PreserveSig] - Int32 SetNameOf( + int SetNameOf( IntPtr hwnd, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] @@ -1340,7 +1340,7 @@ namespace SystemTrayMenu.Helper { // Adds commands to a shortcut menu [PreserveSig()] - Int32 QueryContextMenu( + int QueryContextMenu( IntPtr hmenu, uint iMenu, uint idCmdFirst, @@ -1349,14 +1349,14 @@ namespace SystemTrayMenu.Helper // Carries out the command associated with a shortcut menu item [PreserveSig()] - Int32 InvokeCommand( + int InvokeCommand( ref CMINVOKECOMMANDINFOEX info); // Retrieves information about a shortcut menu command, // including the help string and the language-independent, // or canonical, name for the command [PreserveSig()] - Int32 GetCommandString( + int GetCommandString( uint idcmd, GCS uflags, uint reserved, @@ -1371,7 +1371,7 @@ namespace SystemTrayMenu.Helper { // Adds commands to a shortcut menu [PreserveSig()] - Int32 QueryContextMenu( + int QueryContextMenu( IntPtr hmenu, uint iMenu, uint idCmdFirst, @@ -1380,14 +1380,14 @@ namespace SystemTrayMenu.Helper // Carries out the command associated with a shortcut menu item [PreserveSig()] - Int32 InvokeCommand( + int InvokeCommand( ref CMINVOKECOMMANDINFOEX info); // Retrieves information about a shortcut menu command, // including the help string and the language-independent, // or canonical, name for the command [PreserveSig()] - Int32 GetCommandString( + int GetCommandString( uint idcmd, GCS uflags, uint reserved, @@ -1398,7 +1398,7 @@ namespace SystemTrayMenu.Helper // Allows client objects of the IContextMenu interface to // handle messages associated with owner-drawn menu items [PreserveSig] - Int32 HandleMenuMsg( + int HandleMenuMsg( uint uMsg, IntPtr wParam, IntPtr lParam); @@ -1410,7 +1410,7 @@ namespace SystemTrayMenu.Helper { // Adds commands to a shortcut menu [PreserveSig()] - Int32 QueryContextMenu( + int QueryContextMenu( IntPtr hmenu, uint iMenu, uint idCmdFirst, @@ -1419,14 +1419,14 @@ namespace SystemTrayMenu.Helper // Carries out the command associated with a shortcut menu item [PreserveSig()] - Int32 InvokeCommand( + int InvokeCommand( ref CMINVOKECOMMANDINFOEX info); // Retrieves information about a shortcut menu command, // including the help string and the language-independent, // or canonical, name for the command [PreserveSig()] - Int32 GetCommandString( + int GetCommandString( uint idcmd, GCS uflags, uint reserved, @@ -1437,7 +1437,7 @@ namespace SystemTrayMenu.Helper // Allows client objects of the IContextMenu interface to // handle messages associated with owner-drawn menu items [PreserveSig] - Int32 HandleMenuMsg( + int HandleMenuMsg( uint uMsg, IntPtr wParam, IntPtr lParam); @@ -1445,7 +1445,7 @@ namespace SystemTrayMenu.Helper // Allows client objects of the IContextMenu3 interface to // handle messages associated with owner-drawn menu items [PreserveSig] - Int32 HandleMenuMsg2( + int HandleMenuMsg2( uint uMsg, IntPtr wParam, IntPtr lParam, diff --git a/Helper/WindowsExplorerSort.cs b/Helper/WindowsExplorerSort.cs index fc2248a..bcbf113 100644 --- a/Helper/WindowsExplorerSort.cs +++ b/Helper/WindowsExplorerSort.cs @@ -7,7 +7,7 @@ namespace SystemTrayMenu.Helper internal class WindowsExplorerSort : IComparer { [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - private static extern int StrCmpLogicalW(String x, String y); + private static extern int StrCmpLogicalW(string x, string y); public int Compare(string x, string y) { diff --git a/SystemTrayMenu.cs b/SystemTrayMenu.cs index 1398710..73d89c4 100644 --- a/SystemTrayMenu.cs +++ b/SystemTrayMenu.cs @@ -18,13 +18,13 @@ namespace SystemTrayMenu private enum OpenCloseState { Default, Opening, Closing }; private OpenCloseState openCloseState = OpenCloseState.Default; - private MessageFilter messageFilter = new MessageFilter(); - private MenuNotifyIcon menuNotifyIcon = null; - private Menu[] menus = new Menu[MenuDefines.MenusMax]; - private BackgroundWorker worker = new BackgroundWorker(); - private Screen screen = Screen.PrimaryScreen; - private WaitFastLeave fastLeave = new WaitFastLeave(); - private KeyboardInput keyboardInput; + private readonly MessageFilter messageFilter = new MessageFilter(); + private readonly MenuNotifyIcon menuNotifyIcon = null; + private readonly Menu[] menus = new Menu[MenuDefines.MenusMax]; + private readonly BackgroundWorker worker = new BackgroundWorker(); + private readonly Screen screen = Screen.PrimaryScreen; + private readonly WaitFastLeave fastLeave = new WaitFastLeave(); + private readonly KeyboardInput keyboardInput; private DataGridView dgvFromLastMouseEvent = null; private DataGridViewCellEventArgs cellEventArgsFromLastMouseEvent = null; private int clicksInQueue = 0;