Question

I'm having troubles showing my tray menu above the taskbar. It doesn't matter what I specify as y-position, it only gets against the taskbar at best. The tray menu is a resource.

This is the code I'm using for a rightclick on the systrayicon:

// The message loop
case WM_RBUTTONUP:
{
    CPoint point;
    ::GetCursorPos(&point);

    CMenu menu;
    menu.LoadMenu(IDR_MENU1);
    HMENU hMenu = menu.GetSubMenu(0)->Detach();

    CMFCPopupMenu* pMenu = theApp.GetContextMenuManager()->ShowPopupMenu(hMenu, point.x-5, point.y, this, TRUE);
    pMenu->SetForegroundWindow();
    break;
}
Was it helpful?

Solution

Indeed RecalcLayout makes sure that a popup is displayed within monitor's working area (not obscuring taskbar. But there is nothing to prevent you to override this behavior. The difference between a Windows’ menu and CMFCPopupMenu is that the menu is a window that is created by the OS and CMFCPopupMenu is a frame window that mimics the behavior of the menu. You were almost there but you missed the fact that SetForegroundWindow does not move window, it merely changes Z-order. I personally do not see any advantage of doing it. However, as I say nothing is impossible to achieve. Do following if you really want to do it against advice:

    CMFCPopupMenu* pMFCMenu = pCntxtMgr->ShowPopupMenu(hMenu, ptClick.x, ptClick.y, AfxGetMainWnd());
    CRect rectMenu;
    pMFCMenu->GetWindowRect(rectMenu);

    pMFCMenu->SetWindowPos(&CWnd::wndTopMost, rectMenu.left, rectMenu.top + 25, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);

25 is just an arbitrary number I used for demo purpose. You will have to calculate it to prevent menu bottom appearing off the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top