-
September 20th, 2010, 03:28 PM
#1
[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?
-
September 20th, 2010, 03:42 PM
#2
Re: CButton and EnableWindow?
Yes you are. You're doing it wrong. If you want to disable a toolbar button use the CCmdUI mechanism.
-
September 21st, 2010, 02:12 AM
#3
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.
-
September 21st, 2010, 07:08 AM
#4
Re: CButton and EnableWindow?
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.
-
September 21st, 2010, 07:17 AM
#5
Re: CButton and EnableWindow?
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?
-
September 21st, 2010, 07:24 AM
#6
Re: CButton and EnableWindow?
A CDialogBar is derived from CControlBar, which is also the base class for CToolBar. That's the way they work.
-
September 21st, 2010, 07:31 AM
#7
Re: CButton and EnableWindow?
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.
Best regards,
Igor
-
September 21st, 2010, 07:50 AM
#8
Re: CButton and EnableWindow?
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.
-
September 21st, 2010, 08:01 AM
#9
Re: CButton and EnableWindow?
Visual C++ is chock full of these little 'gotchas', isn't it? Thanks for the help, guys.
-
September 21st, 2010, 05:08 PM
#10
Re: CButton and EnableWindow?
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|