예제 #1
0
        private static bool TryGetNotifyIconIdentifier(NotifyIcon notifyIcon, out NOTIFYICONIDENTIFIER identifier)
        {
            identifier = new NOTIFYICONIDENTIFIER()
            {
                cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONIDENTIFIER))
            };

            int id;

            if (!TryGetNonPublicFieldValue(notifyIcon, "id", out id))
            {
                return(false);
            }

            NativeWindow window;

            if (!TryGetNonPublicFieldValue(notifyIcon, "window", out window))
            {
                return(false);
            }

            identifier.uID  = (uint)id;
            identifier.hWnd = window.Handle;
            return(true);
        }
예제 #2
0
        private NOTIFYICONIDENTIFIER GetNotifyIconIdentifier()
        {
            // Ugly hacks here
            var result = new NOTIFYICONIDENTIFIER();

            result.uID = (uint)(int)_notifyIconIdField.GetValue(_notifyIcon);
            var window = (NativeWindow)_notifyIconWindowField.GetValue(_notifyIcon);

            result.hWnd   = window.Handle;
            result.cbSize = (uint)Marshal.SizeOf(result);
            return(result);
        }
예제 #3
0
        public static RG GetIconRect(NotifyIcon icon)
        {
            RECT rect = new RECT();
            NOTIFYICONIDENTIFIER notifyIcon = new NOTIFYICONIDENTIFIER();

            notifyIcon.cbSize = Marshal.SizeOf(notifyIcon);
            notifyIcon.hWnd   = GetHandle(icon);
            notifyIcon.uID    = GetId(icon);

            int hresult = Shell_NotifyIconGetRect(ref notifyIcon, out rect);

            return(new RG(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top));
        }
예제 #4
0
    public static Rectangle GetIconRect(NotifyIcon icon)
    {
        NOTIFYICONIDENTIFIER notifyIcon = new NOTIFYICONIDENTIFIER();

        notifyIcon.cbSize = Marshal.SizeOf(notifyIcon);
        //use hWnd and id of NotifyIcon instead of guid is needed
        notifyIcon.hWnd = GetHandle(icon);
        notifyIcon.uID  = GetId(icon);

        int hresult = Shell_NotifyIconGetRect(ref notifyIcon, out var rect);

        //rect now has the position and size of icon

        return(new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top));
    }
예제 #5
0
        internal static RECT GetNotifyIconArea(IDisposable notifyicon)
        {
            var field                 = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            var num                   = (int)field.GetValue(notifyicon);
            var fieldInfo             = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            var window                = (NativeWindow)fieldInfo.GetValue(notifyicon);
            var notifyiconidentifier2 = new NOTIFYICONIDENTIFIER {
                hWnd = window.Handle, uID = (uint)num
            };

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

            RECT rect;

            Shell_NotifyIconGetRect(ref notifyiconidentifier2, out rect);
            return(rect);
        }
예제 #6
0
        private static Point GetNotifyIconPosition(IDisposable notifyicon)
        {
            var field                 = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            var num                   = (int)field.GetValue(notifyicon);
            var fieldInfo             = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            var window                = (NativeWindow)fieldInfo.GetValue(notifyicon);
            var notifyiconidentifier2 = new NOTIFYICONIDENTIFIER {
                hWnd = window.Handle, uID = (uint)num
            };

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

            RECT rect;

            Shell_NotifyIconGetRect(ref notifyiconidentifier2, out rect);
            return(new Point(rect.left + (rect.right - rect.left) / 2, rect.top + (rect.bottom - rect.top) / 2));
        }
    public static RECT GetNotifyIconRect(NotifyIcon icon)
    {
        var rect = new RECT();

        var notifyIcon = new NOTIFYICONIDENTIFIER
        {
            cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONIDENTIFIER)),
            hWnd   = GetHandle(icon),
            uID    = GetId(icon)
        };

        var result = Shell_NotifyIconGetRect(ref notifyIcon, out rect);

        if (result != WinError.S_OK)
        {
            throw new Win32Exception(result);
        }

        return(rect);
    }
예제 #8
0
        public static Rectangle GetNotifyIconRectangle(NotifyIcon notifyIcon)
        {
            if (!OperatingSystem.GteWindows7)
            {
                return(new Rectangle(0, 0, 0, 0));
            }

            NOTIFYICONIDENTIFIER _notifyIconIdentifier = new NOTIFYICONIDENTIFIER();

            _notifyIconIdentifier.cbSize = (uint)Marshal.SizeOf(_notifyIconIdentifier);
            _notifyIconIdentifier.hWnd   = GetHandle(notifyIcon);
            _notifyIconIdentifier.uID    = (uint)GetUID(notifyIcon);

            RECT _notifyIconLocation = new RECT();

            Shell_NotifyIconGetRect(ref _notifyIconIdentifier, out _notifyIconLocation);

            return(Rectangle.FromLTRB(_notifyIconLocation.left, _notifyIconLocation.top, _notifyIconLocation.right,
                                      _notifyIconLocation.bottom));
        }
예제 #9
0
        /// <summary>
        /// Gets position of the tray icon on the screen
        /// </summary>
        /// <returns>Position of the tray icon on the screen</returns>
        /// <remarks>Works only on Win7 and later. Will return <see cref="Cursor.Position"/> when running on earlier OS</remarks>
        public Point GetIconPosition()
        {
            var ident = new NOTIFYICONIDENTIFIER
            {
                cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONIDENTIFIER)),
                hWnd   = _ownerForm.Handle,
                uID    = (uint)_id
            };
            //ident.guidItem = guid; // don't know why, but setting GUID produces "The parameter is incorrect" HRESULT

            var rect   = new Rectangle();
            var result = Shell_NotifyIconGetRect(ref ident, ref rect);

            if (result != 0)
            {
                throw new Win32Exception(result);
            }

            return(new Point(rect.Left, rect.Top));
        }
예제 #10
0
        public static RECT GetNotifyIconLocation(NotifyIcon notifyIcon)
        {
            FieldInfo idFieldInfo = notifyIcon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            int       iconid      = (int)idFieldInfo.GetValue(notifyIcon);

            FieldInfo    windowFieldInfo = notifyIcon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            NativeWindow nativeWindow    = (NativeWindow)windowFieldInfo.GetValue(notifyIcon);
            IntPtr       iconhandle      = nativeWindow.Handle;

            RECT rect = new RECT();
            NOTIFYICONIDENTIFIER nid = new NOTIFYICONIDENTIFIER()
            {
                hWnd = iconhandle,
                uID  = (uint)iconid
            };

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

            int result = Shell_NotifyIconGetRect(ref nid, out rect);

            return(rect);
        }
예제 #11
0
        public static Rect?GetNotifyIconRectangle(NotifyIcon notifyicon)
        {
            // get notify icon id
            FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            int       iconid      = (int)idFieldInfo.GetValue(notifyicon);

            // get notify icon hwnd
            FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);

            System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon);
            IntPtr iconhandle = nativeWindow.Handle;

            if (iconhandle == IntPtr.Zero)
            {
                return(null);
            }

            RECT rect = new RECT();
            NOTIFYICONIDENTIFIER nid = new NOTIFYICONIDENTIFIER()
            {
                hWnd = iconhandle,
                uID  = (uint)iconid
            };

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

            int result = Shell_NotifyIconGetRect(ref nid, 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);
        }
예제 #12
0
 public static extern int Shell_NotifyIconGetRect(ref NOTIFYICONIDENTIFIER identifier, out RECT iconLocation);
예제 #13
0
        internal static RECT GetNotifyIconArea(IDisposable notifyicon)
        {
            var field = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            var num = (int)field.GetValue(notifyicon);
            var fieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            var window = (NativeWindow)fieldInfo.GetValue(notifyicon);
            var notifyiconidentifier2 = new NOTIFYICONIDENTIFIER { hWnd = window.Handle, uID = (uint)num };
            notifyiconidentifier2.cbSize = (uint)Marshal.SizeOf(notifyiconidentifier2);

            RECT rect;
            Shell_NotifyIconGetRect(ref notifyiconidentifier2, out rect);
            return rect;
        }
예제 #14
0
 public int[] GetRectangle()
 {
     RECT rect = new RECT();
     NOTIFYICONIDENTIFIER nid = new NOTIFYICONIDENTIFIER()
     {
         hWnd = iconData.WindowHandle,
         uID = (uint)iconData.TaskbarIconId
     };
     nid.cbSize = (uint)Marshal.SizeOf(nid);
     Shell_NotifyIconGetRect(ref nid, out rect);
     int[] result = new int[4];
     result[0] = rect.Bottom;
     result[1] = rect.Top;
     result[2] = rect.Left;
     result[3] = rect.Right;
     return result;
 }
예제 #15
0
 private static extern int Shell_NotifyIconGetRect(ref NOTIFYICONIDENTIFIER identifier, out RECT iconLocation);
예제 #16
0
 private static extern Int32 Shell_NotifyIconGetRect([In] ref NOTIFYICONIDENTIFIER identifier, [Out] out RECT iconLocation);
예제 #17
0
 public static extern int Shell_NotifyIconGetRect([In] ref NOTIFYICONIDENTIFIER identifier, [Out] out RectStruct iconLocation);
예제 #18
0
        private static Point GetNotifyIconPosition(IDisposable notifyicon)
        {
            var field = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            var num = (int) field.GetValue(notifyicon);
            var fieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
            var window = (NativeWindow) fieldInfo.GetValue(notifyicon);
            var notifyiconidentifier2 = new NOTIFYICONIDENTIFIER {hWnd = window.Handle, uID = (uint) num};
            notifyiconidentifier2.cbSize = (uint) Marshal.SizeOf(notifyiconidentifier2);

            RECT rect;
            Shell_NotifyIconGetRect(ref notifyiconidentifier2, out rect);
            return new Point(rect.left + (rect.right - rect.left) / 2, rect.top + (rect.bottom - rect.top) / 2);
        }