Ejemplo n.º 1
0
        /// <summary>
        /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows 7+.)
        /// </summary>
        private static Rect?GetNotifyIconRect(TaskbarIcon icon)
        {
            // get notify icon id
            var iconDataField = icon.GetType().GetField("iconData", BindingFlags.NonPublic | BindingFlags.Instance);

            if (iconDataField == null)
            {
                return(null);
            }
            var iconData = (NotifyIconData)iconDataField.GetValue(icon);

            NativeMethods.RECT rect;
            var notifyIconId = new NativeMethods.NOTIFYICONIDENTIFIER {
                hWnd = iconData.WindowHandle,
                uID  = iconData.TaskbarIconId
            };

            notifyIconId.cbSize = (uint)Marshal.SizeOf(notifyIconId);

            var result = NativeMethods.Shell_NotifyIconGetRect(ref notifyIconId, out rect);

            // 0 means success, 1 means the notify icon is in the fly-out - either is fine
            if (result != 0 && result != 1)
            {
                return(null);
            }

            // convert to System.Rect and return
            return(rect);
        }