系统菜单自动生成popup菜单。。简单(分享+送分)
OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu *hMenu; // The handle to the main menu.
int nMenu; // The index of the menu item.
CMenu hMenuOurs; // The pop-up menu that we are creating.
UINT uMenuState; // The menu state.
CMenu *hSubMenu; // A submenu.
char szBuf[128]; // A buffer to store the menu string.
// Get the main menu.
CWnd *pWnd=AfxGetMainWnd();
hMenu =pWnd->GetMenu();
nMenu = 0;
// Create a pop-up menu.
hMenuOurs.CreatePopupMenu();
// have finished creating your pop-up menu.
uMenuState=hMenu->GetMenuState(nMenu,MF_BYPOSITION);
while ((uMenuState !=-1))
{
if (uMenuState != -1)
{
// Get the menu string.
hMenu->GetMenuString(nMenu, szBuf,128,MF_BYPOSITION);
uMenuState=hMenu->GetMenuState(nMenu,MF_BYPOSITION);
if ((LOBYTE(uMenuState) & MF_POPUP) && uMenuState!=0xFFFFFFFF) // Pop-up menu.
{
hSubMenu =hMenu->GetSubMenu(nMenu);
hMenuOurs.AppendMenu(LOBYTE(uMenuState),(UINT )hSubMenu->m_hMenu,szBuf);
}
else // Is a menu item, get the ID.
{
//nID = hMenu->GetMenuItemID(nMenu);
//hMenuOurs.AppendMenu(LOBYTE(uMenuState),nID,szBuf);
}
nMenu++; // Get the next item.
}
}
// TrackPopupMenu expects screen coordinates.
ClientToScreen(&point);
hMenuOurs.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,pWnd,NULL);
while (hMenuOurs.RemoveMenu(0,MF_BYPOSITION))
;
// Destroy the pop-up menu.
hMenuOurs.DestroyMenu();
}