2

I have created a treeview at runtime in MFC application , I have added few nodes to it now i want to do some stuff on click of nodes so how i can get click event of treeview ?

My code looks like this :

CTreeCtrl *m_ctlTreeview;
m_ctlTreeview = new CTreeCtrl ;
m_ctlTreeview->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
                 TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
         TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |  
         TVS_TRACKSELECT,
                 CRect(25, 60, 385, 260), this, 0x1221);


hparentitem = m_ctlTreeview->InsertItem("Parent",TVI_ROOT);
m_ctlTreeview->InsertItem("Child", hparentitem);
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75

2 Answers2

3

One option is to add a handler for the notification messages for that child window ID (0x1221 in your example) to the parent class at design time using ON_NOTIFY in the message map as usual. If there are no messages, the handler won't be triggered.

Alternatively, you could add a generic WM_NOTIFY handler to the message map of the parent window with ON_MESSAGE, and then check to see if the message comes from your new tree control.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • See http://msdn.microsoft.com/en-us/library/k35k2bfs.aspx http://msdn.microsoft.com/en-us/library/749htf6k.aspx and http://msdn.microsoft.com/en-us/library/ff486107.aspx – Anthony Williams Sep 21 '10 at 07:40
1

I'm familiar with WTL coding, which has similarity with MFC. Where MFC has a CTreeCtrl, WTL has a CTreeViewCtrl.

The dialog class that contains the tree control should check for the following notifications with a notify code handler:

TVN_SELCHANGED -> OnTreeSelectionChange
NM_RCLICK      -> OnRButtonUp

I don't want to quote any WTL code, as it may only serve to confuse, but I hope that these messages help!