Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1792
  • Last Modified:

How to hide/unhide a menu item in win32 programming

Is there is any windows API to hide and unhide a menu item at runtime.
Shantanu
0
Shantanu
Asked:
Shantanu
1 Solution
 
pjknibbsCommented:
If you have a HMENU handle to the menu, you can call EnableMenuItem() to disable or grey out menu options. As far as I know you can't actually completely hide menu options without removing them from the menu--this can also be done at run time, but is a more complex process than EnableMenuItem().
0
 
ShantanuAuthor Commented:
piknibbs, in VB we have visible property for menu bar which do the same but I want the paralle l in win32
0
 
RolandAZCommented:
Shantanu,

//needed variables
 MENUITEMINFO menuiteminfo;
 UINT uItem = 0; //position of menu item to hide
 HMENU hMenuX;
 HMENU hSubMenu;
 int cchsize = 128; //
 ZCHAR sMenuText[256];
 int iSubMenu = 5;
//////////////////////////////////
//to hide menuitem in menubar, submenu 5, menuitem 0
//------
  hMenuX = GetMenu(m_hWnd); //hWnd HWND of window
  hSubMenu = GetSubMenu(hMenuX,iSubMenu);
  menuiteminfo.cbSize = sizeof(menuiteminfo);
   //set mask at your discretion
  menuiteminfo.fMask = MIIM_CHECKMARKS | MIIM_DATA |
    MIIM_ID | MIIM_STATE | MIIM_SUBMENU | MIIM_TYPE ;
  menuiteminfo.cch = 128;
  menuiteminfo.dwTypeData = sMenuText;
  menuiteminfo.fType = MFT_STRING;
 GetMenuItemInfo(hSubMenu, uItem,TRUE,&menuiteminfo);
 RemoveMenu(hSubMenu,uItem, MF_BYPOSITION);
//end -------

//////////////////////////////
//to unhide menuitem
//----------------------
  InsertMenuItem(hSubMenu,uItem,TRUE,&menuiteminfo);
  DrawMenuBar(m_hWnd);
//end ----------------
////////////////////

Key APIs are : GetMenu, GetSubMenu, GetMenuItemInfo,RemoveMenu,InsertMenuItem

Make sure to set menuiteminfo parameters fMask, cbSize,
cch,dwTypeData,and fType. Else you'll get an error.

Copy and paste into your routines and it should work fine.
Hope that gets you started...

Roland
 
 


 
 
     
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now