private bool SysTrayCallback(uint message, NOTIFYICONDATA nicData) { TrayIcon trayIcon = new TrayIcon(nicData.hWnd); if (nicData.uID != null) { trayIcon.UID = nicData.uID; } lock (_lockObject) { switch ((NIM)message) { case NIM.NIM_ADD: // Ensure the icon doesn't already exist. if (TrayIcons.Contains(trayIcon)) { return(false); } // Add the icon. trayIcon.Title = nicData.szTip; try { trayIcon.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } catch { trayIcon.Icon = null; } trayIcon.HWnd = nicData.hWnd; trayIcon.UID = nicData.uID; trayIcon.CallbackMessage = nicData.uCallbackMessage; TrayIcons.Add(trayIcon); break; case NIM.NIM_DELETE: try { if (!TrayIcons.Contains(trayIcon)) { // Nothing to remove. return(false); } // Woo! Using Linq to avoid iterating! TrayIcons.Remove(trayIcon); } catch (Exception ex) { Trace.WriteLine("Unable to remove the icon from the collection. Error: " + ex.ToString()); } break; case NIM.NIM_MODIFY: try { bool exists = false; if (TrayIcons.Contains(trayIcon)) { exists = true; trayIcon = TrayIcons.Single(i => i.HWnd == nicData.hWnd && i.UID == nicData.uID); } trayIcon.Title = nicData.szTip; trayIcon.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); trayIcon.HWnd = nicData.hWnd; trayIcon.UID = nicData.uID; trayIcon.CallbackMessage = nicData.uCallbackMessage; if (!exists) { TrayIcons.Add(trayIcon); } } catch (Exception ex) { Trace.WriteLine("Unable to modify the icon in the collection. Error: " + ex.ToString()); } break; } } return(true); }
private bool SysTrayCallback(uint message, NOTIFYICONDATA nicData) { Console.WriteLine(message + " - " + nicData.uID); lock (_lockObject) { TrayIcon trayIcon; switch ((NIM)message) { case NIM.NIM_ADD: trayIcon = new TrayIcon((IntPtr)nicData.hWnd); trayIcon.UID = (uint)nicData.uID; // Ensure the icon doesn't already exist. if (TrayIcons.Contains(trayIcon)) { return(false); } // Add the icon. trayIcon.Title = nicData.szTip; try { trayIcon.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon((IntPtr)nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } catch { trayIcon.Icon = null; } trayIcon.HWnd = (IntPtr)nicData.hWnd; trayIcon.UID = (uint)nicData.uID; trayIcon.CallbackMessage = (uint)nicData.uCallbackMessage; TrayIcon old = TrayIcons.FirstOrDefault(i => i.HWnd == (IntPtr)nicData.hWnd); if (old != null) { TrayIcons.Remove(old); } TrayIcons.Add(trayIcon); break; case NIM.NIM_DELETE: try { TrayIcon oldTrayIcon = TrayIcons.FirstOrDefault(i => i.HWnd == (IntPtr)nicData.hWnd); if (oldTrayIcon != null) { TrayIcons.Remove(oldTrayIcon); } else { return(false); } } catch (Exception ex) { Trace.WriteLine("Unable to remove the icon from the collection. Error: " + ex.ToString()); } break; case NIM.NIM_MODIFY: try { trayIcon = TrayIcons.Single(i => i.HWnd == (IntPtr)nicData.hWnd); if (trayIcon == null) { break; } trayIcon.Title = nicData.szTip; trayIcon.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon((IntPtr)nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); trayIcon.HWnd = (IntPtr)nicData.hWnd; trayIcon.UID = (uint)nicData.uID; trayIcon.CallbackMessage = (uint)nicData.uCallbackMessage; TrayIcons.Remove(trayIcon); TrayIcons.Add(trayIcon); } catch (Exception ex) { Trace.WriteLine("Unable to modify the icon in the collection. Error: " + ex.ToString()); } break; } } return(true); }