Jump to content

Auto Generate Buttons


Recommended Posts

Hello,

 

I wanted to know if it was possible to auto-generate buttons from a ini file. The thought was to read the ini and for each value create a button for it. Now I would like to have the buttons places from left to right then after lets say 5 buttons have created move down to the next row and start again with lets say a limit of 8 rows.

INI example:

[Buttons]
Button1Text=Test 1
Button1Command=http://google.com
Button2Text=Test 2
Button2Command=cmd.exe

Any help would be appreciated 
 

Link to comment
Share on other sites

This example automatically generates buttons based on the "Buttons" section of an "ini" file.

#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
; Referenced  https://www.autoitscript.com/forum/topic/178272-is-this-done-correctly/?do=findComment&comment=1279329

Local $sFilePath = @ScriptDir & "\Test.ini"
Local $sSection = "Buttons"
Local $aArray = IniReadSection($sFilePath, $sSection)
_ArrayDelete($aArray, 0)
;_ArrayDisplay($aArray)

Local $iIndex
Local $iTotButtons = UBound($aArray)
Local $iNumPerRow = 5
Local $iButWidth = 100
Local $iButDepth = 30
Local $aButiD[$iTotButtons]
Local $hGUI = GUICreate('Buttons From "' & $sFilePath & '"', 10 + ($iButWidth + 10) * $iNumPerRow, _
                                                             10 + Ceiling($iTotButtons / $iNumPerRow) * ($iButDepth + 10), -1, $WS_EX_TOPMOST)
For $i = 0 To $iTotButtons - 1
    ; $x = $iXBorder + (($iRectWidth + $iSpacing) * Mod($i, $iNumCols))
    $x = 10 + (($iButWidth + 10) * Mod($i, $iNumPerRow))

    ; $y = $iYBorder + (($iRectDepth + $iSpacing) * Int($i / $iNumCols))
    $y = 10 + (($iButDepth + 10) * Int($i / $iNumPerRow))

    $aButiD[$i] = GUICtrlCreateButton($aArray[$i][0], $x, $y, $iButWidth, $iButDepth) ;, $BS_NOTIFY)
Next

GUISetState(@SW_SHOW)

Do
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aButiD[0] To $aButiD[$iTotButtons - 1]
            $iIndex = ($msg - $aButiD[0])
            If StringRight($aArray[$iIndex][0], 7) = "Command" Then ; Something to distinguish an executable value from ini file.
                ShellExecute($aArray[$iIndex][1]) ; Buttons with the button's text ending in "Command".
            Else
                MsgBox(0, $aArray[$iIndex][0], "Value: " & $aArray[$iIndex][1], 2)
            EndIf
            $msg = ""
    EndSwitch
Until GUIGetMsg() = -3


The Test.ini file.

[Buttons]
Button1Text=Test 1
Button1Command=http://google.com
Button2Text=Test 2
Button2Command=cmd.exe
Button3Text=Test 3
Button3Command=https://www.autoitscript.com/forum/topic/178272-is-this-done-correctly/?do=findComment&comment=1279329
Button4Text=Test 4
Button4Command=Notepad.exe
Button5Text=Test 5
Button5Command=Calc.exe

 

Link to comment
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
 Share

×
×
  • Create New...