[BUG] TaskbarForm is activated even though it shouldn't be activated (#391), version 1.2.9.11

This commit is contained in:
Markus Hofknecht 2022-06-04 18:37:57 +02:00
parent 10b4c820ea
commit ac70e4369a
5 changed files with 45 additions and 24 deletions

View file

@ -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();
}
/// <summary>
/// This ensures that next click on taskbaritem works as activate event/click event.
/// </summary>
private void SetStateNormal()
{
if (Form.ActiveForm == taskbarForm)
{
taskbarForm.WindowState = FormWindowState.Normal;
}
}
}
}

View file

@ -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;

View file

@ -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")]

View file

@ -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);

View file

@ -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);
}
/// <summary>