Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CTreeCtrl right click selected item update

Posted on 2003-11-15
2
Medium Priority
?
813 Views
Last Modified: 2013-11-20
This one has been driving me crazy.  I want to allow a user to perform actions on objects in a CTreeCtrl.  The problem is the SelectedItem of a CTreeCtrl does not update when just the right mouse button is clicked on an item.  I need it to so the user obtains the object they right click on to bring the context menu up.  To do this in the function NM_RCLICK is mapped to I get the cursor point, then after creating the popup MapWindowPoints to the CTreeCtrl and then run HitTest to find the object that the cursor is suposedly over.  It doesn't work.  It chooses an object 5 below the one the cursor is on.  Any ideas why this is or another way to get the current highlighted object (checking for TVIS_SELECTED doesn't work.  I tried that first).  Here is my current code:
    HTREEITEM hItem;

      CPoint point;

    CMenu mnuTop;


    GetCursorPos(&point);

    mnuTop.LoadMenu(IDR_BATCHES_POPUP);

    CMenu* pPopup = mnuTop.GetSubMenu(0);
    ASSERT_VALID(pPopup);
   
    pPopup->TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON |
                           TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd(), NULL);

    *pResult = 0;

      //Set item right clicked on as selected
      this->MapWindowPoints(FromHandle(m_BatchTree.m_hWnd), &point, 1);
      hItem = m_BatchTree.HitTest(point);
      if(hItem) m_BatchTree.SelectItem(hItem);

Thanks.
0
Comment
Question by:stringsandbeyond
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 9757666
This is code fragment from my project, it works:

    POINT point;
    POINT pt_screen;

    if ( ! GetCursorPos(&point) )
        return;

    pt_screen = point;                      // keep screen point (we need it to show menu)
    m_tree_control.ScreenToClient(&point);  // convert to tree control coordinates

    HTREEITEM hItem = m_tree_control.HitTest(point); // what item is under this point

    if ( ! hItem )
        return;

   ...
0
 

Author Comment

by:stringsandbeyond
ID: 9757767
Thanks.

I had assumed GetCursorPos() was returning client coords of the class it was called from.  It seems it returns screen coords.  Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

927 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