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

ID: Q230201


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK), on platform(s):
    • Microsoft Windows versions 95, 98
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000


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 kbNTOS400 kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS95 kbWinOS98
Version : WINDOWS:95,98
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: February 1, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.