Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

enable/disable menu item

219 views
Skip to first unread message

Check Abdoul

unread,
Aug 28, 2000, 3:00:00 AM8/28/00
to

Hi...

Handle UPDATE_COMMAND_UI for the menu item and

call
pCmdUI->Enable(FALSE);

Cheers
Check Abdoul
-----------------


"Gerhard" <ger...@ginko.de> wrote in message
news:39aa...@news.ginko.net...
> Hi,
>
> I have another problem in MFC.
> My goal is that one of the menus is disabled at startup and gets enabled
> after the initialization (which takes a while) is complete.
> To disable I checked "gray" in the Ressource Editor, now I'd like to
enable
> at the end of CWinApp::InitInstance().
>
> I do this with the following two simple code lines:
>
> CMenu *menu = m_pMainWnd->GetMenu();
> menu->EnableMenuItem(3, MF_ENABLED);
>
> I hope I got the flag right, I don't have any code or doc at my disposal
at
> the
> moment...
>
> This code does simply nothing. What's wrong?
> Thank you very much again
>
> Gerhard
>
>
>
>

Jeff Partch

unread,
Aug 28, 2000, 3:00:00 AM8/28/00
to
Hi, Gerhard!

Not sure, but try adding an MF_BYPOSITION (or MF_BYCOMMAND if that's what it is) to your
EnableMenuItem call. Like so...

menu->EnableMenuItem(3, MF_ENABLED|MF_BYPOSITION);

If that doesn't fix it post a follow-up, and I'll really get my hands dirty if that's what it takes.

HTH,

Jeff...

--
Please post all follow-ups to the newsgroup only.

Gerhard wrote in message <39aa...@news.ginko.net>...

Gerhard

unread,
Aug 28, 2000, 6:32:44 PM8/28/00
to

Gerhard Prilmeier

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to
Hello Jeff!

> Not sure, but try adding an MF_BYPOSITION (or MF_BYCOMMAND if that's what it is) to your
> EnableMenuItem call. Like so...
>
> menu->EnableMenuItem(3, MF_ENABLED|MF_BYPOSITION);

Thank you, MF_BYPOSITION was indeed needed. Now the command basically works, the button is enabled but
remains grayed until clicking once on it. How to fix this?

I also tried the other way, using the UPDATE_COMMAND_UI message. Unfortunately this is called directly
after the app has been started, which is not my intention. Moreover this msg doesn't seem to work at all
for pop-up menus.

Thanks a lot!

Gerhard


Jeff Partch

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to
Gerhard,

This is a top-level item on the apps main menu right? Try something like this (from the end of a
standard MFC InitInstance)...

BOOL CNg81500App::InitInstance()
{
...

// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

CMenu* pMenu = pMainFrame->GetMenu();
if (pMenu)
{
pMenu->EnableMenuItem(3, MF_ENABLED|MF_BYPOSITION);
pMainFrame->DrawMenuBar();
}

return TRUE;
}

Jeff...

--
Please post all follow-ups to the newsgroup only.

Gerhard Prilmeier wrote in message <39AB7C87...@ops.de>...

Gerhard Prilmeier

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
Hi Jeff!

>
> pMainFrame->DrawMenuBar();

Yeah that's it! Thanks

Bye
Gerhard


Gerhard Prilmeier

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
It's almost ridiculous that I have to post again but I'm already stuck at the next thing.
You were right, the first menu item we talked about was part of the main menu bar. It has a submenu. In
this submenu I have two menu items "Start" and "Stop". I'd like to disable start and enable stop after
clicking start and the other way round.
How to disable (or gray) menu items at runtime?
My approach was the following:
I retrieved a pointer using GetMenu() etc., then I called
menu->EnableMenuItem(0, MF_GRAYED | MF_BYPOSITION);

The result was exactly nothing.

I also tried menu->ModifyMenu() which simply removed the menu item instead of graying it.
I also tried to remove the menu and re-insert it with the grayed flag. The result was the same as with
ModifyMenu().

Of course I used DrawMenuBar() as well.

If you are interested I'll send you my code, just tell me.

Thank you very much

Gerhard


Jim Ross

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to

Use ON_UPDATE_COMMAND_UI for the menu items you want to control. You can use
ClassWhiz to set these up for you.


Gerhard Prilmeier <gerhard....@ops.de> wrote:

Jim [VC/MFC MVP]
To send mail, change spam-me-not to msn

Bob Moore [MVP]

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
On Wed, 30 Aug 2000 15:42:57 +0200, Gerhard Prilmeier wrote:

>I retrieved a pointer using GetMenu() etc., then I called
>menu->EnableMenuItem(0, MF_GRAYED | MF_BYPOSITION);
>
>The result was exactly nothing.

I think a little explanation is required here. Jim is of course right,
you need to use the UPDATE_COMMAND_UI mechanism. The issue is that
your call to EnableMenuItem _IS_ working, but as soon as MFC processes
the WM_INITPOPUPMENU message, its default behaviour is to enable any
menu item which has a handler, so all your good work is undone. You
need to go with the flow and use the MFC command ui updating
mechanism.

Oh, and DrawMenuBar only affects bar menus, hence the name.

--
Bob Moore [MVP]
http://www.mooremvp.freeserve.co.uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Due to an unreasonable amount of queries, I no
longer answer unsolicited email questions. Sorry,
no exceptions.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Gerhard Prilmeier

unread,
Sep 1, 2000, 10:55:56 AM9/1/00
to

Thank you, everything fine now!

Bye
Gerhard

0 new messages