Search code examples
c++mfcribbonribbon-control

Switch ribbon category for the user


I have a program written in C++ using MFC. It has a ribbon UI, Office style.

I would like to achieve the following - Suppose the user has highlighted category A of the ribbon, and clicks a button in that category, I would like my program to switch to category B and highlight it for him in response to that click.

This might sound strange, but I really have a use case where the user might not realize that he needs to switch categories, so I would like to do it for him automatically.

Can this be done?


Solution

  • Perhaps you're looking for CMFCRibbonBar::SetActiveCategory.

    There is a similar question on CodeProject that includes a sample function for programmatically selecting a category by name. Reproduced below for information:

    int CYourClassName::ActivateCategoryByName( CString strCategoryName)
    {
        // Grab Pointer to MainFrame 
        CMainFrame* pMainFrame = ( CMainFrame*) AfxGetMainWnd();
    
        // Grab Pointer to RibbonBar
        CMFCRibbonBar* pmrb = &pMainFrame->m_wndRibbonBar;
    
        // Get Category Count
        int nCategoryCount = pmrb->GetCategoryCount();
    
        // Scan Category
        for ( int nCategoryNdx = 0; nCategoryNdx < nCategoryCount; nCategoryNdx++)
        {
            // Grab Pointer to Category
            CMFCRibbonCategory* pmrc = pmrb->GetCategory( nCategoryNdx);
    
            // Get Category Name
            CString strName = pmrc->GetName();
    
            // Check for Requested Category
            if ( strName == strCategoryName)
            {
                pmrb->ShowCategory( nCategoryNdx, TRUE);
                pmrb->SetActiveCategory( pmrc, TRUE);       
                return nCategoryNdx;
            }
        }
    
        return -1;
    }