HOWTO: Dynamically Change Position of Menu Items on Top-Level Menu Bar

ID: Q230201


The information in this article applies to:


SUMMARY

This article describes how menu item positions on the top-level menu bar can be changed at run time. If the top-level menu items have sub menus they will also be moved with the top-level menu.


MORE INFORMATION

Steps to change the position of menu item:

  1. Get the handle to the menu.


  2. Create and initialize a MENUITEMINFO structure.


  3. Copy the menu item info from the item to be changed into the new MENUITEMINFO structure.


  4. Insert the new item so that it precedes the position specified.


  5. Remove the original item.


  6. Redraw the menu bar.


 
HMENU hMenu;
TCHAR szBuffer[40];
TCHAR buf[50];
MENUITEMINFO MInfo;
int OrigPosition, NewPosition ;

hMenu            = GetMenu(hWnd);
MInfo.cbSize     = sizeof(MENUITEMINFO);
OrigPosition     = 2;
NewPosition      = 4;

MInfo.fMask      = MIIM_DATA | MIIM_ID |MIIM_SUBMENU |MIIM_TYPE; 
MInfo.fType      = MFT_STRING;
MInfo.fState     = MFS_DEFAULT;
MInfo.dwTypeData = szBuffer;
MInfo.cch        = 40; //sizeof(Buffer);

if(GetMenuItemInfo(hMenu, OrigPosition, TRUE, &MInfo))
   {
   if(InsertMenuItem(hMenu, NewPosition, TRUE, &MInfo))  // Insert between 3 and 4
      {
      RemoveMenu(hMenu, OrigPosition, MF_BYPOSITION);
      DrawMenuBar(hWnd);
      }
   } 

Additional query words:


Keywords          : kbMenu kbSDKWin32 kbGrpUser 
Version           : WINDOWS:95,98
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: May 21, 1999