Shell_NotifyIconGetRect (shell32)
Last changed: -24.155.240.198

.
Summary

Retrieves the Notifyicon bounding rectangle.

C# Signature:

    [DllImport("shell32.dll", SetLastError = true)]
    static extern int Shell_NotifyIconGetRect([In]ref NOTIFYICONIDENTIFIER identifier, [Out]out RECT iconLocation);

User-Defined Types:

NOTIFYICONIDENTIFIER

RECT

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public Int32 left;
        public Int32 top;
        public Int32 right;
        public Int32 bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NOTIFYICONIDENTIFIER
    {
        public uint cbSize;
        public IntPtr hWnd;
        public uint uID;
        public Guid guidItem;
    }

    [DllImport("shell32.dll", SetLastError = true)]
    private static extern int Shell_NotifyIconGetRect([In]ref NOTIFYICONIDENTIFIER identifier, [Out]out RECT iconLocation);

    public static RECT GetNotifyIconRect(IntPtr windowHandle, uint taskbarIconId)
    {
        var rect = new RECT();
        var notifyIcon = new NOTIFYICONIDENTIFIER();
        notifyIcon.cbSize = (uint)Marshal.SizeOf(notifyIcon.GetType());
        notifyIcon.hWnd = windowHandle;
        notifyIcon.uID = taskbarIconId;
        var hresult = Shell_NotifyIconGetRect(ref notifyIcon, out rect);
        if (hresult == 0x80004005)
        {
            // E_FAIL, will happen if the icon is not there.
            throw new Win32Exception(hresult, "Failed to get icon position.");
        }

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

        return rect;
    }

Documentation