I am creating a program like Windows Task Manager.
The working environment is visual c ++ 6.0.
void CProcess01Dlg::OnRclickListCtrl(NMHDR* pNMHDR, LRESULT* pResult) {
CPoint ptInList, ptInSrceen;
GetCursorPos(&ptInSrceen);
ptInList = ptInSrceen;
m_ctrlList.ScreenToClient(&ptInList);
POSITION pos = m_ctrlList.GetFirstSelectedItemPosition();
int nListIndex = m_ctrlList.GetNextItem(-1, LVNI_SELECTED);
CMenu menu, *pMenu;
menu.LoadMenuA(IDR_MENU1);
CString str;
str.Format("%d",nListIndex);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
if( 0 <= nListIndex)
{
pMenu = menu.GetSubMenu(0);
}
else
{
}
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, ptInSrceen.x, ptInSrceen.y, this);
}
The above code is a function that handles events when an item in list control is right-clicked in MFC.
I want to add an event in the context menu when a context menu appears when I right-click an item.
Tell us how you're handling the event.
Thank you :)
Use InsertMenu
and/or AppendMenu
to add more items to the menu.
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu* popup = menu.GetSubMenu(0);
popup->InsertMenu(MF_STRING, MF_BYPOSITION, ID_XXX1, "Insert");
popup->AppendMenu(MF_STRING, ID_XXX2, "Append");
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, ptInSrceen.x, ptInSrceen.y, this);
The last parameter in TrackPopupMenu
is the handle of the window which will receives the menu messages. You just need to handle the commands in your dialog:
BEGIN_MESSAGE_MAP(CProcess01Dlg, CDialogEx)
ON_COMMAND(ID_FILE_NEW, onfilenew)
ON_COMMAND(ID_XXX1, foo)
...
END_MESSAGE_MAP()
CProcess01Dlg::foo()
{
...
}