We help IT Professionals succeed at work.

CListControl and cursor position

DevonportA1
DevonportA1 asked
on
Hi,

I'm using a CListControl in report view and when the user selects an item and right clicks a popUP menu appears.  However, even when no item is selected and the mouse is right clicked anywhere on the control the popUp menu still appears.  What I want is the rightClick to only work when the cursur is over one of the items in coumn one.  Does anyone know how I can achieve this.

The code I have is like this:


void CMyClass::OnListRClick( NMHDR * pNotifyStruct, LRESULT * result)
{
     ShowPopUpMenu();
}


void CMyClass::ShowPopUpMenu()
{
     POINT pt;  
     
     ::GetCursorPos(&pt);  
     CMenu menuPopup;

     menuPopup.CreatePopupMenu();    
     
     menuPopup.AppendMenu(MF_STRING|MF_BYCOMMAND,IDM_EDIT_ITEM,"Edit...");
     menuPopup.AppendMenu(MF_STRING|MF_BYCOMMAND,IDM_DELETE_ITEM,"Delete");
     menuPopup.TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);
}

Comment
Watch Question

Top Expert 2007

Commented:
Top Expert 2007

Commented:
Hey! That wasn't nice... I'll try again.

Try handling the WM_CONTEXTMENU message.
a) handle the WM_CONTEXTMENU message
b) if mouse position is (-1,-1), conext menu was called via keyboard - look if any item is selected
c) for "normal" coords, use CListCtrl::HitTest() to check where the click occured

Peter

Sr Program Manager
Commented:
Hi,

Just modify ur code like bellow.

void CListCtrlDlg::OnRclickMylist(NMHDR* pNMHDR, LRESULT* pResult)
{
     POINT sPt;
     ::GetCursorPos(&sPt);
     m_ctrlList.ScreenToClient(&sPt);

     LVHITTESTINFO lvHitTestInfo;
     lvHitTestInfo.pt = sPt;
     int nError = m_ctrlList.SubItemHitTest(&lvHitTestInfo);

     if(nError != -1)
     {
          //Popup the menu if on valid row and second column
          if((lvHitTestInfo.iItem != -1) && (lvHitTestInfo.iSubItem == 1))
          {
               //TODO:
               //Load the menu
               //Get the sub menu to popup
               //Call TrackPopupMenu(...) to show the popup menu
          }
     }

     *pResult = 0;
}

Try it out.
VinExpert

Author

Commented:
Thanks,

this works great

Explore More ContentExplore courses, solutions, and other research materials related to this topic.