Context Menu Over ListCtrl Question
Hi,
I have a list control usually with several entries.
I am trying to implement a context menu that will either
delete the selected entry or clear all the entries.
My main problem is that I can create the context menu and it comes up,
but it comes up everywhere on the form and I want it to come up only
when right clicked over the listview.
(And then I have to check if and which item is selected ...)
The code I'm using seems to work anywhere on the dialog. How can
I limit the popup menu to only work when over the list control?
Thanks very much...
Tony C.
code used so far:
//==============================================================
void CAstroCalcVC1Dlg::OnNMRClickList1(NMHDR *pNMHDR, LRESULT
*pResult)
{
LPNMITEMACTIVATE pNMItemActivate =
reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
CMenu menu;
VERIFY(menu.LoadMenu(IDR_POPUP_MENU));
CMenu* pPopup = menu.GetSubMenu(0);
int selected = m_list.GetNextItem(-1,LVNI_SELECTED);
if(selected <0) {
pPopup->EnableMenuItem(1,MF_GRAYED);
}
RECT rect;
GetWindowRect(&rect);
CPoint mousepos;
GetCursorPos(&mousepos);
pPopup->TrackPopupMenu(NULL,mousepos.x,mousepos.y, this);
*pResult = 0;
}
//============================================================
//AND
void CAstroCalcVC1Dlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu mnuPopup;
mnuPopup.LoadMenu(IDR_POPUP_MENU);
// Get a pointer to the button
CListCtrl *pLV;
pLV = reinterpret_cast<CListCtrl *>(GetDlgItem(IDC_LIST1));
CRect rectLV;
pLV->GetWindowRect(&rectLV);
// Get a pointer to the first item of the menu
CMenu *mnuPopupMenu = mnuPopup.GetSubMenu(0);
ASSERT(mnuPopupMenu);
// Find out if the user right-clicked the listctrl
// because we are interested only in the listctrl
if( rectLV.PtInRect(point) ) // Since the user right-clicked
the button, display the context menu
{
mnuPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y, this);
}
}
==>>Neither one works such the the popup comes up only over the list
control ...