Пример #1
0
        private static bool CanGetNotifyIconIdentifier(NotifyIcon notifyIcon,
                                                       out NativeMethods.NOTIFYICONIDENTIFIER identifier)
        {
            // You can either use uID + hWnd or a GUID, but GUID is new to Win7 and isn't used by NotifyIcon anyway.

            identifier = new NativeMethods.NOTIFYICONIDENTIFIER();

            BindingFlags flags  = BindingFlags.NonPublic | BindingFlags.Instance;
            Type         niType = typeof(NotifyIcon);

            FieldInfo idFieldInfo = niType.GetField("id", flags);

            identifier.uID = (uint)(int)idFieldInfo.GetValue(notifyIcon);

            FieldInfo    windowFieldInfo = niType.GetField("window", flags);
            NativeWindow nativeWindow    = (NativeWindow)windowFieldInfo.GetValue(notifyIcon);

            identifier.hWnd = nativeWindow.Handle;

            if (identifier.hWnd == null || identifier.hWnd == IntPtr.Zero)
            {
                return(false);
            }

            identifier.cbSize = (uint)Marshal.SizeOf(identifier);
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows 7+.)
        /// </summary>
        /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param>
        /// <returns>The location of the specified NotifyIcon. Null if the location could not be found.</returns>
        public static Rect?GetNotifyIconRectWindows7(NotifyIcon notifyicon)
        {
            if (Compatibility.CurrentWindowsVersion != Compatibility.WindowsVersion.Windows7Plus)
            {
                throw new PlatformNotSupportedException("This method can only be used under Windows 7 or later. Please use GetNotifyIconRectangleLegacy() if you use an earlier operating system.");
            }

            // get notify icon id
            FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            int       iconid      = (int)idFieldInfo.GetValue(notifyicon);

            // get notify icon hwnd
            IntPtr iconhandle;

            try
            {
                FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
                System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon);
                iconhandle = nativeWindow.Handle;
                if (iconhandle == null || iconhandle == IntPtr.Zero)
                {
                    return(null);
                }
            } catch {
                return(null);
            }

            NativeMethods.RECT rect = new NativeMethods.RECT();
            NativeMethods.NOTIFYICONIDENTIFIER nid = new NativeMethods.NOTIFYICONIDENTIFIER()
            {
                hWnd = iconhandle,
                uID  = (uint)iconid
            };
            nid.cbSize = (uint)Marshal.SizeOf(nid);

            int result = NativeMethods.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);
        }
        /// <summary>
        /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows 7+.)
        /// </summary>
        /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param>
        /// <returns>The location of the specified NotifyIcon. Null if the location could not be found.</returns>
        public static Rect? GetNotifyIconRectWindows7(NotifyIcon notifyicon)
        {
            if (Compatibility.CurrentWindowsVersion != Compatibility.WindowsVersion.Windows7Plus)
                throw new PlatformNotSupportedException("This method can only be used under Windows 7 or later. Please use GetNotifyIconRectangleLegacy() if you use an earlier operating system.");

            // 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 == null || iconhandle == IntPtr.Zero)
                return null;

            NativeMethods.RECT rect = new NativeMethods.RECT();
            NativeMethods.NOTIFYICONIDENTIFIER nid = new NativeMethods.NOTIFYICONIDENTIFIER()
            {
                hWnd = iconhandle,
                uID = (uint)iconid
            };
            nid.cbSize = (uint)Marshal.SizeOf(nid);

            int result = NativeMethods.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;
        }