Sign in to follow this  
Followers 0
taietel

Menu from INI

3 posts in this topic

#1 ·  Posted (edited)

I'm trying to make a GUI with a dinamic menu, from an ini file. The structure of the ini is something like this:

[Menu1]

Submenu11=cmd11

Submenu12=cmd12

[Menu2]

Submenu21=cmd21

[Menu3]

Submeniu31=cmd31

Submeniu32=cmd32

Submeniu33=cmd33

[Menu4]

Submenu41=cmd41

Submenu42=cmd42

The problem is that I can not get the values. I have read a post (http://www.autoitscript.com/forum/index.php?showtopic=106850&view=findpost&p=753987) from PsaltyDS, but I'm stucked...

The code so far:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
$sIni = @ScriptDir & "\menu.ini"
$hForm = GUICreate("Sample INI menu", 633, 452, 192, 124)
$readmenu = IniReadSectionNames($sIni) ;read section names to array
For $i = 1 To $readmenu[0]
    $mnu = GUICtrlCreateMenu($readmenu[$i]) ;create menu from section names
    $var = IniReadSection($sIni, $readmenu[$i])
    For $j = 1 To UBound($var) - 1
        $var[$j][0]=GUICtrlCreateMenuItem($var[$j][0], $mnu); create submenus
        GUICtrlSetOnEvent(-1, "_MenuItemHit")
        ;the values are in the $var[$j][1]
    Next
Next
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func _MenuItemHit()
    Local $ctrlMenu = @GUI_CtrlId
    Local $hMenu = @GUI_CtrlHandle
    Local $iIndex, $sText
    For $iIndex = 1 To UBound($var) - 1
        If $var[$iIndex][0] = $ctrlMenu Then
            $sText = $var[$iIndex][1]
            ExitLoop
        EndIf
        If $sText = "" Then $sText = "<Not Found>"
    Next
    ConsoleWrite("_MenuItemHit():  ID = " & $ctrlMenu & "; Handle = " & $hMenu & "; Text = " & $sText & @LF)
EndFunc

Func _Close()
    Exit
EndFunc

If I try with Menu4, for example, it triggers the cmd41 and cmd42, but not for the other submenus.

Please point me in the right direction. Thank you in advance!

Edited by taietel

Share this post


Link to post
Share on other sites



The problem is that you are re-using the $var array for each section. The $var from the first section is over-written by the second section... etc. Only the data for the last section is left in the array by the time the GUI is shown. You need to collect them all into one array for searching when the _MenuItemHit() event runs.

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thank you Psalty! I realized that after reading your post   :idea:.

I try to make a launcher for programs I use most and if I update manually the ini file, the menu will also update.

I finally got it to work (the script it's kind of messy...    Posted Image ):

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>

Opt("GUIOnEventMode", 1)
$sIni = @ScriptDir & "\menu.ini"
$hForm = GUICreate("Sample INI menu", 633, 452, 192, 124)
Global $readmenu = IniReadSectionNames($sIni) ;read section names to array
Global $vr[100][100]
Global $sname, $vname
For $i = 1 To UBound($readmenu) - 1
    $mnu = GUICtrlCreateMenu($readmenu[$i]) ;create menu from section names
    $var = IniReadSection($sIni, $readmenu[$i])
    For $j = 1 To UBound($var) - 1
              $vr[$j][0] = GUICtrlCreateMenuItem($var[$j][0], $mnu); create submenus
           $vr[$j][1] = $var[$j][1]
           $sname &= $vr[$j][0] & "|"
           $vname &= $vr[$j][1] & "|"
           GUICtrlSetOnEvent($vr[$j][0], "_MenuItemHit")
    Next
Next


GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")

GUISetState(@SW_SHOW)

While 1
   Sleep(10)
WEnd

Func _MenuItemHit()
      Local $ctrlMenu = @GUI_CtrlId
      Local $iIndex, $sText
    Local $snm = StringSplit($sname, "|")
    Local $vnm = StringSplit($vname, "|")
    For $i = 1 To UBound($readmenu) - 1
            For $iIndex = 1 To UBound($vr) - 1
                    If $ctrlMenu = $snm[$iIndex] Then
                             $sText = $vnm[$iIndex]
                             ExitLoop
                    EndIf
                    ;If $sText = "" Then $sText = "<Not Found>"
            Next
    Next
      ConsoleWrite("Menu ID = " & $ctrlMenu & " command to execute = " & $sText & @LF)

    ;If $sText ="" Then 

    ;   ShellExecute($sText) ;this way it can open executables, documents...

    ;else

    ;MsgBox(0,"Error:", "Nothing to run." & @CRLF & "Check your INI!")

    ;EndIf
EndFunc

Func _Close()
     Exit
EndFunc

I know there is an easy way to do that but I can not figure it right now...

Edited by taietel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0