Here is something which allows you to add menu items dynamically and link them to the required user input:
#region
#include <guiconstantsex.au3>
#include <windowsconstants.au3>
#include <staticconstants.au3>
#include <guitreeview.au3>
Global $aUserItems[2] = [0, 0]
; Create an array to hold the menu items when created
Global $aMenuItems[1][2] = [[0]]
; Declare ini file
$sIni = "M:ProgramAu3 Scriptsmenu.ini"
; Create GUI
$hGUI = GUICreate("Test", 500, 500)
; Add the static menu
Global $mFileMenu = GUICtrlCreateMenu("&File")
Global $mExitItem = GUICtrlCreateMenuItem("E&xit", $mFileMenu)
Global $mHelpMenu = GUICtrlCreateMenu("&Help")
Global $mAboutItem = GUICtrlCreateMenuItem("&About", $mHelpMenu)
$hButton_Ins = GUICtrlCreateButton("Show User Menus", 10, 10, 120, 30)
$hButton_Rem = GUICtrlCreateButton("Remove User Menus", 10, 50, 120, 30)
$hButton_Add = GUICtrlCreateButton("Add User Menu Item", 10, 90, 120, 30)
GUISetState()
#endregion
While 1
; Get the control ID
$iMsg = GUIGetMsg()
Switch $iMsg
Case $GUI_EVENT_CLOSE, $mExitItem
Exit
Case $hButton_Ins
; Get the first and last ControlID created from the user menus
$aUserItems = _Fill
_Menu
(0, "UserMenu")
Case $hButton_Rem
; Delete all the user menus
For $i = $aUserItems[0] To $aUserItems[1]
GUICtrlDelete($i)
Next
Case $hButton_Add
_Add
_User
_Items
("UserMenu")
Case $mAboutItem
MsgBox(0, "Hi", "Hope you find this interesting")
Case Else
; Loop through the array to see if it is a menuitem controlID
For $i = 1 To $aMenuItems[0][0]
If $aMenuItems[$i][0] = $iMsg Then
; It is!
MsgBox(0, "Menu", "You clicked: " & @CRLF & $aMenuItems[$i][1] & @CRLF & @CRLF & _
"Which means we will run: " & @CRLF & IniRead($sIni, "Links", StringReplace($aMenuItems[$i][1], " ", "_"), "Error"))
; No point in looking further
ExitLoop
EndIf
Next
EndSwitch
WEnd
Func _Fill
_Menu
($hCID, $sText)
Local $aCID[2]
; This si the first ControlID we create
$aCID[0] = GUICtrlCreateDummy()
; Read next level down
$aMenu = IniReadSection($sIni, $sText)
If Not @error Then
; Now either create a further submenu or a menuitem
For $i = 1 To $aMenu[0][0]
If StringInStr($aMenu[$i][0], "Item") Then
; Increase the count of menuitems
$aMenuItems[0][0] += 1
; Increase the array size by adding a new element
ReDim $aMenuItems[$aMenuItems[0][0] + 1][2]
; Add the ControlID and text to the newly created array element
$aMenuItems[$aMenuItems[0][0]][0] = GUICtrlCreateMenuItem(StringReplace($aMenu[$i][1], "_", " "), $hCID)
$aMenuItems[$aMenuItems[0][0]][1] = StringReplace($aMenu[$i][1], "_", " ")
Else
; Create menu
If $hCID Then
; It is a submenu
$aMenu[$i][0] = GUICtrlCreateMenu(StringReplace($aMenu[$i][1], "_", " "), $hCID)
Else
; It is a top level menu
$aMenu[$i][0] = GUICtrlCreateMenu(StringReplace($aMenu[$i][1], "_", " "))
EndIf
; Fill menu - note this is a recursive call and we need to take care
_Fill
_Menu
($aMenu[$i][0], $aMenu[$i][1])
EndIf
Next
EndIf
; And this is the last ControlID we create
$aCID[1] = GUICtrlCreateDummy()
Return $aCID
EndFunc ;==>_Fill_Menu
Func _Add
_User
_Items
($sText)
; Create child GUI
$hGUI_Add = GUICreate("Add Items", 400, 400, 50, -50, -1, $WS_EX_MDICHILD, $hGUI)
; Create a treeview
GUICtrlCreateLabel("", 10, 10, 200, 380, $SS_BLACKFRAME)
GUICtrlSetState(-1, $GUI_DISABLE)
$hTreeView = GUICtrlCreateTreeView(20, 20, 180, 380);, $WS_BORDER)
$hTV_Item = $hTreeView
; Fill the treeview with the menu structure
_Fill
_TV
($hTreeView, $sText)
; Create other controls
GUICtrlCreateLabel("Enter menu item text", 220, 10, 160, 20)
$hInput_Item = GUICtrlCreateInput("", 220, 40, 160, 20)
GUICtrlCreateLabel("Enter link", 220, 80, 160, 20)
$hInput_Link = GUICtrlCreateInput("", 220, 100, 160, 20)
$hButton_Addition = GUICtrlCreateButton("Add to menu", 310, 150, 80, 30)
GUISetState()
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hButton_Addition
; Do we have user input
If GUICtrlRead($hInput_Item) <> "" And GUICtrlRead($hInput_Link) <> "" Then
; Read the selected menu from teh treeview
$sSection = StringReplace(_GUICtrlTreeView_GetText($hTreeView, GUICtrlRead($hTreeView)), " ", "_")
; Get the item count
$aItems = IniReadSection($sIni, $sSection)
$iIndex = $aItems[0][0] + 1
; Write the new item
$sItem = StringReplace(GUICtrlRead($hInput_Item), " ", "_")
IniWrite($sIni, $sSection, "Item" & $iIndex, $sItem)
; Write the link
IniWrite($sIni, "Links", $sItem, GUICtrlRead($hInput_Link))
ExitLoop
EndIf
EndSwitch
WEnd
; Delete GUI
GUIDelete($hGUI_Add)
; Delete all the user menus
For $i = $aUserItems[0] To $aUserItems[1]
GUICtrlDelete($i)
Next
; Reset menu
$aUserItems = _Fill
_Menu
(0, "UserMenu")
EndFunc ;==>_Add_User_Items
Func _Fill
_TV
($hTV_Item, $sText)
; Read next level down
$aMenu = IniReadSection($sIni, $sText)
If Not @error Then
; Now run through menu structure
For $i = 1 To $aMenu[0][0]
Switch $aMenu[$i][0]
Case "Menu"
; Create TreeViewItem
$hTV_NewItem = GUICtrlCreateTreeViewItem(StringReplace($aMenu[$i][1], "_", " "), $hTV_Item)
; Fill TV - note this is a recursive call and we need to take care
_Fill
_TV
($hTV_NewItem, $aMenu[$i][1])
EndSwitch
Next
EndIf
EndFunc ;==>_Fill_TV
Still a bit rough and ready, but it shows the principle.
[UserMenu]
Menu=Menu_1
Menu=Menu_2
[Menu_1]
Menu=SubMenu_11
Menu=SubMenu_12
[SubMenu_11]
Item1=Item_111
Item2=Item_112
Item3=Item_113
[SubMenu_12]
Item1=Item_121
Item2=Item_122
[Menu_2]
Item1=Item_21
Item2=Item_22
[Links]
Item_111=Link 111
Item_112=Link 112
Item_113=Link 113
Item_121=Link 121
Item_122=Link 122
Item_21=Link 21
Item_22=Link 22
Note how I have added numbers to the section keys to prevent overwriting them.
Let me know what you think.