Solved

Popup menu inside TreeView problem

Posted on 2003-10-22
2
915 Views
Last Modified: 2013-11-20
Hi.
I want to create a popup menu on a right cick in a tree view.
The popup should be different if the click was on an item or not.
My problem is that the popup created upon clicking not on an item isn't
shown in the right place (next to mouse cursor) but a bit far from it.

My code follows :

void CLeftView::OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult)
{
    CMenu mPopups, *pFloatingPopup = NULL;
    mPopups.LoadMenu(IDR_POPUPS);
    CPoint pos;
    GetCursorPos(&pos);
    GetTreeCtrl().ScreenToClient(&pos);
    UINT uFlags;
    HTREEITEM hItem = GetTreeCtrl().HitTest(pos, &uFlags);

    if (hItem != NULL && (uFlags & TVHT_ONITEM) != 0)
    {
        GetTreeCtrl().SelectItem(hItem);
        pFloatingPopup = mPopups.GetSubMenu(0);
    }
    else // not on item
        pFloatingPopup = mPopups.GetSubMenu(1);


    pFloatingPopup->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, pos.x,
pos.y, AfxGetMainWnd());

    *pResult = 0;
}

Any ideas as to why that happens and a fix ?

0
Comment
Question by:dkremer
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 9599654
TrackPopupMenu x, y parameters are screen coordinates and you already converted pos to client coordinates.

void CLeftView::OnNMRclick(NMHDR *pNMHDR, LRESULT *pResult)
{
   CMenu mPopups, *pFloatingPopup = NULL;
   mPopups.LoadMenu(IDR_POPUPS);
   CPoint pos, pos1;
   GetCursorPos(&pos);
   pos1 = pos;
   GetTreeCtrl().ScreenToClient(&pos);
   UINT uFlags;
   HTREEITEM hItem = GetTreeCtrl().HitTest(pos, &uFlags);

   if (hItem != NULL && (uFlags & TVHT_ONITEM) != 0)
   {
       GetTreeCtrl().SelectItem(hItem);
       pFloatingPopup = mPopups.GetSubMenu(0);
   }
   else // not on item
       pFloatingPopup = mPopups.GetSubMenu(1);


   pFloatingPopup->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, pos1.x,
pos1.y, AfxGetMainWnd());

   *pResult = 0;
}
0
 
LVL 1

Author Comment

by:dkremer
ID: 9604914
My bad.
Don't need to switch from screen to client since TrackPopupMenu takes screen coordinates.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 48
abstract class with all non abstract mentods 6 76
Use of condition with 'serial' in ansible 2 49
changeXy challenge 13 67
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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

15 Experts available now in Live!

Get 1:1 Help Now