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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
haveThree challenge 22 123
Execute multiple curl cmds with sleep and send output to file 10 114
c++  placing data into a form and an editbox 5 23
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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