Ribbon UI dynamic button menu edit

900 views Asked by At

I have an application with a ribbon UI. In this UI a button exists with a menu attached to it. What I wish to do is access the menu from the button handler to dynamically add and remove menu items.

void
CMyScrollView::OnMenuButtonHandler ()
{
  // TODO: Add your command handler code here
  CMFCRibbonBar *pRibbon  = ((CMDIFrameWndEx*)GetTopLevelFrame())GetRibbonBar()
  // Control ID_BTN_EDIT_MENU
  // This where I would like to isolate and vary menu contents
}
1

There are 1 answers

0
xMRi On BEST ANSWER

In the CMainFRame window create a handler for the AFX_WM_ON_BEFORE_SHOW_RIBBON_ITEM_MENU message (ON_REGISTERED_MESSAGE).

Check for the Id of the button. Remove all previous items and add the one, you want.

LRESULT CMainFrame::OnBeforeShowRibbonItemMenu(WPARAM,LPARAM lp)
{
  CMFCRibbonBaseElement *pElement = reinterpret_cast<CMFCRibbonBaseElement*>(lp);

  // Try to get our menu button
  switch (pElement->GetID())
  {
    case ID_RIBBON_DROPDOWN_BUTTON:
    {
      CMFCRibbonButton *pButton = DYNAMIC_DOWNCAST(CMFCRibbonButton, pElement);
      if (pButton)
      {
        // MY_LIST copntains members with the ID and the text: m_uiCmdId, m_strTitle
        const MY_LIST &list = ....;
        if (list.size()!=0)
        {
          pButton->RemoveAllSubItems();

          for (it = list.begin(); it!=list.end(); ++it)
            pButton->AddSubItem(new CSomeKindOfRibbonButton(it->m_uiCmdId, it->m_strTitle));
        }
      }
 ...