功能区UI动态按钮菜单编辑

时间:2016-01-06 08:53:00

标签: c++ mfc windows-ribbon-framework

我有一个带有功能区UI的应用程序。在此UI中,存在一个带有附加菜单的按钮。我想要做的是从按钮处理程序访问菜单,以动态添加和删除菜单项。

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 个答案:

答案 0 :(得分:1)

在CMainFRame窗口中,为AFX_WM_ON_BEFORE_SHOW_RIBBON_ITEM_MENU消息创建处理程序(ON_REGISTERED_MESSAGE)。

检查按钮的ID。删除所有以前的项目并添加您想要的项目。

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));
        }
      }
 ...