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

删除回忆录丶 提交于 2019-12-11 04:46:58

问题


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



来源:https://stackoverflow.com/questions/5597525/how-do-i-position-a-notification-tray-icon-context-menu-on-windows-xp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!