Swapnil Gupta
Swapnil Gupta

Reputation: 8941

How to get Click Event of Treeview(CTreeCtrl) in MFC created at runtime?

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);

Upvotes: 2

Views: 9729

Answers (2)

user206705
user206705

Reputation:

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!

Upvotes: 1

Anthony Williams
Anthony Williams

Reputation: 68561

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.

Upvotes: 3

Related Questions