To get the clicking item in an MFC tree control, you can override the OnLButtonDown() function in your CTreeCtrl derived class and use the HitTest() function to determine which item was clicked. Here's an example code snippet:
void CMyTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
UINT uFlags = 0;
HTREEITEM hItem = HitTest(point, &uFlags);
if (hItem != NULL && (uFlags & TVHT_ONITEM) != 0)
{
// The item was clicked, do something with it
// For example, retrieve its text:
CString strText = GetItemText(hItem);
// Or select it:
SelectItem(hItem);
}
CTreeCtrl::OnLButtonDown(nFlags, point);
}
In this code, we first call the HitTest() function to get the handle of the nearest tree item to the clicked point. The function returns NULL if no item was clicked. We also pass a pointer to a UINT variable to receive additional information about where the click occurred on the item (for example, on its label, checkbox, or icon). If the clicked item is valid and the click occurred on the item (not on the background of the control), we can do something with it. In this example, we retrieve its text using the GetItemText() function or select it using the SelectItem() function. Finally, we call the base class implementation of OnLButtonDown() to allow normal processing of the message.