Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CTreeCtrl right click selected item update

Posted on 2003-11-15
2
Medium Priority
?
804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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