[ Home | Contents | Search | Next | Previous | Up ]
Date: 11/30/99
Time: 12:31:33 AM
Q. I use a TTreeWindow in my application.I want to popup menu when the user right-clicks on one item of the tree. I have a problem when the cliked item is not the selected item.
A. Personally, I hate it when tree windows revert to the previously
selected node after a right click. I like it when the node seleted with the right mouse
button *stays* selected. Here's how I do it. The trick is to determine which node the
cursor is over with a hit-test, then selecting it yourself. I use TVGN_CARET to make it
stay selected, though you could try TVGN_DROPHILITE (and then maybe resetting it in an
RBUTTONUP handler).
void TTree::EvRButtonDown(uint modKeys, TPoint& point)
{
TTwHitTestInfo HitInfo;
memset( &HitInfo, 0, sizeof(HitInfo) );
HitInfo.pt = point;
SendMessage( TVM_HITTEST, 0, (TParam2)&HitInfo );
//if ( HitInfo.flags & TVHT_ONITEMLABEL || HitInfo.flags &
TVHT_ONITEMICON ) { // version of Ken
if ( HitInfo.flags & TVHT_ONITEM || HitInfo.flags &
TVHT_ONITEMRIGHT ) {
// causes SelectionChanging and
SelectionChanged to be called
//
SendMessage( TVM_SELECTITEM, TVGN_CARET,
(TParam2)HitInfo.hItem );
if (GetContextMenu()) {
TPoint pt = point;
ClientToScreen(pt);
GetContextMenu()->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt, 0, *this);
return;
}
}
TTreeWindow::EvRButtonDown(modKeys, point);
}
--Ken
Little edited version of Ken's function. (Yura).