From ac70e4369a7432bed314676592781bc7e4fef0f9 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Sat, 4 Jun 2022 18:37:57 +0200 Subject: [PATCH] [BUG] TaskbarForm is activated even though it shouldn't be activated (#391), version 1.2.9.11 --- Business/App.cs | 48 +++++++++++++++------------ Business/Menus.cs | 14 ++++++++ Properties/AssemblyInfo.cs | 4 +-- UserInterface/TaskbarForm.Designer.cs | 1 + UserInterface/TaskbarForm.cs | 2 ++ 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/Business/App.cs b/Business/App.cs index 5fb2f46..7c5e642 100644 --- a/Business/App.cs +++ b/Business/App.cs @@ -18,6 +18,7 @@ namespace SystemTrayMenu private readonly AppNotifyIcon menuNotifyIcon = new(); private readonly Menus menus = new(); private readonly TaskbarForm taskbarForm = null; + private readonly Timer timerActivateMenuAfterTaskbarFormMinimizedFinished = new(); public App() { @@ -28,29 +29,43 @@ namespace SystemTrayMenu menuNotifyIcon.Click += () => menus.SwitchOpenClose(true); menuNotifyIcon.OpenLog += Log.OpenLogFile; menus.MainPreload(); + timerActivateMenuAfterTaskbarFormMinimizedFinished.Interval = 20; + timerActivateMenuAfterTaskbarFormMinimizedFinished.Tick += Timer_Tick; + + void Timer_Tick(object sender, EventArgs e) + { + timerActivateMenuAfterTaskbarFormMinimizedFinished.Stop(); + if (menus.IsNotNullAndIsUsableAndNotClosing()) + { + taskbarForm.Activate(); + menus.ReActivateIfVisible(); + } + } if (Properties.Settings.Default.ShowInTaskbar) { taskbarForm = new TaskbarForm(); taskbarForm.FormClosed += (s, e) => Application.Exit(); - taskbarForm.Deactivate += (s, e) => SetStateNormal(); - taskbarForm.Resize += (s, e) => SetStateNormal(); - taskbarForm.Activated += TasbkarItemActivated; - void TasbkarItemActivated(object sender, EventArgs e) + int eachSecondResizeEvent = 1; + taskbarForm.Resize += (s, e) => { - SetStateNormal(); - taskbarForm.Activate(); - taskbarForm.Focus(); - menus.SwitchOpenCloseByTaskbarItem(); - } - } + if (eachSecondResizeEvent++ % 2 == 0 && Form.ActiveForm != null) + { + menus.SwitchOpenCloseByTaskbarItem(); + timerActivateMenuAfterTaskbarFormMinimizedFinished.Start(); + } - DllImports.NativeMethods.User32ShowInactiveTopmost(taskbarForm); + taskbarForm.WindowState = FormWindowState.Minimized; + }; + + taskbarForm.Show(); + } } public void Dispose() { taskbarForm?.Dispose(); + timerActivateMenuAfterTaskbarFormMinimizedFinished.Dispose(); SystemEvents.DisplaySettingsChanged -= (s, e) => SystemEvents_DisplaySettingsChanged(); menus.Dispose(); menuNotifyIcon.Dispose(); @@ -60,16 +75,5 @@ namespace SystemTrayMenu { menus.ReAdjustSizeAndLocation(); } - - /// - /// This ensures that next click on taskbaritem works as activate event/click event. - /// - private void SetStateNormal() - { - if (Form.ActiveForm == taskbarForm) - { - taskbarForm.WindowState = FormWindowState.Normal; - } - } } } \ No newline at end of file diff --git a/Business/Menus.cs b/Business/Menus.cs index a40ddad..bf9990b 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -14,6 +14,7 @@ namespace SystemTrayMenu.Business using System.Linq; using System.Windows.Forms; using SystemTrayMenu.DataClasses; + using SystemTrayMenu.DllImports; using SystemTrayMenu.Handler; using SystemTrayMenu.Helper; using SystemTrayMenu.UserInterface; @@ -562,6 +563,19 @@ namespace SystemTrayMenu.Business timerStillActiveCheck.Start(); } + internal bool IsNotNullAndIsUsableAndNotClosing() + { + return menus[0] != null && menus[0].IsUsable && Form.ActiveForm == menus[0]; + } + + internal void ReActivateIfVisible() + { + menus[0].Activate(); + NativeMethods.User32ShowInactiveTopmost(menus[0]); + NativeMethods.ForceForegroundWindow(menus[0].Handle); + timerStillActiveCheck.Start(); + } + internal bool IsOpenCloseStateOpening() { return openCloseState == OpenCloseState.Opening; diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 1e1f9b6..21c5939 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -39,5 +39,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.9.10")] -[assembly: AssemblyFileVersion("1.2.9.10")] +[assembly: AssemblyVersion("1.2.9.11")] +[assembly: AssemblyFileVersion("1.2.9.11")] diff --git a/UserInterface/TaskbarForm.Designer.cs b/UserInterface/TaskbarForm.Designer.cs index ba5e5fd..a3c67d4 100644 --- a/UserInterface/TaskbarForm.Designer.cs +++ b/UserInterface/TaskbarForm.Designer.cs @@ -45,6 +45,7 @@ this.Name = "TaskbarForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "SystemTrayMenu"; + this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.LocationChanged += new System.EventHandler(this.TaskbarForm_LocationChanged); this.ResumeLayout(false); diff --git a/UserInterface/TaskbarForm.cs b/UserInterface/TaskbarForm.cs index 4b4bb00..8cc8e5e 100644 --- a/UserInterface/TaskbarForm.cs +++ b/UserInterface/TaskbarForm.cs @@ -22,7 +22,9 @@ namespace SystemTrayMenu.UserInterface private void TaskbarForm_LocationChanged(object sender, System.EventArgs e) { + this.LocationChanged -= new System.EventHandler(this.TaskbarForm_LocationChanged); SetLocation(); + this.LocationChanged += new System.EventHandler(this.TaskbarForm_LocationChanged); } ///