Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I would like to be able to "customize" my toolbar depending on what type of child frame becomes active in my MDI application. I have been using CMFCToolbar because they look nice and Visual Studio steers you in that direction.

I could not determine how to do it automatically (like the menus change when the document type changes) so I tried creating independent toolbars for each document type - and switching them in OnMDIActivate() - but that caused too many hassles with CMFCToolbar, including:
- toolbars the wrong size
- toolbars not showing up
- reload app and the toolbar is in the wrong form (docked/undocked)even though I disabled LoadState/SaveState
- gaps where the toolbar used to be - sometimes the image is still there but the toolbar is not (no repaint of the frame)

.. so I gave up on that. [<flame>IMHO it is shockingly hard to make customized use of CMFCToolbar viz its lack of predictable behavior, ease of reading the code, documentation, etc.]

What I would like to do now is simply use *one* toolbar but gray-out certain buttons for certain document types, something like:

void CChildFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd)
    CMDIChildWndEx::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
    // which Doc type are we now working with?
    if (bActivate) {

        CView *theView = ((CChildFrame *)pActivateWnd)->GetActiveView();
        if (theView == NULL) return;

        CMainFrame *MF = (CMainFrame *)theApp.m_pMainWnd;
        CMFCToolBar *TB = &MF->m_wndToolBar;
        if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenView))) {
            // disable certain toolbar buttons
        else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenListingView))) {

            // disable certain toolbar buttons
        else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenStaffView))) {
            // disable certain toolbar buttons
            //TB->GetToolBarCtrl().EnableButton(ID_FILE_OPEN, FALSE);
        else if (theView->IsKindOf ( RUNTIME_CLASS (CDrumGenLogView))) {
            // disable certain toolbar buttons

As you can see, I have a EnableButton(FALSE) call in there ... but wait, that doesn't exist any more in CMFCToolbar.

Can anyone point me to a super simple way to disable a CMFCToolbar button?


Venkat and Anna,

Thanks for the replies. The following code seems to work with one caveat mentioned below (I decided to stick with CMFCToolBar):

void CMyToolBar::EnableButton (int iCmd, BOOL bStatus)
    // iCmd is not the index, but the command ID - this way the
    // buttons can be re-arranged and this routine still works
    int idx = CommandToIndex (iCmd);
    if (idx == -1) return;  // not in this toolbar
    UINT iStyle = GetButtonStyle (idx);
    CMFCToolBarButton *Button = GetButton (idx);
    if (bStatus) { // enable
        SetButtonStyle (idx, iStyle & !TBBS_DISABLED);
    else {
        SetButtonStyle (idx, iStyle | TBBS_DISABLED);
    Button->EnableWindow (bStatus);
void CMyToolBar::OnUpdateCmdUI(CFrameWnd *pFrame, BOOL bDisableIfNoHndler)
    CBasePane::OnUpdateCmdUI(pFrame, bDisableIfNoHndler);
    CMainFrame *MF = (CMainFrame *)pFrame;
    for (int i=0; i<NUMTOOLBUTTONS; i++)
        EnableButton (MF->m_ToolBarStatus[i].iCmd, MF->m_ToolBarStatus[i].bEnabled);

I keep the enabled/disabled status of each button in ToolBarStatus[]. The buttons are Grayed out correctly now .. however they are still active (at least some of them); in other words, the EnableWindow() call does not seem to have any effect. Further thoughts?

Share this answer
Check this with TBBS_DISABLED style,
CMFCToolBar::SetButtonStyle.But this will use zero based button index.
Share this answer
Albertino 12-Aug-11 5:09am    
You can get the index using this:

int b_id = m_wndToolBar.CommandToIndex(ID_MY_BUTTON);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900