Ian Olsen
Ian Olsen

Reputation: 153

How do I position a notification (tray) icon context menu on Windows XP?

I'm using C++ and Win32.

I want my context menu and settings dialog to show up near the tray icon. I think I need the icon's coordinates to do that.

Shell_NotifyIconGetRect wasn't available until Windows 7.

WM_CONTEXTMENU is available starting in Win2k, but only provides coordinates in wParam as of Vista (and when specifying NOTIFYICON_VERSION_4).

Upvotes: 2

Views: 2050

Answers (3)

Anders
Anders

Reputation: 101756

To display the menu, all you need is the coords passed to you by WM_CONTEXTMENU or WM_RBUTTONUP (These are of course not normal messages, but something generated by the tray and you therefore don't have to deal with mouse vs keyboard)

Shell_NotifyIconGetRect is used if you want to display a toast (custom window) near the tray. On < 7 you can emulate it with findwindow by looking for the TrayNotifyWnd class with Shell_TrayWnd as the parent

Upvotes: 1

Ian Olsen
Ian Olsen

Reputation: 153

Retrieving the click coordinates with GetCursorPos works well:

// Inside WndProc's switch(message)...
case WM_APP_NOTIFYCALLBACK:
    switch (LOWORD(lParam))
    {
    case WM_CONTEXTMENU: // XP and later
        {
            POINT pt = {};
            if( GetCursorPos(&pt) )
                ShowContextMenu(hWnd, pt, iStatus);
        }
        break;
    // ...
    }
    // ...

Upvotes: 2

Erik
Erik

Reputation: 91310

The correct way of solving this is to either use the mouse message coordinates, or GetMessagePos for other messages.

Upvotes: 2

Related Questions