Solved

Popup menu inside TreeView problem

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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