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;
}