[RESOLVED] CButton and EnableWindow?
This seems really simple. I want to disable a button. A simple google search tells me that I should be passing false to the button's EnableWindow function.
It's not working, and I'm not sure why.
Code snippet:
Code:
// Get the 'save' button
CButton* saveButton = (CButton*)pMainFrame->m_toolBar.GetDlgItem(IDC_SAVE_BUTTON);
saveButton->SetIcon(::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_SAVE_ICON))); // Works
// The save button starts out disabled.
saveButton->EnableWindow(FALSE); // Does not work
// saveButton->ShowWindow(FALSE); // Works
Now, I included a commented out 'showwindow' line in there which works just fine. It removes the button from the display. It's only there for testing purposes. The icon I put on the button with SetIcon also works just fine. I'm missing something dumb, aren't I?
Re: CButton and EnableWindow?
Yes you are. You're doing it wrong. If you want to disable a toolbar button use the CCmdUI mechanism.
Re: CButton and EnableWindow?
You need to handle the CN_UPDATE_COMMAND_UI notification sent with WM_COMMAND. That means you need to add an entry like this to your message map:
Code:
ON_UPDATE_COMMAND_UI(IDC_SAVE_BUTTON, OnUpdateSaveButton)
In your handler you should call Enable(FALSE),
Code:
void CMainFrame::OnUpdateSaveButton(CCmdUI* pCmdUI)
{
pCmdUI->Enable(condition);
}
where condition is a condition that evaluates to BOOL.
Notice that OnUpdateSaveButton will be called (about) twice a second.
Re: CButton and EnableWindow?
Quote:
Originally Posted by
GCDEF
Yes you are. You're doing it wrong. If you want to disable a toolbar button use the CCmdUI mechanism.
It's not a toolbar button, it's a regular CButton in a CDialogBar dialog. The m_toolBar is named that because the function of that CDialogBar is mainly to be a tool bar.
Re: CButton and EnableWindow?
Quote:
Originally Posted by
cilu
You need to handle the CN_UPDATE_COMMAND_UI notification sent with WM_COMMAND.
Well, I tried this out and it definitely works. Thanks!
I'm still confused about why EnableWindow() doesn't work, though. Google for 'disable CButton' and the first five responses all say to use EnableWindow() to enable or disable it. Why does it presumably work for them, and doesn't work here?
Re: CButton and EnableWindow?
A CDialogBar is derived from CControlBar, which is also the base class for CToolBar. That's the way they work.
Re: CButton and EnableWindow?
Quote:
I'm still confused about why EnableWindow() doesn't work, though. Google for 'disable CButton' and the first five responses all say to use EnableWindow() to enable or disable it. Why does it presumably work for them, and doesn't work here?
The effect is because of special 'update UI' mechanism used in document-view framework which overrides native UI controls behavior. Whenever you switch to plain dialog-based app, you find EnableWindow starting to work all of a sudden. :)
Re: CButton and EnableWindow?
Quote:
Originally Posted by
Igor Vartanov
The effect is because of special 'update UI' mechanism used in document-view framework which overrides native UI controls behavior. Whenever you switch to plain dialog-based app, you find EnableWindow starting to work all of a sudden. :)
Not really. It's because that's how control bars work. EnableWindow works on CFormView and dialog controls and other windows in doc/view apps, just not in control bars.
Re: CButton and EnableWindow?
Visual C++ is chock full of these little 'gotchas', isn't it? Thanks for the help, guys.
Re: CButton and EnableWindow?
Quote:
Originally Posted by
Ankheg
Visual C++ is chock full of these little 'gotchas', isn't it? Thanks for the help, guys.
That's not really Visual C++ per se - it's more like MFC.