Solved

Popup menu inside TreeView problem

Posted on 2003-10-22
2
909 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
Comment Utility
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
Comment Utility
My bad.
Don't need to switch from screen to client since TrackPopupMenu takes screen coordinates.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

10 Experts available now in Live!

Get 1:1 Help Now