We modified the RibbonBar sample application for you:
http://www.prof-uis.com/download/forums/TestDynamicMenuInRibbon.zip
This modified sample application has the new ribbon button displaying dynamically constructed popup menu. The new ribbon button can be seen in the Clipboard group of buttons inside the Home ribbon tab page. First of all, we inserted the following code into the CMainFrame::_InitRibbonNode_Home_Clipboard()
method for inserting new ribbon button into the ribbon bar:
CExtRibbonNode * pDynMenuRibbonButton = new CExtRibbonNode( 123, 0, NULL, 0, _T("Button with dynamic menu") );
pDynMenuRibbonButton->RibbonILE_RuleRemoveEntriesByILV( true, false, true );
pDynMenuRibbonButton->InsertNode( &m_wndRibbonBar, new CExtCustomizeCmdTreeNode( 124, 124 ) );
pRibbonGroup->InsertNode( NULL, pDynMenuRibbonButton );
Second, we implemented the
WindowProc()
virtual method in the
CMainFrame
class for initializing popup menu of the ribbon button on the fly, just before it appears on the screen:
virtual LRESULT WindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
if( message == CExtPopupMenuWnd::g_nMsgPrepareMenu )
{
CExtPopupMenuWnd::MsgPrepareMenuData_t * pData =
reinterpret_cast < CExtPopupMenuWnd::MsgPrepareMenuData_t * > ( wParam );
ASSERT( pData != NULL );
CExtPopupMenuWnd * pPopup = pData->m_pPopup;
ASSERT( pPopup != NULL );
INT nReplacePos = pPopup->ItemFindPosForCmdID( 124 );
if( nReplacePos >= 0 )
{
pPopup->ItemRemove( nReplacePos );
pPopup->ItemInsertCommand( 1000, nReplacePos + 0, _T("Dynamically created menu item 1") );
pPopup->ItemInsertCommand( 1001, nReplacePos + 1, _T("Dynamically created menu item 2") );
pPopup->ItemInsertCommand( 1002, nReplacePos + 2, _T("Dynamically created menu item 3") );
pData->m_bMenuChanged = true;
return 1L;
}
}
return CExtNCW < CFrameWnd > :: WindowProc( message, wParam, lParam );
}
That’s all. Please note, the
124
command identifier value is used in the both code snippets and maker menu item identifier.