Jump to content

Maps : Get/Set from, to Ini file


Recommended Posts


INI file

[GUI_Settings]
GuiXPos=638
GuiYPos=187
GuiWidth=640
GuiHeight=640
GuiTitle=My Custom GUI
GuiIcon=C:\Windows\SysWOW64\shell32.dll, 239
GuiFont=12, 400, 0, Comic Sans MS
RuningCnt=2
[GUI_Messages]
1=Move and resize the window, then reopen to check if it saves its position
2=Is everything alright?
3=to start again delete the ini file

$ini = _Map_GetfromIni($MyIni)
$value = $ini.Section.Key

$Title = $ini.GUI_Settings.GuiTitle

avoid spaces and dots in section names,  and keys

e.g. if it was 'GUI Settings'
$Title = $ini["GUI Settings"].GuiTitle

 

; https://www.autoitscript.com/forum/topic/212138-maps-getset-from-to-ini-file/?do=findComment&comment=1535856

#NoTrayIcon
#include <GUIConstants.au3>
#include <WinAPIShellEx.au3>
#include <Array.au3>

Opt("ExpandVarStrings", 1) ;0=don't expand, 1=do expand

Global $MyIni = SetIni(StringTrimRight(@ScriptFullPath, 4) & ".ini")
Global $ini = _Map_GetfromIni($MyIni)
OnAutoItExitRegister("SaveToExit")

_WinAPI_SetCurrentProcessExplicitAppUserModelID(_WinAPI_CreateGUID()) ; ensure a unique ID
Global $hGUI = GUICreate($ini.GUI_Settings.GuiTitle _
        , $ini.GUI_Settings.GuiWidth _
        , $ini.GUI_Settings.GuiHeight _
        , $ini.GUI_Settings.GuiXPos _
        , $ini.GUI_Settings.GuiYPos _
        , BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME))

; check for the icon
$aIcon = StringSplit($ini.GUI_Settings.GuiIcon, ", ", 1)
GUISetIcon($aIcon[1], ($aIcon[0] = 2 ? Int($aIcon[2]) : ""))

; check for the font
$aFont = StringSplit($ini.GUI_Settings.GuiFont, ", ", 1)
If $aFont[0] = 4 Then GUISetFont($aFont[1],  $aFont[2], $aFont[3], $aFont[4])

Global $Label1 = GUICtrlCreateLabel($ini.GUI_Messages[$ini.GUI_Settings.RuningCnt], 10, 15, $ini.GUI_Settings.GuiWidth - 20, 25)
GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT)

GUISetState(@SW_SHOW)

;more accuracy GUI reposition
WinMove($hGUI, "", Default, Default, $ini.GUI_Settings.GuiWidth, $ini.GUI_Settings.GuiHeight)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


;--------------------------------------------------------------------------------------------------------------------------------
Func SaveToExit()   ; save postion by exit
    Local $aWpos = WinGetPos($hGUI)
    $ini.GUI_Settings.GuiXPos = $aWpos[0]
    $ini.GUI_Settings.GuiYPos = $aWpos[1]
    $ini.GUI_Settings.GuiWidth = $aWpos[2]
    $ini.GUI_Settings.GuiHeight = $aWpos[3]
    $ini.GUI_Settings.RuningCnt += 1
    _Map_SaveToIni($ini, $MyIni)
EndFunc   ;==>SaveToExit
;--------------------------------------------------------------------------------------------------------------------------------
Func SetIni($sIniFile)   ; Make  a ini file if not exist
    ;Local $sIniFile = FileGetShortName(StringTrimRight(@ScriptFullPath, 4) & ".ini")
    If Not FileExists($sIniFile) Then
        Local $mIni[], $mSection[]
        $mSection.GuiXPos = -1
        $mSection.GuiYPos = -1
        $mSection.GuiWidth = @DesktopWidth / 3
        $mSection.GuiHeight = @DesktopWidth / 3
        $mSection.GuiTitle = "My Custom GUI"
        $mSection.GuiIcon = @SystemDir & "\shell32.dll, 239"
        $mSection.GuiFont = "12, 400, 0, Comic Sans MS"
        $mSection.RuningCnt = 1

        $mIni["GUI_Settings"] = $mSection ; add Section as GUI_Settings map to ini map

        Local $mMessage[]
        $mMessage[1] = "Move and resize the window, then reopen to check if it saves its position"
        $mMessage[2] = "Is everything alright?"
        $mMessage[3] = "to start again delete the ini file"

        $mIni["GUI_Messages"] = $mMessage ; add Section as GUI_Messages map to ini map

        _Map_SaveToIni($mIni, $sIniFile)
        Sleep(300)
        ShellExecute($sIniFile)

    EndIf
    Return $sIniFile
EndFunc   ;==>SetIni

; #FUNCTION# --------------------------------------------------------------------------------------------------------------------
; Name...........: _Map_GetfromIni
; Description....: Reads an INI file and returns a map of section names to their respective key-value pairs.
; Syntax.........: _Map_GetfromIni($sFilePath)
; Parameters.....: $sFilePath - The path to the INI file to read.
; Return values..: A map of maps where each section is a map of key-value pairs collection.
; Author ........: ioa747
; Notes .........: $ini = _Map_GetfromIni($sIniPath)
;                  value = $ini.Section.Key
; Link ..........:
; Dependencies...:
;--------------------------------------------------------------------------------------------------------------------------------
Func _Map_GetfromIni($sFilePath)
    Local $aSectionName, $Data, $mNewMap[]

    ;check if FileExists
    If Not FileExists($sFilePath) Then Return SetError(13, 0, -1)

    ; an array of all section names in the INI file.
    $aSectionName = IniReadSectionNames($sFilePath)

    For $R = 1 To $aSectionName[0]
        ; Read the INI section $aSectionName[$R]. This will return a 2 dimensional array.
        Local $aKey = IniReadSection($sFilePath, $aSectionName[$R])
        Local $mKey[]
        ; Check if an error occurred.
        If Not @error Then
            ; Enumerate through the array displaying the keys and their respective values.
            For $i = 1 To $aKey[0][0]
                ;ConsoleWrite($i & ") " & $aKey[$i][0] & ":" & $aKey[$i][1] & @CRLF)
                $mKey[$aKey[$i][0]] = $aKey[$i][1]
            Next
        EndIf
        $mNewMap[$aSectionName[$R]] = $mKey
    Next
    Return $mNewMap
EndFunc   ;==>_Map_GetfromIni
;--------------------------------------------------------------------------------------------------------------------------------
Func _Map_SaveToIni(ByRef $mMap, $sFilePath)
    Local $aSectionName, $mKey, $aKey, $Data
    $aSectionName = MapKeys($mMap)
    $mKey = $mMap[$aSectionName[0]]
    $aKey = MapKeys($mKey)
    For $R = 0 To UBound($aSectionName) - 1
        $mKey = $mMap[$aSectionName[$R]]
        $aKey = MapKeys($mKey)
        Local $iRowsCnt = UBound($aKey)
        Local $aSection[$iRowsCnt + 1][2]
        $aSection[0][0] = $iRowsCnt
        For $i = 0 To $iRowsCnt - 1
            $Data = $mKey[$aKey[$i]]
            ;ConsoleWrite($i & ") " & $aKey[$i] & ":" & $Data & @CRLF)
            $aSection[$i + 1][0] = $aKey[$i]
            $aSection[$i + 1][1] = $Data
        Next
        ; Write the array to the section labelled $aSectionName[$R].
        IniWriteSection($sFilePath, $aSectionName[$R], $aSection)
    Next
    ;_ArrayDisplay($aSection, "$aSection")
EndFunc   ;==>_Map_SaveToIni
;----------------------------------------------------------------------------------------

 

Edited by ioa747
add ; check for the font

I know that I know nothing

Link to comment
Share on other sites

  • ioa747 changed the title to Maps : Get/Set from, to Ini file

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...