Help Center

GetItemIDfunction

Retrieves the menu item ID of a menu item located at the specified position in a menu.

Windowsfunctiondoc-orphan
No implementation located. This member is documented, but the source scan found no matching declaration. It is likely declared in a header this scan does not resolve, or provided by a macro.

Syntax

FUNCTION GetItemID (BYVAL hMenu AS HMENU, BYVAL nPos AS LONG) AS UINT

Parameters

NameDescription
hMenuA handle to the menu that contains the item whose identifier is to be retrieved.
nPosThe zero-based relative position of the menu item whose identifier is to be retrieved.

Return value

The return value is the identifier of the menu item located at the specified position in a menu.

Description

Retrieves the menu item ID of a menu item located at the specified position in a menu.

Remarks

When working with Win32 menus, it is easy to assume that menu item positions are global and that GetItemID(hMenu, nPos) will return the identifier of the item located at that absolute index. Unfortunately, this assumption is incorrect and leads to the function consistently returning -1.

The key detail — which is not clearly stated in Microsoft’s documentation — is the following: The Windows API function GetMenuItemID, used by GetItemID, does not accept absolute positions, it only accepts positions relative to the specific submenu you pass as hMenu.

In other words:

  • Each submenu (HMENU) has its own independent list of items.
  • Positions are 0‑based within that submenu only.
  • Passing the main menu handle (hMenu) and a global index will always fail.
  • Passing the wrong submenu handle will also fail.

This explains why calling GetItemID(hMenu, absolutePos) returns -1, even when the item clearly exists.

To retrieve the ID of a menu item, you must:

  • Determine which submenu contains the item.
  • Determine the relative position of the item within that submenu.
  • Pass the correct submenu handle and the relative position to GetItemID.

For example: CMenu.GetItemID(CMenu.GetSubMenu(hMenu, 0), 1)

This works because:

  • GetSubMenu(hMenu, 0) returns the correct submenu handle.
  • 1 is the position relative to that submenu.

Finding the correct submenu and position:

To make this easier, the framework provides: FindItemPos(hMenu, itemID, hMenuFound, itemPos)

This function returns:

  • hMenuFound → the handle of the submenu
  • itemPos → the zero‑based position inside that submenu

Once you have these values, you can safely call: GetItemID(hMenuFound, itemPos)

This is the only combination that Win32 accepts.

Reference

  • Documented in Windows/WIndows Controls/CMenu Class.md
  • Topic: About Menus