I have the following problem: MFC is disabling my toolbar (a CToolbar) controls if I don't have a message map entry for it's corresponding message (let's say ID_MYBUTTON1). Is there a way around this? I had the same problems with the menu but I found that you could disable the auto disabling by setting CFrameWnd::m_bAutoMenuEnable to false but I cannot find a similar member for CToolbar.

I guess I could add handlers redirecting to an empty function but it would be nice if I could just stop this behavior without using "tricks".


+1  A: 

Add a *ON_UPDATE_COMMAND_UI* handler for each of the controls in your toolbar. Something like this:


void myToolBar::uiButtonHandler(CCmdUI* pCmdUI) 
    pCmdUI->Enable(TRUE); // Or whatever logic you want.

For details read the appropriate section in the MSDN.

Well I would like not to have to add an handlers for each of my controls, else I would do like I said in my question and provide an empty handler for each of them. Still, thanks for your answer. I found a way and only have to override 1 function but it don't work totally right now. I'll post what I did and update it when I get it to work totally.
+1  A: 

Well like I said in reply to zdan's answer I found a way. Just override the OnUpdateCmdUI function in CToolBar like this

class MyToolBar : public CToolBar
    virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
    { return CToolBar::OnUpdateCmdUI(pTarget, FALSE);}

the bDisableIfNoHndler flag is the one responsible for telling the toolbar to disable buttons if no handlers is found so I just force it to FALSE.

Though now I'm having another problem. The toolbar seem fines but it do not send the commands when I press a button. I'm not sure why because if I access the same commands from the menu it works fine. I'll try to see if this is related.

Thanks for your helps.

Update: Found my problem. Basically the problem was that my handlers to my commands were in MyFrame::PreTranslateMessage (after doing like suggested in this question's answer : http://stackoverflow.com/questions/978071/how-to-redirect-mfc-messages-to-another-object) but the commands were not sent through this function (though when accessed from the menu they did). They were sent through MyFrame::OnCommand though so I just changed the code from PreTranslateMessage to OnCommand and now everything works fine. I don't know MFC enough to know why this was the case but now everything seems to works so thanks for the help everyone.