Skip to content

Commit 3e83050

Browse files
committed
Re-create tray icon on taskbar restart #3396
1 parent d99ec35 commit 3e83050

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

app/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public static TimeSpan GetIdleTime()
2626
private const int SC_MONITORPOWER = 0xF170;
2727
private const int MONITOR_OFF = 2;
2828

29+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
30+
public static extern int RegisterWindowMessage(string lpString);
31+
32+
public static readonly int WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");
33+
34+
2935
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
3036
private static extern IntPtr SendMessage(nint hWnd, int hMsg, int wParam, int lParam);
3137

app/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public static void Main(string[] args)
103103
Visible = true
104104
};
105105

106+
Logger.WriteLine($"Tray Icon: {trayIcon.Visible}");
107+
106108
settingsForm.SetContextMenu();
107109
trayIcon.MouseClick += TrayIcon_MouseClick;
108110
trayIcon.MouseMove += TrayIcon_MouseMove;
@@ -357,7 +359,12 @@ static void TrayIcon_MouseMove(object? sender, MouseEventArgs e)
357359

358360
static void OnExit(object sender, EventArgs e)
359361
{
360-
trayIcon.Visible = false;
362+
if (trayIcon is not null)
363+
{
364+
trayIcon.Visible = false;
365+
trayIcon.Dispose();
366+
}
367+
361368
PeripheralsProvider.UnregisterForDeviceEvents();
362369
clamshellControl.UnregisterDisplayEvents();
363370
NativeMethods.UnregisterPowerSettingNotification(unRegPowerNotify);

app/Settings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ protected override void WndProc(ref Message m)
723723
m.Result = (IntPtr)1;
724724
}
725725

726+
if (m.Msg == NativeMethods.WM_TASKBARCREATED)
727+
{
728+
Logger.WriteLine("Taskbar created, re-creating tray icon");
729+
Program.trayIcon.Visible = true;
730+
}
731+
726732
try
727733
{
728734
base.WndProc(ref m);

0 commit comments

Comments
 (0)