How To Dynamically Change Position of Menu Items on Top-Level Menu Bar (230201)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0

This article was previously published under Q230201

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);
      }
   }
				

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbhowto kbMenu KB230201