4

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".

Thanks

n1ckp
  • 1,481
  • 1
  • 14
  • 21

2 Answers2

2

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

ON_UPDATE_COMMAND_UI(ID_MYBUTTON1, uiButtonHandler);

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

For details read the appropriate section in the MSDN.

zdan
  • 28,667
  • 7
  • 60
  • 71
  • 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. – n1ckp Oct 02 '09 at 22:38
  • This applies to CDialogBar based classes too. I had a PUSHBUTTON on my CDialogBar which was always disabled by default. Adding the CCmdUI handler as advised by @zdan above fixed the problem. – osullivj Mar 30 '17 at 14:04
1

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
{
public:
    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 : 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.

Community
  • 1
  • 1
n1ckp
  • 1,481
  • 1
  • 14
  • 21