1.利用 Wizard 添加并编辑菜单资源;
2.为View窗口添加 WM_CONTEXTMENU消息映射函数 或者 WM_RBUTTONDOWN消息映射函数:
// WM_CONTEXTMENU
void CMyView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: 在此处添加消息处理程序代码
if (point.x==-1 && point.y==-1)
{
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
point=rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(IDR_POPMENU_VIEW));
CMenu *pPopup=menu.GetSubMenu(0);
ASSERT(pPopup!=NULL);
CWnd *pWndPopupOwner=this;
while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner=pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner);
}
// WM_RBUTTONDOWN
void CMyView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CMenu menu;
menu.LoadMenu(IDR_POPMENU_VIEW);
CMenu *pPopup=menu.GetSubMenu(0);
ClientToScreen(&point);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, GetParent());
CView::OnRButtonDown(nFlags, point);
}