Solved

CTreeCtrl right click selected item update

Posted on 2003-11-15
2
776 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 50 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 58
Separate into files by filename 12 67
post4 challenge 28 83
Line meaning 9 77
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now