一、創(chuàng)建新工程
二、編輯菜單資源
1、添加菜單 按“Ctrl+R”,雙擊“Menu”圖標

2、于菜單編輯器內編輯菜單

三、添加函數(shù)


四、添加代碼(紅色部分)
void CCMenuView::OnUpdateShow(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
MessageBox("右鍵彈出菜單測試!");
}
void CCMenuView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu menu;
menu.LoadMenu(IDR_MENU1);
CMenu *pPopup=menu.GetSubMenu(0);
ClientToScreen(&point);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
this);
CView::OnRButtonDown(nFlags, point);
}
五、編譯
六、運行

七、函數(shù)說明
CMenu::TrackPopupMenu函數(shù)聲明
BOOL TrackPopupMenu(UINT nFlags,int x,int y,CWnd*pWnd,LPCRECT LPrECT=0)
nFlags:彈出菜單的位置
x:彈出菜單的水平位置
y:彈出菜單的垂直位置
pWnd:彈出菜單的所有者
lpRect:一包含矩形坐標的RECT結構體指針。
功能:顯示一彈出菜單。函數(shù)調用成功,返回非零值;否則返回零。