public TrayIcon(NOTIFYITEMICON s) { InitializeComponent(); thisIcon = s; Icon.Source = s.image; Icon.Height = 16; }
private bool SysTrayCallback(uint message, NOTIFYICONDATA nicData) { var trayIcon = new NOTIFYITEMICON(); lock (_lockObject) { switch ((NIM)message) { case NIM.NIM_ADD: // Ensure the icon doesn't already exist. if (destructors.ContainsKey(nicData.uID)) { return(false); // TODO test it } // Add the icon. trayIcon.Title = nicData.szTip; trayIcon.hIcon = nicData.hIcon; trayIcon.original = nicData; try { trayIcon.image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(nicData.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch { trayIcon.image = null; } trayIcon.hWnd = nicData.hWnd; trayIcon.UID = nicData.uID; destructors.Add(nicData.uID, addNewNotification(trayIcon)); break; case NIM.NIM_DELETE: try { if (!destructors.ContainsKey(nicData.uID)) { // Nothing to remove. return(false); } else { destructors[nicData.uID].Destroy(); destructors.Remove(nicData.uID); } } catch (Exception) { } break; case NIM.NIM_MODIFY: try { if (!destructors.ContainsKey(nicData.uID)) { return(false); } else { destructors[nicData.uID].Destroy(); destructors.Remove(nicData.uID); trayIcon.Title = nicData.szTip; trayIcon.hIcon = nicData.hIcon; trayIcon.original = nicData; try { trayIcon.image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(nicData.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch { trayIcon.image = null; } trayIcon.hWnd = nicData.hWnd; trayIcon.UID = nicData.uID; destructors.Add(nicData.uID, addNewNotification(trayIcon)); } } catch (Exception ex) { Console.WriteLine("Unable to modify the icon in the collection. Error: " + ex.ToString()); } break; } } return(true); }