Link to home
Start Free TrialLog in
Avatar of dkremer
dkremer

asked on

Popup menu inside TreeView problem

Hi.
I want to create a popup menu on a right cick in a tree view.
The popup should be different if the click was on an item or not.
My problem is that the popup created upon clicking not on an item isn't
shown in the right place (next to mouse cursor) but a bit far from it.

My code follows :

void CLeftView::OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult)
{
    CMenu mPopups, *pFloatingPopup = NULL;
    mPopups.LoadMenu(IDR_POPUPS);
    CPoint pos;
    GetCursorPos(&pos);
    GetTreeCtrl().ScreenToClient(&pos);
    UINT uFlags;
    HTREEITEM hItem = GetTreeCtrl().HitTest(pos, &uFlags);

    if (hItem != NULL && (uFlags & TVHT_ONITEM) != 0)
    {
        GetTreeCtrl().SelectItem(hItem);
        pFloatingPopup = mPopups.GetSubMenu(0);
    }
    else // not on item
        pFloatingPopup = mPopups.GetSubMenu(1);


    pFloatingPopup->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, pos.x,
pos.y, AfxGetMainWnd());

    *pResult = 0;
}

Any ideas as to why that happens and a fix ?

ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkremer
dkremer

ASKER

My bad.
Don't need to switch from screen to client since TrackPopupMenu takes screen coordinates.