How to disable a menu using SetMenuItemInfo()?
To disable a menu using SetMenuItemInfo(), you can follow these steps:
- Obtain the menu handle by using the GetMenu() function. If it is a submenu, obtain the submenu handle using the GetSubMenu() function.
- To obtain information about a menu item: use the GetMenuItemInfo() function to retrieve details about a specific menu item. You will need to input the menu handle, the index of the menu item, and a MENUITEMINFO structure.
- Change the information of menu items: set the fState field of the obtained menu item information struct to MF_DISABLED or MF_GRAYED, indicating that the menu item is disabled or grayed out.
- To set the information of a menu item: Use the SetMenuItemInfo() function to update the menu item information and set it back into the menu item. You need to pass in the menu handle, the index of the menu item, and the modified MENUITEMINFO structure.
Here is an example code:
// 获取菜单句柄
HMENU hMenu = GetMenu(hWnd);
// 获取菜单项的信息
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_STATE;
GetMenuItemInfo(hMenu, ID_MENU_ITEM, FALSE, &mii);
// 修改菜单项的信息
mii.fState |= MF_DISABLED;
// 设置菜单项的信息
SetMenuItemInfo(hMenu, ID_MENU_ITEM, FALSE, &mii);
Where hWnd is the handle of the window, and ID_MENU_ITEM is the ID of the menu item to be disabled. Please replace these parameters according to the actual situation.