In the menu handler, how do I get the ID of the Menu item

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

In the menu handler, how do I get the ID of the Menu item

15 Dec 2021, 10:16

Hi,In the menu handler, how do I get the ID of the Menu item

In addition to these three information, how to get the menu ID

Code: Select all

BoundGivePar := Func("GivePar").Bind("First", "Test one")
BoundGivePar2 := Func("GivePar").Bind("Second", "Test two")


Menu MyMenu, Add, Give parameters, % BoundGivePar
Menu MyMenu, Add, Give parameters2, % BoundGivePar2
Menu MyMenu, Show


GivePar(a, b, ItemName, ItemPos, MenuName)
{
    MsgBox % "a:`t`t" a "`n"
           . "b:`t`t" b "`n"
           . "ItemName:`t" ItemName "`n"
           . "ItemPos:`t`t" ItemPos "`n"
           . "MenuName:`t`t" MenuName
}
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: In the menu handler, how do I get the ID of the Menu item

15 Dec 2021, 10:48

If by "menu ID" you mean the menu handle, try this:

Code: Select all

GivePar(a, b, ItemName, ItemPos, MenuName)
{

	MenuID := DllCall( "GetMenuItemID", "ptr", MenuGetHandle( "MyMenu" ), "int", ItemPos-1 )

    MsgBox % "a:`t`t" a "`n"
           . "b:`t`t" b "`n"
           . "ItemName:`t" ItemName "`n"
           . "ItemPos:`t`t" ItemPos "`n"
           . "MenuName:`t`t" MenuName "`n"
           . "Menu ID:`t`t" MenuID
}
Hope this helps :)
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: In the menu handler, how do I get the ID of the Menu item

15 Dec 2021, 11:01

TLM wrote:
15 Dec 2021, 10:48
If by "menu ID" you mean the menu handle, try this:

Code: Select all

GivePar(a, b, ItemName, ItemPos, MenuName)
{

	MenuID := DllCall( "GetMenuItemID", "ptr", MenuGetHandle( "MyMenu" ), "int", ItemPos-1 )

    MsgBox % "a:`t`t" a "`n"
           . "b:`t`t" b "`n"
           . "ItemName:`t" ItemName "`n"
           . "ItemPos:`t`t" ItemPos "`n"
           . "MenuName:`t`t" MenuName "`n"
           . "Menu ID:`t`t" MenuID
}
Hope this helps :)
It's very useful. I modified your script a little bit

Code: Select all

BoundGivePar := Func("GivePar").Bind("First", "Test one")
BoundGivePar2 := Func("GivePar").Bind("Second", "Test two")


Menu MyMenu, Add, Give parameters, % BoundGivePar
Menu MyMenu, Add, Give parameters2, % BoundGivePar2
Menu MyMenu, Show


GivePar(a, b, ItemName, ItemPos, MenuName)
{

    MenuID := DllCall( "GetMenuItemID", "ptr", Menu_GetMenuByName(MenuName), "int", ItemPos-1 )
    msgbox,% MenuID
    MsgBox % "a:`t`t" a "`n"
           . "b:`t`t" b "`n"
           . "ItemName:`t" ItemName "`n"
           . "ItemPos:`t`t" ItemPos "`n"
           . "MenuName:`t`t" MenuName
}


; ======================================================================================================================
; GetMenuByName   Retrieves a handle to the menu specified by its name.
; Return values:  If the function succeeds, the return value is a handle to the menu.
;                 Otherwise, the return value is zero (False).
; Remarks:        Based on MI.ahk by Lexikos -> http://www.autohotkey.com/board/topic/20253-menu-icons-v2/
; ======================================================================================================================
Menu_GetMenuByName(MenuName) {
   Static HMENU := 0
   If !(HMENU) {
      Menu, %A_ThisFunc%Menu, Add
      Menu, %A_ThisFunc%Menu, DeleteAll
      Gui, %A_ThisFunc%GUI:+HwndHGUI
      Gui, %A_ThisFunc%GUI:Menu, %A_ThisFunc%Menu
      HMENU := Menu_GetMenu(HGUI)
      Gui, %A_ThisFunc%GUI:Menu
      Gui, %A_ThisFunc%GUI:Destroy
   }
   If !(HMENU)
      Return 0
   Menu, %A_ThisFunc%Menu, Add, :%MenuName%
   HSUBM := Menu_GetSubMenu(HMENU, 1)
   Menu, %A_ThisFunc%Menu, Delete, :%MenuName%
   Return HSUBM
}
Menu_GetMenu(HWND) {
   ; http://msdn.microsoft.com/en-us/library/ms647640(v=vs.85).aspx
   Return DllCall("User32.dll\GetMenu", "Ptr", HWND, "Ptr")
}
Menu_GetSubMenu(HMENU, ItemPos) {
   ; http://msdn.microsoft.com/en-us/library/ms647984(v=vs.85).aspx
   Return DllCall("User32.dll\GetSubMenu", "Ptr", HMENU, "Int", ItemPos - 1, "Ptr")
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aifritz, Google [Bot], mikeyww and 98 guests