Advertisement

Right click on TreeCtrl Item for Context Menu

Started by May 21, 2001 05:21 AM
3 comments, last by Zeke 23 years, 8 months ago
When the user right clicks on an item in my MFC apps tree control I need it to bring up a context menu. All i need to know is how to find what item the user right clicked on. I cant just add a NM_RCLICK message because the function doesnt know what item was clicked (in fact you dont have to rClick on an item at all- anywhere in the window will do which is not what I want). I just want the user to right click on an item in the tree control and then a menu appears. Can anyone help me with this? Thanks for any help.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Say you have a CTreeCtrl in a dialog, all u have to do is add a NM_RCLICK message to the tree control and in the ''click'' method you find out which item the user clicked. Like this:

void CMyDlg::OnRclickProperty(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
pNMTreeView->itemNew; <== Here u go! =)

itemNew is a TVITEM and is identical to TV_ITEM (just a different naming convention). Based on this u should be able to get all the info u need to get the contextmenu working.

Advertisement
Thanks for the reply Talbot, that is what I tried before but the itemNew was never a valid item. Its probably something else wrong in my code but it just wouldnt work that way.

However I found a way around it by catching RButtonDown and then doing a CTreeCtrl::HitTest with the coords passed into OnRButtonDown().

Id imagine this is really not a good way of doing this but it works.

Thanks again for the reply
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Maybe you should consider simply handling WM_CONTEXTMENU for the menu, though you will still find it necessary to use the hit test to determine the item I believe.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Hey thanks felisandria thats a really good idea. I''ll have a go at that, I think it will take a step out of my code. Thanks again.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

This topic is closed to new replies.

Advertisement