Solved

Popup menu inside TreeView problem

Posted on 2003-10-22
2
920 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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