diff --git a/GUI/NotifyIconAdv.cs b/GUI/NotifyIconAdv.cs index 15a9a8c..b9de2a2 100644 --- a/GUI/NotifyIconAdv.cs +++ b/GUI/NotifyIconAdv.cs @@ -460,8 +460,7 @@ namespace OpenHardwareMonitor.GUI { data.Info = tipText; data.InfoFlags = (int)tipIcon; - NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Modify, data); + NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data); } } @@ -524,26 +523,28 @@ namespace OpenHardwareMonitor.GUI { if (showNotifyIcon && icon != null) { if (!created) { - int i = 0; - do { - created = NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Add, data); - if (!created) { - System.Threading.Thread.Sleep(200); - i++; - } - } while (!created && i < 40); - } else { - NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Modify, data); + // try to modify the icon in case it still exists (after WM_TASKBARCREATED) + if (NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data)) { + created = true; + } else { // modification failed, try to add a new icon + int i = 0; + do { + created = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Add, data); + if (!created) { + System.Threading.Thread.Sleep(200); + i++; + } + } while (!created && i < 40); + } + } else { // the icon is created already, just modify it + NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Modify, data); } } else { if (created) { int i = 0; - bool deleted = false; + bool deleted; do { - deleted = NativeMethods.Shell_NotifyIcon( - NativeMethods.NotifyIconMessage.Delete, data); + deleted = NativeMethods.Shell_NotifyIcon(NotifyIconMessage.Delete, data); if (!deleted) { System.Threading.Thread.Sleep(200); i++; @@ -667,7 +668,7 @@ namespace OpenHardwareMonitor.GUI { } } - if (message.Msg == NotifyIconWindowsImplementation.WM_TASKBARCREATED) { + if (message.Msg == WM_TASKBARCREATED) { lock (syncObj) { created = false; } @@ -737,6 +738,12 @@ namespace OpenHardwareMonitor.GUI { private static int WM_TASKBARCREATED = NativeMethods.RegisterWindowMessage("TaskbarCreated"); + private enum NotifyIconMessage : int { + Add = 0x0, + Modify = 0x1, + Delete = 0x2 + } + private static class NativeMethods { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr PostMessage(HandleRef hwnd, int msg, @@ -774,12 +781,6 @@ namespace OpenHardwareMonitor.GUI { public int InfoFlags; } - public enum NotifyIconMessage : int { - Add = 0x0, - Modify = 0x1, - Delete = 0x2 - } - [DllImport("shell32.dll", CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool Shell_NotifyIcon(NotifyIconMessage message,