Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Right click on tree control will not select the item

Right click on tree control will not select the item

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shay Harel
    wrote on last edited by
    #1

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

    M 1 Reply Last reply
    0
    • S Shay Harel

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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        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:

        S Offline
        S Offline
        Shay Harel
        wrote on last edited by
        #3

        excellent ! that did the trick, Thanks a lot

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups