How to access a control on a ribbon tab that has not been opened yet?

166 Views Asked by At

I have a ribbon application that has three tabs. On the OnCreate event, I need to check a CheckBox that is not on the tab that is shown when the application opens.

The problem is I can't check that CheckBox. When I open the tab that contains it, the CheckBox is unchecked. What I know is that, because the ribbon is created dynamically, the control I want to access doesn't exist yet.

There is some way to access the CheckBox?

2

There are 2 best solutions below

0
On

Try checking the control after its creation, (eg) the event where you create the control. Once you create it, you can assign the procedure that will be triggered when clicked and will be checked or not. Hope this helps.

0
On

In WTL it's quite simple:

  1. You should override OnRibbonQueryState():

     bool CMainFrame::OnRibbonQueryState(UINT nCmdID, REFPROPERTYKEY key)
     {
       switch (nCmdID)
       {
       case RID_SETTINGS_GUI_SHOWSTATUSBAR:
        if (IsEqualGUID(key.fmtid, UI_PKEY_BooleanValue.fmtid))
            return m_showStatusBarCtrl.IsChecked();
        }
    
        return DefRibbonQueryState(nCmdID, key); //Default WTL behavior
    }
    
  2. Add your handler:

    COMMAND_ID_HANDLER(RID_SETTINGS_GUI_SHOWSTATUSBAR, OnShowStatusbarChanged)
    
    LRESULT CMainFrame::OnShowStatusbarChanged(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        m_showStatusBarCtrl.OnCheckboxChanged();
    
        bool currState = m_showStatusBarCtrl.IsChecked();
        ::ShowWindow(m_hWndStatusBar, currState ? SW_SHOW : SW_HIDE);
        UpdateLayout();
    
        return 0;
    }
    

    m_showStatusBarCtrl - is a just my simple wrapper on state(bool).