veronicas
asked on
CListCtrl right click menu
I want to put a popup menu for my users when the right click on a CListCtrl. I need to check if they are actually on an item, or in white space. If they are on an item, a menu will be presented that says "Edit" or "Add". If they are on white space, just "Add" will be present.
How can I do this? Do I need 2 submenus? How can I tell if an item is highlighted in the list?
How can I do this? Do I need 2 submenus? How can I tell if an item is highlighted in the list?
Add a handler for a right click in the list, then add code like this:
CMenu menu;
VERIFY(menu.LoadMenu(IDR_S MENU));
CMenu* pPopup = menu.GetSubMenu(0);
int selected = m_listItems.GetNextItem(-1 ,LVNI_SELE CTED);
if(selected <0) {
pPopup->EnableMenuItem(ID_ ITEMEDIT,M F_GRAYED);
}
RECT rect;
GetWindowRect(&rect);
CPoint mousepos;
GetCursorPos(&mousepos);
pPopup->TrackPopupMenu(NUL L,mousepos .x,mousepo s.y, this);
Ken
CMenu menu;
VERIFY(menu.LoadMenu(IDR_S
CMenu* pPopup = menu.GetSubMenu(0);
int selected = m_listItems.GetNextItem(-1
if(selected <0) {
pPopup->EnableMenuItem(ID_
}
RECT rect;
GetWindowRect(&rect);
CPoint mousepos;
GetCursorPos(&mousepos);
pPopup->TrackPopupMenu(NUL
Ken
ASKER
kmurphy99's answer is the best.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent!
Now in the Class you can put in a message handler
ON_WM_CONTEXTMENU()
and the function goes something like this :
void mylistctrl::OnContextMenu(
{
if (point.x == -1 && point.y == -1)
{
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(ID_PO
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle(
pPopup->TrackPopupMenu(TPM
}
}