Tuesday, July 21, 2009

MFC - Route Menu commands ElseWhere

Want to Handle Menu command where ever you want? - Use this code




void CUtilities::CmdRouteMenu(CWnd* pWnd, CMenu* pPopupMenu)
{
CCmdUI state;
state.m_pMenu = pPopupMenu;
state.m_pParentMenu = pPopupMenu;
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();



for (state.m_nIndex = 0;
state.m_nIndex < state.m_nIndexMax;
state.m_nIndex++)
{
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);

// menu separator or invalid cmd - ignore it
if (state.m_nID == 0) continue;

if (state.m_nID == (UINT)-1)
{
// possibly a popup menu, route to child menu if so
CMenu* pSub=pPopupMenu->GetSubMenu(state.m_nIndex);
if(pSub) CmdRouteMenu(pWnd,pSub);
}
else
{
// normal menu item, Auto disable if command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(pWnd, FALSE);
}
}
}

Usage:

CMenu menu;
if ( menu.LoadMenu(uMenuID) )
{
CMenu * pMenu = (CMenu*)menu.GetSubMenu(0);
CUtilities::CmdRouteMenu(&m_pParent, pMenu); // Make sure commands get to parent window
// Route commands to parent window:
POINT pt;
GetCursorPos(&pt);
pMenu->TrackPopupMenu(TPM_RIGHTBUTTON|TPM_LEFTALIGN, pt.x, pt.y, &m_pParent);
}


0 תגובות:

Post a Comment

 
Home | About | Link | Link
Simple Proff Blogger Template Created By Herro | Inspiring By Busy Bee Woo Themes