From dff4b3db79a9a3c1696a28df5bb68e3cd9d50037 Mon Sep 17 00:00:00 2001 From: IceDBorn Date: Fri, 26 Feb 2021 21:20:36 +0200 Subject: [PATCH 1/4] Add option to hide the tray icon --- CenterTaskbar/Program.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index 3e6f728..710ad1f 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -107,7 +107,8 @@ public TrayApplication(IReadOnlyList args) header, new MenuItem("Scan for screens", Restart), startup, - new MenuItem("E&xit", Exit) + new MenuItem("Hide", Hide), + new MenuItem("E&xit", Exit), }), Visible = true }; @@ -210,6 +211,11 @@ private async void Restart(object sender, EventArgs e) } + private void Hide(object sender, EventArgs eventArgs) + { + _trayIcon.Visible = false; + } + private void ResetAll() { CancelPositionThread(); From 47a163f56c96739d535d8534c554d07d5d90849f Mon Sep 17 00:00:00 2001 From: IceDBorn Date: Fri, 26 Feb 2021 22:21:35 +0200 Subject: [PATCH 2/4] Remember hide and disable with "-visible" argument --- CenterTaskbar/Program.cs | 63 +++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index 710ad1f..f464a23 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -43,6 +43,7 @@ public class TrayApplication : ApplicationContext { private const string AppName = "CenterTaskbar"; private const string RunRegkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; + private const string HideRegkey = "SOFTWARE\\CenterTaskbar"; private const int OneSecond = 1000; private const int SWP_NOSIZE = 0x0001; private const int SWP_NOZORDER = 0x0004; @@ -80,8 +81,15 @@ public TrayApplication(IReadOnlyList args) if (args.Count > 0) try { - _activeFramerate = int.Parse(args[0]); - Debug.WriteLine("Active refresh rate: " + _activeFramerate); + if (args[0].Equals("-visible")) + { + RemoveHideFromRegistry(); + } + else + { + _activeFramerate = int.Parse(args[0]); + Debug.WriteLine("Active refresh rate: " + _activeFramerate); + } } catch (FormatException e) { @@ -99,19 +107,22 @@ public TrayApplication(IReadOnlyList args) }; // Setup Tray Icon - _trayIcon = new NotifyIcon + if (!IsHideInRegistry()) { - Icon = Resources.TrayIcon, - ContextMenu = new ContextMenu(new[] + _trayIcon = new NotifyIcon { - header, - new MenuItem("Scan for screens", Restart), - startup, - new MenuItem("Hide", Hide), - new MenuItem("E&xit", Exit), - }), - Visible = true - }; + Icon = Resources.TrayIcon, + ContextMenu = new ContextMenu(new[] + { + header, + new MenuItem("Scan for screens", Restart), + startup, + new MenuItem("Hide", Hide), + new MenuItem("E&xit", Exit), + }), + Visible = true + }; + } Start(); SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanged; @@ -143,6 +154,15 @@ public bool IsApplicationInStartup() } } + public bool IsHideInRegistry() + { + using (var key = Registry.CurrentUser.OpenSubKey(HideRegkey, true)) + { + var value = key?.GetValue("Hide"); + return value is string startValue && startValue.StartsWith("True"); + } + } + public void AddApplicationToStartup() { using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) @@ -151,6 +171,14 @@ public void AddApplicationToStartup() } } + public void AddHideToRegistry() + { + using (var key = Registry.CurrentUser.CreateSubKey(HideRegkey, true)) + { + key?.SetValue("Hide", "True"); + } + } + public void RemoveApplicationFromStartup() { using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) @@ -158,6 +186,14 @@ public void RemoveApplicationFromStartup() key?.DeleteValue(AppName, false); } } + + public void RemoveHideFromRegistry() + { + using (var key = Registry.CurrentUser.OpenSubKey(HideRegkey, true)) + { + key?.SetValue("Hide", "False"); + } + } private void Exit(object sender, EventArgs e) { @@ -213,6 +249,7 @@ private async void Restart(object sender, EventArgs e) private void Hide(object sender, EventArgs eventArgs) { + AddHideToRegistry(); _trayIcon.Visible = false; } From 880ac49d2f43a5746455935c9389a368db5b257e Mon Sep 17 00:00:00 2001 From: IceDBorn Date: Sat, 27 Feb 2021 04:03:38 +0200 Subject: [PATCH 3/4] Remember hide only if "Start with Windows" is enabled and disable silent launch by running the app again --- CenterTaskbar/Program.cs | 47 +++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index f464a23..be73bda 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -43,7 +43,6 @@ public class TrayApplication : ApplicationContext { private const string AppName = "CenterTaskbar"; private const string RunRegkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - private const string HideRegkey = "SOFTWARE\\CenterTaskbar"; private const int OneSecond = 1000; private const int SWP_NOSIZE = 0x0001; private const int SWP_NOZORDER = 0x0004; @@ -55,6 +54,7 @@ public class TrayApplication : ApplicationContext private const string ShellSecondaryTrayWnd = "Shell_SecondaryTrayWnd"; private static readonly string ExecutablePath = "\"" + Application.ExecutablePath + "\""; + private static readonly string SilentExecutablePath = "\"" + Application.ExecutablePath + "\"" + " -silent"; private static bool _disposed; private CancellationTokenSource _loopCancellationTokenSource = new CancellationTokenSource(); @@ -72,6 +72,8 @@ public class TrayApplication : ApplicationContext private readonly NotifyIcon _trayIcon; + private readonly bool _hidden = false; + // private Thread positionThread; private readonly Dictionary _positionThreads = new Dictionary(); @@ -81,9 +83,9 @@ public TrayApplication(IReadOnlyList args) if (args.Count > 0) try { - if (args[0].Equals("-visible")) + if (args[0].Equals("-silent")) { - RemoveHideFromRegistry(); + _hidden = true; } else { @@ -107,8 +109,12 @@ public TrayApplication(IReadOnlyList args) }; // Setup Tray Icon - if (!IsHideInRegistry()) + if (!_hidden) { + if (IsHiddenApplicationInStartup()) + { + AddApplicationToStartup(); + } _trayIcon = new NotifyIcon { Icon = Resources.TrayIcon, @@ -153,13 +159,13 @@ public bool IsApplicationInStartup() return value is string startValue && startValue.StartsWith(ExecutablePath); } } - - public bool IsHideInRegistry() + + public bool IsHiddenApplicationInStartup() { - using (var key = Registry.CurrentUser.OpenSubKey(HideRegkey, true)) + using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) { - var value = key?.GetValue("Hide"); - return value is string startValue && startValue.StartsWith("True"); + var value = key?.GetValue(AppName); + return value is string startValue && startValue.StartsWith(SilentExecutablePath); } } @@ -170,12 +176,12 @@ public void AddApplicationToStartup() key?.SetValue(AppName, ExecutablePath); } } - - public void AddHideToRegistry() + + public void AddHiddenApplicationToStartup() { - using (var key = Registry.CurrentUser.CreateSubKey(HideRegkey, true)) + using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) { - key?.SetValue("Hide", "True"); + key?.SetValue(AppName, SilentExecutablePath); } } @@ -186,14 +192,6 @@ public void RemoveApplicationFromStartup() key?.DeleteValue(AppName, false); } } - - public void RemoveHideFromRegistry() - { - using (var key = Registry.CurrentUser.OpenSubKey(HideRegkey, true)) - { - key?.SetValue("Hide", "False"); - } - } private void Exit(object sender, EventArgs e) { @@ -249,7 +247,12 @@ private async void Restart(object sender, EventArgs e) private void Hide(object sender, EventArgs eventArgs) { - AddHideToRegistry(); + if (IsApplicationInStartup()) + { + AddHiddenApplicationToStartup(); + MessageBox.Show("The tray icon is now hidden. To show the icon again, run CenterTaskbar once again.", + "CenterTaskbar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } _trayIcon.Visible = false; } From 854ed566643becd67643174e9ef03b0c08cb6529 Mon Sep 17 00:00:00 2001 From: IceDBorn Date: Sat, 27 Feb 2021 04:28:21 +0200 Subject: [PATCH 4/4] Rename functions and edit warning message --- CenterTaskbar/Program.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index be73bda..1ffba40 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -111,7 +111,7 @@ public TrayApplication(IReadOnlyList args) // Setup Tray Icon if (!_hidden) { - if (IsHiddenApplicationInStartup()) + if (IsSilentApplicationInStartup()) { AddApplicationToStartup(); } @@ -160,7 +160,7 @@ public bool IsApplicationInStartup() } } - public bool IsHiddenApplicationInStartup() + public bool IsSilentApplicationInStartup() { using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) { @@ -177,7 +177,7 @@ public void AddApplicationToStartup() } } - public void AddHiddenApplicationToStartup() + public void AddSilentApplicationToStartup() { using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true)) { @@ -249,8 +249,9 @@ private void Hide(object sender, EventArgs eventArgs) { if (IsApplicationInStartup()) { - AddHiddenApplicationToStartup(); - MessageBox.Show("The tray icon is now hidden. To show the icon again, run CenterTaskbar once again.", + AddSilentApplicationToStartup(); + MessageBox.Show("The tray icon is now hidden. To show the icon again, " + + "kill CenterTaskbar using Task Manager and run CenterTaskbar once again.", "CenterTaskbar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } _trayIcon.Visible = false;