CTreeCtrl: How to cath the Mouse Click on an item?

Posted on 2003-12-11
Last Modified: 2013-11-20
I am trying to catch the Mouse Click event on a node item of a CTreeCtrl, but I have trouble getting the HTREEITEM refering to the node item that has been clicked.

I mapped the following message:

And have the following function:
void CMyDialog::OnClickMyTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult)
   HTREEITEM lNodeItem = ((NM_TREEVIEW*)pNMHDR)->itemNew.hItem;
   *pResult = 0;

But lNodeItem is always NULL.
And if I try this:
It is the item that was previously selected before the click event.

Any ideas on how I could get the HTREEITEM when a node item is clicked?

Question by:benhere

Accepted Solution

Priyesh earned 250 total points
ID: 9925532
Check if this works

void CMyDialog::OnClickMyTreeCtrl(NMHDR* pNMHDR, LRESULT* pResult)
      CPoint pt ;
      GetCursorPos(&pt) ;
      m_Tree.ScreenToClient(&pt) ;

      UINT unFlags = 0 ;
      HTREEITEM hItem = m_Tree.HitTest(pt, &unFlags) ;
      if((unFlags & TVHT_ONITEMLABEL) && hItem != NULL)
            TRACE("%s\n", m_Tree.GetItemText(hItem)) ;

Author Comment

ID: 9929292
Works fine, thanks a lot! :-)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 161
xyBalance chalenge 58 88
scoreUp challenge 14 56
Trying to run powershell  in a batch file. How do I do this? 8 63
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now