1

I am working in MFC VC++ Visual Studio 2010 professional edition.

I would like to disable/enable submenu icon .

I used the function named SetMenuItemBitmaps() used for adding icon.

CMenu* pMenu1 = GetMenu();
CMenu* pSubMenu = pMenu1->GetSubMenu(1);
if(pSubMenu != NULL)
{
pSubMenu ->SetMenuItemBitmaps(ID_NEW, MF_BYCOMMAND, ConvertIconToBitmap(hIcon) , ConvertIconToBitmap(hIcon1));
}

I would like to display hIcon1 as disable icon image, when Submenu Item named ID_NEW is disabled and hIcon for displaying icon when the Submenu Item named ID_NEW is enabled. But it is only loading the hIcon only.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
VickyCool
  • 113
  • 1
  • 12
  • 1
    The `SetMenuItemBitmaps()` documentation clearly mentions that the two custom bitmaps are for the checked and unchecked states of the menu item, not enabled or disabled. Besides, MFC offers a very powerful and well-conceived mechanism for enabling/disabling menu items and toolbar buttons, those `ON_UPDATE_COMMAND_UI` handlers. So I would suggest using these ones. More details in [this](https://stackoverflow.com/questions/40864274/mfc-menu-item-remains-grayed/40873304#40873304) post. You need to specify only one bitmap. – Constantine Georgiou Apr 11 '20 at 23:04
  • Using ON_UPDATE_COMMAND_UI, we can easily enable/disable menu items,I know that by using pCmdUI->Enable(bEnableState) , But I would like to show grey icon In front of particular menu item when the said item is disabled and normal icon when the said item is enabled. How to achieve the same behaviour?? – VickyCool Apr 12 '20 at 11:31
  • `ON_UPDATE_COMMAND_UI` framework only works if you are using a SDI / MDI project. It does not work by default for a menu structure added to a Dialog project. – Andrew Truckle Apr 12 '20 at 16:15
  • Why does you prepare two versions of your icons - one for enabled and one for disabled. At the moment you detect a menu item has been disabled update the associated menu item bitmap to your grayed version of it. – Andrew Truckle Apr 12 '20 at 16:16
  • Does this answer your question? [Difficulty setting menu item bitmaps from CImageList](https://stackoverflow.com/questions/52353745/difficulty-setting-menu-item-bitmaps-from-cimagelist) – Andrew Truckle Apr 12 '20 at 16:25
  • It definitely works for wizard-generated MFC projects with a toolbar. In this case you don't need to specify a disabled version of the icon. Don't know what happens in the case of a dialog-based MFC application though. Don't know if this is a WinAPI or MFC-specific feature either. Maybe check the MFC source code to find out? – Constantine Georgiou Apr 12 '20 at 18:17
  • @ConstantineGeorgiou I went through this years ago. There is a chunk of the MFC code that dialog appls don't use, and that handler is one of them. You would have to add all the missing code in for it to work. No one when it is very simple to toggle a menu item at will as per my linked answer to my own question / answer. – Andrew Truckle Apr 12 '20 at 19:55
  • Andrew:- I found a better option i.e CImageList::ExtractIcon() for extracting the icon handle , which I convert in to BIMAP handle , that can be used in SetMenuItemBitmaps() once for enable icon and disable icon separately , thanks to all for your support – VickyCool Apr 15 '20 at 12:57

0 Answers0