There is no difference between dialog and frame based applications regarding context menus of controls. To add a context menu to a list control, create the menu using the resource editor and add the
ON_WM_CONTEXTMENU()
/
OnContextMenu()
handler to your list control class:
void CMyListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
VERIFY(menu.LoadMenu(IDR_MYLIST_POPUP_MENU));
CMenu *pSub = menu.GetSubMenu(0);
int nCmd = pSub->TrackPopupMenuEx(
TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY,
point.x, point.y, AfxGetMainWnd(), NULL);
if (nCmd)
SendMessage(WM_COMMAND, nCmd);
}
Note that using
AfxGetMainWnd()
requires that the pointer to your main dialog window is assigned to the
CWinApp
member variable
m_pMainWnd
in
InitInstance()
.