Enable/Disable MenuItem

This topic seems to have cropped up regularly over a decade or more. I have ploughed through the exchanges in all the forums, yet I still cannot make it work. The following is a tiny Code::Blocks program, written to isolate the
problem:
// resource.h
#define IDR_MYMENU	101
#define ID_FILE     	102
#define ID_DISABLE	103
#define ID_ENABLE       104
#define ID_MESSAGE	105
#define ID_EXIT		106


// resource.rc
#include <windows.h>
#include "resource.h"
IDR_MYMENU MENU
{
    POPUP "File"
    {
        MENUITEM "M1 Disable M3",	ID_DISABLE
        MENUITEM "M2 Enable M3",        ID_ENABLE
        MENUITEM "M3 MesageBox",	ID_MESSAGE
        MENUITEM "M4 Exit",	        ID_EXIT

    }
}


// main.cpp
... minimal code down to the Switch instruction of case WM_COMMAND, and then ...
   case ID_DISABLE:
	EnableMenuItem((HMENU)IDR_MYMENU,ID_MESSAGE,MF_GRAYED);
   break;


case ID_ENABLE is similar to case ID_DISABLE above, and neither works. case ID_MESSAGE and case ID_EXIT both works as expected.

I have tinkered with EnableMenuItem(), as various posts have suggested, but without success. Can anybody help?

Peter
(HMENU)IDR_MYMENU

This is wrong.

An ID is not an HMENU. Forcibly casting to one won't work (don't cast around compiler errors! The compiler is telling you something is wrong... casting around it just tells the compiler to shut up without actually solving the problem)

The easiest way to get an HMENU is probably with GetMenu:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms647640%28v=vs.85%29.aspx

For that you'd need the HWND of the parent window:

1
2
HMENU menu = GetMenu( my_hwnd );
EnableMenuItem( menu, ID_MESSAGE, MF_GRAYED );


Last edited on
Disch
Thanks for the prompt reply. I did try:

HMENU hmenu = GetMenu(hwnd);
EnableMenuItem( hmenu, ID_MESSAGE, MF_GRAYED );

No joy :(
Peter
I think MF_GRAYED is deprecated, try MF_DISABLED
Thanks L B, but MF_DISABLED instead of MF_GRAYED stop the window appearing. (I am sure I remember reading that GRAYED is preferred now. Maybe that's wrong.)
Peter
Ah, I don't know, all I know is that it was either MF_GRAYED or MF_DISABLED that had a comment in the windows headers saying to use the other one. I remember having an issue similar to this but I can't find where the code is.
So where are all the C++ pros? Come on guys. Disable/Enable Menu Item is surely bread and butter to you fellas. What's wrong with:
HMENU hmenu = GetMenu(hwnd);
EnableMenuItem(hmenu, ID_MESSAGE, MF_GRAYED );

or have I gone wrong somewhere else? Please help.
Peter
This is Windows-specific stuff. Many of us work with cross-platform toolkits rather than platform-specific GUI code.

I'd recommend using either the Windows Programming forum here, or a forum more focussed on Windows development elsewhere
OK Mike. I'll try that. Thanks,
Peter
Topic archived. No new replies allowed.