Right click on tree control will not select the item
-
Hi, I am using a tree control in a simple GUI. I responded to a right click on the control and popped up a context menu for it, so far so good. Next, I want to know which is the HTREEITEM that was selected, so I can do something with it. The problem is this will work only if the item was first selected by a left click first and then the right click. If I select an item by docuble click so it will open all of it's sub items and then right click on one of the sub items, the GetSelectedItem function will return me the root item and not the one I right clicked on. any solution ? Here are my right click and menu handling functions:
void Cfbe_sim_guiDlg::OnNMRclickSystemTree(NMHDR *pNMHDR, LRESULT *pResult) { DWORD dwPos=::GetMessagePos(); CPoint point((int)LOWORD(dwPos),(int)HIWORD(dwPos));//convert mouset to point object CMenu menu; menu.LoadMenu(IDR_MENU1); CMenu *pContextMenu=menu.GetSubMenu(0); pContextMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, point.x,point.y,AfxGetMainWnd(), NULL); *pResult = 0; }
void Cfbe_sim_guiDlg::OnTreeObjectproperties() { /*get handle to the selected item*/ HTREEITEM selected_item = m_ctl_system_tree.GetSelectedItem(); /*get the text of the selecte item*/ CString item_text = m_ctl_system_tree.GetItemText (selected_item); . . . }
-
Hi, I am using a tree control in a simple GUI. I responded to a right click on the control and popped up a context menu for it, so far so good. Next, I want to know which is the HTREEITEM that was selected, so I can do something with it. The problem is this will work only if the item was first selected by a left click first and then the right click. If I select an item by docuble click so it will open all of it's sub items and then right click on one of the sub items, the GetSelectedItem function will return me the root item and not the one I right clicked on. any solution ? Here are my right click and menu handling functions:
void Cfbe_sim_guiDlg::OnNMRclickSystemTree(NMHDR *pNMHDR, LRESULT *pResult) { DWORD dwPos=::GetMessagePos(); CPoint point((int)LOWORD(dwPos),(int)HIWORD(dwPos));//convert mouset to point object CMenu menu; menu.LoadMenu(IDR_MENU1); CMenu *pContextMenu=menu.GetSubMenu(0); pContextMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, point.x,point.y,AfxGetMainWnd(), NULL); *pResult = 0; }
void Cfbe_sim_guiDlg::OnTreeObjectproperties() { /*get handle to the selected item*/ HTREEITEM selected_item = m_ctl_system_tree.GetSelectedItem(); /*get the text of the selecte item*/ CString item_text = m_ctl_system_tree.GetItemText (selected_item); . . . }
That's by design. If you want to select the node the cursor is over then you need to do it manually. Something like this maybe...
// in response to right click notification...
POINT ClientCursPoint, ScreenCursPoint;
::GetCursorPos(&ScreenCursPoint);
ClientCursPoint = ScreenCursPoint; // (save ScreenCursPoint for TrackPopupMenu)
::ScreenToClient(m_ctl_system_tree, &ClientCursPoint);TVHITTESTINFO HTInfo;
HTInfo.pt = ClientCursPoint;
HTREEITEM hItem = TreeView_HitTest(m_ctl_system_tree, &HTInfo);if (hItem)
{
...
TreeView_SelectItem(m_ctl_system_tree, hItem);
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
That's by design. If you want to select the node the cursor is over then you need to do it manually. Something like this maybe...
// in response to right click notification...
POINT ClientCursPoint, ScreenCursPoint;
::GetCursorPos(&ScreenCursPoint);
ClientCursPoint = ScreenCursPoint; // (save ScreenCursPoint for TrackPopupMenu)
::ScreenToClient(m_ctl_system_tree, &ClientCursPoint);TVHITTESTINFO HTInfo;
HTInfo.pt = ClientCursPoint;
HTREEITEM hItem = TreeView_HitTest(m_ctl_system_tree, &HTInfo);if (hItem)
{
...
TreeView_SelectItem(m_ctl_system_tree, hItem);
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
excellent ! that did the trick, Thanks a lot