Search code examples
c#winapi

Win32 API - how to insert new popup menu at specified position in the main menu bar?


I have a C# application which uses Win32 API calls to modify main menu bar of another application and insert a new popup menu with a few menu items in it.

I'm using

AppendMenu(menu, MF_POPUP, newMenu, "My New Popup Menu");

to add the new popup menu to the main menu bar.

Now AppendMenu obviously adds it at the end, so it appears after existing "Help" popup. I would like to specify a position for this new popup menu so that appears before "Help".

I've looked at using InsertMenu instead since it allows you to specify the position of an item (and I already use it for adding popup menu items), but it doesn't allow me to pass in the pointer to new popup.

Essentially I need something along the lines of InsertMenu(mainMenuHandle, position, MF_POPUP, newPopupMenuHandle).


Solution

  • According to the Igor Tandetnik's suggestion:

    Refer to the Doc:InsertMenuA function

    [in] uIDNewItem

    Type: UINT_PTR

    The identifier of the new menu item or, if the uFlags parameter has the MF_POPUP flag set, a handle to the drop-down menu or submenu.

    uIDNewItem is used to specify the handle when MF_POPUP is used