I have the grayed button set for the resource itself. In my CMainFrame class, I
use GetMenu to get the CMenu and use EnableMenuItem to change the state. If I
check the return value, I see this change as I would expect (that is, subsequent
calls return the value MF_GRAYED that I set).
I've used this at various times, including during a handler for OnInitMenu. I
also have a tool bar associated with it. I'm not clear how the menu and toolbar
are interrelated (first time I've used a toolbar). The toolbar exhibits the
same behavior - I cannot disable the corresponding buttons.
Anyone have any ideas? It can't be this hard!
Tony
P.S. Here's an example of the code I'm trying...
void CMainFrame::OnInitMenu(CMenu* pMenu)
{
CFrameWnd::OnInitMenu(pMenu);
int EnableFlag;
EnableFlag = mDBChanged ? MF_ENABLED : MF_GRAYED;
pMenu->EnableMenuItem( ID_SAVE_DATABASE, EnableFlag | MF_BYCOMMAND );
EnableFlag = mMenuChanged ? MF_ENABLED : MF_GRAYED;
pMenu->EnableMenuItem( ID_SAVE_MENU, EnableFlag | MF_BYCOMMAND );
}
Since it appears you are using a CFrameWnd, which is good, there is a much
easier way to enable or disable commands. In Class Wizard, go to your
CMainFrame class, and find the ID for your particular menu command (e.g.
ID_EDIT_COPY). This is the ID you assigned it in resource editor. Add a
function for the UPDATE_COMMAND_UI message. Within that newly created
function (e.g. CMainFrame::OnUpdateEditCopy()), you will be passed a
variable called pCmdUI, which is of type CCmdUI*. To enable the command,
call Enable(TRUE) on that object, and to disable it, call Enable(FALSE). For
example:
void CMainFrame::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
if( <whatever criteria you want to use here> ) {
pCmdUI->Enable(TRUE);
} else {
pCmdUI->Enable(FALSE);
}
}
This is the preferred method for enabling commands for a couple of reasons:
First, the MFC framework hides a lot of stuff from you, and this is how it
expects you to do this. If you use other methods, the framework can end up
thwarting you actions (as you have now seen). Second, it allows you to tie
toolbar buttons and menu items together, simply by giving them the same
resource ID in the resource editor. That way, if one is enabled, the other
is also enabled.
That's the nuts and bolts of it. If you are actually interested in how this
works, here's a quick explanation:
When the framework encounters "idle" time (which is when there are no more
messages in the message queue), it calls all of the functions which are
defined for the UPDATE_COMMAND_UI message. This has the effect of
continually updating the status of your controls. The default handler is
called for any items you don't provide specific functions for. The default
handler for menu items/toolbar buttons is to enable it IF there is an
available command processor (that is, if you have created the function for
the corresponding COMMAND message, such as OnEditCopy in the above example).
So basically, you were setting the item to "grayed out" when you initialized
the menu, but the framework was updating the controls as soon as it
encountered the first "idle" time, and thus calling the default handler for
the UPDATE_COMMAND_UI message.
For more information on this, try Kruglinski's "Inside Visual C++", recently
renamed "Programming Visual C++" for the new VC++ 6.0.
Good Luck,
Doug Kramer
Tony Drumm wrote in message <368E4394...@ibm.net>...
You need to write an ON_UPDATE_COMMAND_UI handler for your button or menu item. The
handler should look like this
void CMyClass::OnUpdateMyItem(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if( someCondition)
{
pCmdUI->Enable(FALSE);
}
else
{
pCmdUI->Enable(TRUE);
}
}
This handler is called in the idle loop of the application -- you don't need to call
it yourself (not sure you can even). If you have a toolbar button and a menu item
with the same ID then both will be controlled by your handler. There are some other
things you can do in the handler also (see the CCmdUI class).
HTH,
David Wilkinon
===============
rMenu.GetSubMenu(0)->EnableMenuItem(IDM_TERM_ADDNOTE, false);
AND NOT LIKE THAT
rMenu.EnableMenuItem(IDM_TERM_ADDNOTE, false);
But: The second line compiles fine and shows the menu, but always all items
enabled
Hope this helps
Nathan Troxler
Tony Drumm wrote in message <368E4394...@ibm.net>...
Instead of false/true you have of course to set for example
MF_GRAYED/MF_ENABLED. !
The rest works for me, I have no CommandUpdateUI Handler
defined in my FormView!.
Nathan Troxler wrote in message ...