How do I position a notification (tray) icon conte

2019-08-01 16:50发布

问题:

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).

回答1:

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



回答2:

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


回答3:

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