Menu trouble no MFC
I'm using the Win32 API in c++ for my menu I made for a 2D level editor. When the editor starts up, I want some menu items to be grayed. I can easily do that in my resource (.rc) file by adding GRAYED after the item identifier. However, I want to re-enable some items after a map is created. I've tried using EnableMenuItem() with no success, and SetMenuItemInfo confuses me with its MAPITEMINFO structure. I'm not sure what to change in the structure to enable the menu item. If anyone could help me, I would appreciate it. If you need source code, I will post it.
EnableMenuItem should work, but it should be done every time a popup is opened or accessed. you can go ahead and post relevant code, let's take a look at it... but in general, what i do for my menus in Win32 is similar to what MFC does (similar to how MFC's command enablers work): every time a menu is opened I enable/disable/gray/check/uncheck my menu items through WM_INITMENU.
The Ce* functions are like MFC's command enablers... every time a menu is opened, whether it be from the main menu or a tracked popup, each of these command enabler functions gets called. say in my case i have a project-based app... when a menu is chosen the CeProjectValid enablers are called and the menu items CM_CLOSE_PROJECT and CM_SAVE_PROJECT_AS are appropriately enabled/disabled (depending on if a project is open or not).
LRESULT OnInitMenu(HWND window, WPARAM wparam, LPARAM lparam){ // menu handle HMENU menu = (HMENU)wparam; if(!menu) return DefWindowProc(window, WM_INITMENU, wparam, lparam); // File Menu CeProjectValid(menu, CM_CLOSE_PROJECT); // CM_CLOSE_PROJECT = menu ID CeProjectValid(menu, CM_SAVE_PROJECT_AS);}
void CeProjectValid(HMENU menu, UINT code){ mpProject* ptr = GetProject(); if(ptr->IsValid()) EnableMenuItem(menu, code, MF_ENABLED); else EnableMenuItem(menu, code, MF_GRAYED);}
The Ce* functions are like MFC's command enablers... every time a menu is opened, whether it be from the main menu or a tracked popup, each of these command enabler functions gets called. say in my case i have a project-based app... when a menu is chosen the CeProjectValid enablers are called and the menu items CM_CLOSE_PROJECT and CM_SAVE_PROJECT_AS are appropriately enabled/disabled (depending on if a project is open or not).
Ok, well yadango, I don't really understand all of that OnInitMenu stuff. I just want to use simple Win32 menu functions that can enable or disable the menu items. To gray the menu items prior to execution, I do this
Resource.rc
g_hMenu = handle to my menu
And I was hoping I could use EnableMenuItem(g_hMenu, FILE_CLOSEMAP, MF_ENABLED | MF_BYCOMMAND); or something like that.
Resource.rc
ID_MENU MENUBEGIN POPUP "&File" BEGIN MENUITEM "&New Map\tCtrl-N", FILE_NEWMAP MENUITEM "&Open Map\tCtrl-O", FILE_OPENMAP MENUITEM "&Close Map\tCtrl-W", FILE_CLOSEMAP, GRAYED MENUITEM SEPARATOR MENUITEM "&Save\tCtrl-S", FILE_SAVE, GRAYED MENUITEM "Save &As", FILE_SAVEAS MENUITEM SEPARATOR MENUITEM "E&xit\tAlt-F4", FILE_EXIT END POPUP "&Edit" BEGIN MENUITEM "&Undo\tCtrl-Z", EDIT_UNDO, GRAYED MENUITEM "&Redo\tCtrl-Y", EDIT_REDO, GRAYED MENUITEM SEPARATOR MENUITEM "Cu&t Section\tCtrl-X", EDIT_CUT, GRAYED MENUITEM "&Copy Section\tCtrl-C", EDIT_COPY, GRAYED MENUITEM "&Paste Section\tCtrl-V", EDIT_PASTE, GRAYED MENUITEM "&Delete Section\tDelete", EDIT_DELETE, GRAYED END POPUP "&Map" BEGIN MENUITEM "&Clear", MAP_CLEAR, GRAYED MENUITEM "&Properties", MAP_PROPERTIES, GRAYED END POPUP "&Help" BEGIN MENUITEM "&About", HELP_ABOUT MENUITEM "&Help", HELP_HELP ENDEND
g_hMenu = handle to my menu
And I was hoping I could use EnableMenuItem(g_hMenu, FILE_CLOSEMAP, MF_ENABLED | MF_BYCOMMAND); or something like that.
the answer is yes, you can, but you should do it from WM_INITMENU and WM_INITMENUPOPUP.
in your window procedure, the following below should work.
in your window procedure, the following below should work.
case(WM_INITMENU) : { if(can_close_map) EnableMenuItem((HMENU)wParam, FILE_CLOSEMAP, MF_ENABLED | MF_BYCOMMAND); else EnableMenuItem((HMENU)wParam, FILE_CLOSEMAP, MF_GRAYED | MF_BYCOMMAND); break; }
Yeah, that should work, but I also came up with another solution as I was experimenting. Whenever I use the EnableMenuItem() function outside of WM_INITMENU:, I just do SendMessage(g_hWnd, WM_INITMENU, 0, 0); It seems to work. I'll just use whatever solution works better for easier code management. Thanks yadango.
Edit: Found out that yours looks neater and is easier to manage. I'll use that.
[Edited by - GuitarPlayer0912 on August 17, 2006 4:09:40 PM]
Edit: Found out that yours looks neater and is easier to manage. I'll use that.
[Edited by - GuitarPlayer0912 on August 17, 2006 4:09:40 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement