[Solved] Unable To Change Menu Bitmap


My Bad! I forgot to declare hMenu as a static variable. Because of that, I was losing my menu handle. That’s why SetMenuItemInfo() would not work later, in the command code blocks. I knew that it had to be something simple.

Anyway, here are the final bits of code. First, the creation of the Max/Restore buttons:

WINDOWPLACEMENT   wp;
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hWnd, &wp);
if(wp.showCmd==SW_SHOWNORMAL){    mII.wID=ID_MENUBAR_MAX;      mII.hbmpItem=hMBB.max; }
if(wp.showCmd==SW_SHOWMAXIMIZED){ mII.wID=ID_MENUBAR_RESTORE;  mII.hbmpItem=hMBB.restore; }
InsertMenuItem(hMenu, NUMMI+2, TRUE, &mII);

Here is the handling of the commands:

case ID_MENUBAR_MAX:
     SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
     return 0;

case ID_MENUBAR_RESTORE:
     SendMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
     return 0;

Finally, I handled the changing of the menu button by hooking WM_SYSCOMMAND. That way, the button would change, even if the command came through the system menu, or some other system command:

case WM_SYSCOMMAND: {
     auto sysResult = DefWindowProc(hWnd, WM_SYSCOMMAND, wParam, lParam);
     if(wParam==SC_MAXIMIZE) {
         mII.cbSize = sizeof(MENUITEMINFO);
         mII.fMask = MIIM_ID | MIIM_BITMAP;
         mII.wID = ID_MENUBAR_RESTORE;
         mII.hbmpItem = hMBB.restore;
         SetMenuItemInfo(hMenu, NUMMI+2, TRUE, &mII);
         DrawMenuBar(hWnd);
       }
      if(wParam==SC_RESTORE) {
          mII.cbSize = sizeof(MENUITEMINFO);
          mII.fMask = MIIM_ID | MIIM_BITMAP;
          mII.wID = ID_MENUBAR_MAX;
          mII.hbmpItem = hMBB.max;
          SetMenuItemInfo(hMenu, NUMMI+2, TRUE, &mII);
          DrawMenuBar(hWnd);
        }
       return sysResult;
 }

Note: It is important to call DefWindowProc whenever adding anything to system or non-client commands. Use the command title for the message parameter. The typedef auto is used, to handle whatever variable that the function returns.

0

solved Unable To Change Menu Bitmap