Treeview: Right Mouse Button
Quote:
> I have a tree control on a dialog box, which responds to the right mouse
> button by displaying a popup-menu. However, if the right-mouse button is
> clicked on an item which is not currently selected, the focus remains with
> the previous item. Does anybody know of a way of changing the focus to the
> item which the mouse right-clicked on?
Here's some code that works for me (in an OnRclick handler in a CTreeView):
CPoint pt(::GetMessagePos ());
ScreenToClient(&pt);
CTreeCtrl& theTree = GetTreeCtrl();
// find item they clicked on
TV_HITTESTINFO hitTest;
memset(&hitTest, 0, sizeof(TVHITTESTINFO));
hitTest.pt = pt;
theTree.HitTest(&hitTest);
// if found one
if (hitTest.flags & LVHT_ONITEM)
{
// select that item & notify document of change
VERIFY(theTree.SelectItem(hitTest.hItem));
//... specialized view code omitted for clarity
// now do the context menu
CRect rect;
theTree.GetItemRect(hitTest.hItem, &rect, TRUE);
ClientToScreen(rect);
CPoint point(rect.left, rect.bottom);
OnContextMenu(this, point);
}
Jim [VC/MFC MVP]
To send mail, change spam-me-not to msn