示例#1
0
        private bool SysTrayCallback(uint message, NOTIFYICONDATA nicData)
        {
            if (nicData.hWnd == 0)
            {
                return(false);
            }

            NotifyIcon trayIcon = new NotifyIcon((IntPtr)nicData.hWnd);

            trayIcon.UID = nicData.uID;

            lock (_lockObject)
            {
                if ((NIM)message == NIM.NIM_ADD || (NIM)message == NIM.NIM_MODIFY)
                {
                    try
                    {
                        bool exists = false;

                        if (nicData.dwState != 1)
                        {
                            if (nicData.guidItem == new Guid(VOLUME_GUID))
                            {
                                return(false);
                            }

                            foreach (NotifyIcon ti in TrayIcons)
                            {
                                if ((nicData.guidItem != Guid.Empty && nicData.guidItem == ti.GUID) || (ti.HWnd == (IntPtr)nicData.hWnd && ti.UID == nicData.uID))
                                {
                                    exists   = true;
                                    trayIcon = ti;
                                    break;
                                }
                            }

                            if ((NIF.TIP & nicData.uFlags) != 0 && !string.IsNullOrEmpty(nicData.szTip))
                            {
                                trayIcon.Title = nicData.szTip;
                            }

                            if ((NIF.ICON & nicData.uFlags) != 0)
                            {
                                if ((IntPtr)nicData.hIcon != IntPtr.Zero)
                                {
                                    try
                                    {
                                        System.Windows.Media.Imaging.BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon((IntPtr)nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                                        if (bs != null)
                                        {
                                            trayIcon.Icon = bs;
                                        }
                                    }
                                    catch
                                    {
                                        if (trayIcon.Icon == null)
                                        {
                                            trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                                        }
                                    }
                                }
                                else
                                {
                                    trayIcon.Icon = null;
                                }
                            }
                            trayIcon.HWnd = (IntPtr)nicData.hWnd;
                            trayIcon.UID  = nicData.uID;
                            trayIcon.GUID = nicData.guidItem;

                            // guess version in case we are receiving icons that aren't sending NIM_SETVERSION to new explorers
                            if ((NIF.VISTA_MASK & nicData.uFlags) != 0)
                            {
                                trayIcon.Version = 4;
                            }
                            else if ((NIF.XP_MASK & nicData.uFlags) != 0)
                            {
                                trayIcon.Version = 3;
                            }

                            if (nicData.uVersion > 0 && nicData.uVersion <= 4)
                            {
                                trayIcon.Version = nicData.uVersion;
                            }

                            if ((NIF.MESSAGE & nicData.uFlags) != 0)
                            {
                                trayIcon.CallbackMessage = nicData.uCallbackMessage;
                            }

                            if (!exists)
                            {
                                // default placement to a menu bar like rect
                                trayIcon.Placement = defaultPlacement;

                                if (trayIcon.Icon == null)
                                {
                                    trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                                }

                                TrayIcons.Add(trayIcon);
                                CairoLogger.Instance.Debug("Added tray icon: " + trayIcon.Title);

                                if ((NIM)message == NIM.NIM_MODIFY)
                                {
                                    // return an error to the notifyicon as we received a modify for an icon we did not yet have
                                    return(false);
                                }
                            }
                            else
                            {
                                CairoLogger.Instance.Debug("Modified tray icon: " + trayIcon.Title);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        CairoLogger.Instance.Error("Unable to modify the icon in the collection.", ex);
                    }
                }
                else if ((NIM)message == NIM.NIM_DELETE)
                {
                    try
                    {
                        if (!TrayIcons.Contains(trayIcon))
                        {
                            // Nothing to remove.
                            return(false);
                        }

                        TrayIcons.Remove(trayIcon);

                        CairoLogger.Instance.Debug("Removed tray icon: " + nicData.szTip);
                    }
                    catch (Exception ex)
                    {
                        CairoLogger.Instance.Error("Unable to remove the icon from the collection.", ex);
                    }
                }
                else if ((NIM)message == NIM.NIM_SETVERSION)
                {
                    foreach (NotifyIcon ti in TrayIcons)
                    {
                        if ((nicData.guidItem != Guid.Empty && nicData.guidItem == ti.GUID) || (ti.HWnd == (IntPtr)nicData.hWnd && ti.UID == nicData.uID))
                        {
                            ti.Version = nicData.uVersion;
                            CairoLogger.Instance.Debug("Modified version to " + ti.Version + " on tray icon: " + ti.Title);
                            break;
                        }
                    }
                }
            }
            return(true);
        }
示例#2
0
        private bool SysTrayCallback(uint message, NOTIFYICONDATA nicData)
        {
            NotifyIcon trayIcon = new NotifyIcon((IntPtr)nicData.hWnd);

            trayIcon.UID = nicData.uID;

            lock (_lockObject)
            {
                if ((NIM)message == NIM.NIM_ADD || (NIM)message == NIM.NIM_MODIFY)
                {
                    try
                    {
                        bool exists = false;

                        if (nicData.dwState != 1)
                        {
                            if (nicData.guidItem == new Guid(VOLUME_GUID))
                            {
                                return(false);
                            }

                            foreach (NotifyIcon ti in TrayIcons)
                            {
                                if ((nicData.guidItem != Guid.Empty && nicData.guidItem == ti.GUID) || (ti.HWnd == (IntPtr)nicData.hWnd && ti.UID == nicData.uID))
                                {
                                    exists   = true;
                                    trayIcon = ti;
                                    break;
                                }
                            }

                            if (((uint)NIF.NIF_TIP & nicData.uFlags) != 0)
                            {
                                trayIcon.Title = nicData.szTip;
                            }

                            if (((uint)NIF.NIF_ICON & nicData.uFlags) != 0)
                            {
                                if ((IntPtr)nicData.hIcon != IntPtr.Zero)
                                {
                                    try
                                    {
                                        trayIcon.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon((IntPtr)nicData.hIcon, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                                    }
                                    catch
                                    {
                                        if (trayIcon.Icon == null)
                                        {
                                            trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                                        }
                                    }
                                }
                                else
                                {
                                    trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                                }
                            }
                            trayIcon.HWnd = (IntPtr)nicData.hWnd;
                            trayIcon.UID  = nicData.uID;
                            trayIcon.GUID = nicData.guidItem;

                            if (nicData.uVersion > 0 && nicData.uVersion <= 4)
                            {
                                trayIcon.Version = nicData.uVersion;
                            }

                            if (((uint)NIF.NIF_MESSAGE & nicData.uFlags) != 0)
                            {
                                trayIcon.CallbackMessage = nicData.uCallbackMessage;
                            }

                            if (!exists)
                            {
                                TrayIcons.Add(trayIcon);
                                Trace.WriteLine("Added tray icon: " + trayIcon.Title);
                            }
                            else
                            {
                                Trace.WriteLine("Modified tray icon: " + trayIcon.Title);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("Unable to modify the icon in the collection. Error: " + ex.ToString());
                    }
                }
                else if ((NIM)message == NIM.NIM_DELETE)
                {
                    try
                    {
                        if (!TrayIcons.Contains(trayIcon))
                        {
                            // Nothing to remove.
                            return(false);
                        }

                        // Woo! Using Linq to avoid iterating!
                        TrayIcons.Remove(trayIcon);

                        Trace.WriteLine("Removed tray icon: " + nicData.szTip);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("Unable to remove the icon from the collection. Error: " + ex.ToString());
                    }
                }
                else if ((NIM)message == NIM.NIM_SETVERSION)
                {
                    foreach (NotifyIcon ti in TrayIcons)
                    {
                        if ((nicData.guidItem != Guid.Empty && nicData.guidItem == ti.GUID) || (ti.HWnd == (IntPtr)nicData.hWnd && ti.UID == nicData.uID))
                        {
                            ti.Version = nicData.uVersion;
                            break;
                        }
                    }
                }
            }
            return(true);
        }
示例#3
0
        private bool SysTrayCallback(uint message, SafeNotifyIconData nicData)
        {
            if (nicData.hWnd == IntPtr.Zero)
            {
                return(false);
            }

            NotifyIcon trayIcon = new NotifyIcon(nicData.hWnd);

            trayIcon.UID = nicData.uID;

            lock (_lockObject)
            {
                if ((NIM)message == NIM.NIM_ADD || (NIM)message == NIM.NIM_MODIFY)
                {
                    try
                    {
                        bool exists = false;

                        // hide icons while we are shell which require UWP support & we have a separate implementation for
                        if (nicData.guidItem == new Guid(VOLUME_GUID) && ((Shell.IsCairoRunningAsShell && Shell.IsWindows10OrBetter) || GroupPolicyManager.Instance.HideScaVolume))
                        {
                            return(false);
                        }

                        foreach (NotifyIcon ti in TrayIcons)
                        {
                            if (ti.Equals(nicData))
                            {
                                exists   = true;
                                trayIcon = ti;
                                break;
                            }
                        }

                        if ((NIF.STATE & nicData.uFlags) != 0)
                        {
                            trayIcon.IsHidden = nicData.dwState == 1;
                        }

                        if ((NIF.TIP & nicData.uFlags) != 0 && !string.IsNullOrEmpty(nicData.szTip))
                        {
                            trayIcon.Title = nicData.szTip;
                        }

                        if ((NIF.ICON & nicData.uFlags) != 0)
                        {
                            if (nicData.hIcon != IntPtr.Zero)
                            {
                                try
                                {
                                    System.Windows.Media.Imaging.BitmapSource bs =
                                        System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                                            nicData.hIcon, Int32Rect.Empty,
                                            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                                    DestroyIcon(nicData.hIcon);

                                    if (bs != null)
                                    {
                                        bs.Freeze();
                                        trayIcon.Icon = bs;
                                    }
                                }
                                catch
                                {
                                    if (trayIcon.Icon == null)
                                    {
                                        trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                                    }
                                }
                            }
                            else
                            {
                                trayIcon.Icon = null;
                            }
                        }

                        trayIcon.HWnd = nicData.hWnd;
                        trayIcon.UID  = nicData.uID;

                        if ((NIF.GUID & nicData.uFlags) != 0)
                        {
                            trayIcon.GUID = nicData.guidItem;
                        }

                        // guess version in case we are receiving icons that aren't sending NIM_SETVERSION to new explorers
                        if ((NIF.VISTA_MASK & nicData.uFlags) != 0)
                        {
                            trayIcon.Version = 4;
                        }
                        else if ((NIF.XP_MASK & nicData.uFlags) != 0)
                        {
                            trayIcon.Version = 3;
                        }

                        if (nicData.uVersion > 0 && nicData.uVersion <= 4)
                        {
                            trayIcon.Version = nicData.uVersion;
                        }

                        if ((NIF.MESSAGE & nicData.uFlags) != 0)
                        {
                            trayIcon.CallbackMessage = nicData.uCallbackMessage;
                        }

                        if (!exists)
                        {
                            // default placement to a menu bar-like rect
                            trayIcon.Placement = defaultPlacement;

                            // set properties used for pinning
                            trayIcon.Path = Shell.GetPathForHandle(trayIcon.HWnd);
                            trayIcon.SetPinValues();

                            if (trayIcon.Icon == null)
                            {
                                trayIcon.Icon = Common.IconImageConverter.GetDefaultIcon();
                            }

                            TrayIcons.Add(trayIcon);
                            CairoLogger.Instance.Debug($"NotificationArea: Added: {trayIcon.Title} Path: {trayIcon.Path} Hidden: {trayIcon.IsHidden} GUID: {trayIcon.GUID} UID: {trayIcon.UID} Version: {trayIcon.Version}");

                            if ((NIM)message == NIM.NIM_MODIFY)
                            {
                                // return an error to the notifyicon as we received a modify for an icon we did not yet have
                                return(false);
                            }
                        }
                        else
                        {
                            CairoLogger.Instance.Debug($"NotificationArea: Modified: {trayIcon.Title}");
                        }
                    }
                    catch (Exception ex)
                    {
                        CairoLogger.Instance.Error("NotificationArea: Unable to modify the icon in the collection.", ex);
                    }
                }
                else if ((NIM)message == NIM.NIM_DELETE)
                {
                    try
                    {
                        if (!TrayIcons.Contains(trayIcon))
                        {
                            // Nothing to remove.
                            return(false);
                        }

                        TrayIcons.Remove(trayIcon);

                        CairoLogger.Instance.Debug($"NotificationArea: Removed: {nicData.szTip}");
                    }
                    catch (Exception ex)
                    {
                        CairoLogger.Instance.Error("NotificationArea: Unable to remove the icon from the collection.", ex);
                    }
                }
                else if ((NIM)message == NIM.NIM_SETVERSION)
                {
                    foreach (NotifyIcon ti in TrayIcons)
                    {
                        if (ti.Equals(nicData))
                        {
                            ti.Version = nicData.uVersion;
                            CairoLogger.Instance.Debug($"NotificationArea: Modified version to {ti.Version} on: {ti.Title}");
                            break;
                        }
                    }
                }
            }
            return(true);
        }