How to stop MFC from disabling my controls if I do

2019-05-04 17:24发布

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

标签: c++ mfc
2条回答
Bombasti
2楼-- · 2019-05-04 17:46

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.

查看更多
迷人小祖宗
3楼-- · 2019-05-04 18:00

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.

查看更多
登录 后发表回答