locked
MFC Feature Pack: SetCheck() in Ribbon RRS feed

  • Question

  • I'm trying to figure out how to (a) set a check box on the ribbon as checked and (b) how to make a button on the ribbon appear pressed.

    I can get a pointer to my checkbox/button like this:

    CMFCRibbonButton *pButton = (CMFCRibbonButton *)m_wndRibbonBar.FindByID( ID_BUTTON );

    It seems like there should be a CMFCRibbonButton::SetCheck() member function:

    pButton->SetCheck( true );

    But there isn't. Before the Feature Pack, I'd do this with a CButton, which does have a CButton::SetCheck() function.

    Monday, May 11, 2009 2:52 PM

Answers

  • Hello,

    1. For checking the checkbox, we could use CCmdUI::SetCheck() on the OnUpdateRibbonXXX() handler, i.e.
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
        ON_COMMAND(ID_RIBBON_BTN_XXX, OnRibbonBtnXXX)
        ON_UPDATE_COMMAND_UI(ID_RIBBON_BTN_XXX, OnUpdateRibbonBtnXXX)
    END_MESSAGE_MAP()
    
    void CMainFrame::OnRibbonBtnXXX() 
    {
        m_bCheckBoxXXX = !m_bCheckBoxXXX;
    }
    
    void CMainFrame::OnUpdateRibbonBtnXXX(CCmdUI* pCmdUI) 
    {
        pCmdUI->SetCheck (m_bCheckBoxXXX);	
    }
    2. For the button pressed state, I'm sorry that there is no a method for it currently as I know,  the corresponding variable m_bIsPressed of the class CMFCRibbonBaseElement(this is the base class of CMFCRibbonButton) is protected, since the class CMFCRibbonBar is a friend class of the class CMFCRibbonBaseElement, so it can handle the button state in its code(OnLButtonDown, OnLButtonUp), but unfortunately, it doesn't expose a way to allow us specifying it outside.

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Sachelis Saturday, May 16, 2009 12:22 PM
    Wednesday, May 13, 2009 10:46 AM

All replies

  • Hello,

    1. For checking the checkbox, we could use CCmdUI::SetCheck() on the OnUpdateRibbonXXX() handler, i.e.
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
        ON_COMMAND(ID_RIBBON_BTN_XXX, OnRibbonBtnXXX)
        ON_UPDATE_COMMAND_UI(ID_RIBBON_BTN_XXX, OnUpdateRibbonBtnXXX)
    END_MESSAGE_MAP()
    
    void CMainFrame::OnRibbonBtnXXX() 
    {
        m_bCheckBoxXXX = !m_bCheckBoxXXX;
    }
    
    void CMainFrame::OnUpdateRibbonBtnXXX(CCmdUI* pCmdUI) 
    {
        pCmdUI->SetCheck (m_bCheckBoxXXX);	
    }
    2. For the button pressed state, I'm sorry that there is no a method for it currently as I know,  the corresponding variable m_bIsPressed of the class CMFCRibbonBaseElement(this is the base class of CMFCRibbonButton) is protected, since the class CMFCRibbonBar is a friend class of the class CMFCRibbonBaseElement, so it can handle the button state in its code(OnLButtonDown, OnLButtonUp), but unfortunately, it doesn't expose a way to allow us specifying it outside.

    Sincerely,
    Wesley
    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Sachelis Saturday, May 16, 2009 12:22 PM
    Wednesday, May 13, 2009 10:46 AM
  • Thanks Wesley. The ON_UPDATE_COMMAND_UI message does appear to be the only way. That works well in most cases, but there are certainly cases when it'd be nice to change the checkbox state based on other events. Oh well. Note that calling SetCheck() for a button does make the button appear pressed (i.e., a button can be treated as a checkbox).

    Steve

    Saturday, May 16, 2009 12:25 PM