#include <WinAPI.au3>
#include <Array.au3>
Global $aEN[1], $LV, $Default

; #FUNCTION# ====================================================================================================================
; Name...........: _IconNameToID_Init
; Description ...: Load a libary for using in the _IconNameToID functions.
; Syntax.........: _IconNameToID_Init($sDllPath = -1)
; Parameters ....: $sDllPath = A valid path to a libary.
; Return values .: Success - 1
;                  Failure - -1, sets @error: -1
; Author ........: Sprenger120
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _IconNameToID_Init($sDllPath = -1)
	If $sDllPath = -1 Then Return SetError(-1, 0, -1)
	$hModule = _WinAPI_LoadLibraryEx($sDllPath, 0x2)
	If Not $hModule Then Return SetError(-1, 0, -1)
	_ResourceEnumNames($hModule, 14)
	If @error Then Return SetError(-1, 0, -1)
	_WinAPI_FreeLibrary($hModule)
	$aEN[0] = $sDllPath
	Return 1
EndFunc   ;==>_IconNameToID_Init


; #FUNCTION# ====================================================================================================================
; Name...........: _IconNameToID_Set
; Description ...: Set the icon of a control using _IconNameToID_Get
; Syntax.........: _IconNameToID_Set($iControlID = -1, $sIconName = "", $icontype = 1)
; Parameters ....: $iControlID = The ID of the control do you want to set.
; 				   $sIconName  = The Group Icon name of the icon.
;				   $icontype   = The show type of the icon
;					|0 = small
;					|1 = normal
; Return values .: Success - 1
;                  Failure - -1, sets @error: -1
; Author ........: Sprenger120
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _IconNameToID_Set($iControlID = -1, $sIconName = "", $icontype = 0)
	$sIconName = _IconNameToID_Get($sIconName)
	If $sIconName = "" Then
		Return ""
	Else
		GUICtrlSetImage($iControlID, $aEN[0], $sIconName, $icontype)
	EndIf
	Return 1
EndFunc   ;==>_IconNameToID_Set

; #FUNCTION# ====================================================================================================================
; Name...........: _IconNameToID_Get
; Description ...: Get the ID of a icon.
; Syntax.........: _IconNameToID_Get($sSerch)
; Parameters ....: $sSerch  = The Group Icon name of the icon.
; Return values .: Success - The ID of the icon.
;                  Failure - -1, sets @error: -1
; Author ........: Sprenger120
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _IconNameToID_Get($sSerch)
	$sRet = ""
	For $x = 1 To UBound($aEN) - 1
		If $aEN[$x] == $sSerch Then
			If $x = 0 Or $x  = 1 Then
				$sRet = $x
			Else
				$sRet = -$x
			EndIf
		EndIf
	Next
	ConsoleWrite($sRet & @CRLF)
	Return $sRet
EndFunc   ;==>_IconNameToID_Get


; #FUNCTION# ====================================================================================================================
; Name...........: _ResourceEnumNames
; Description ...:
; Syntax.........:
; Parameters ....:
; Return values .:
; Author ........: smashly
; Modified.......:
; Remarks .......: Internal
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================

Func _ResourceEnumNames($hModule, $iType)
	Local $aRet, $xCB
	If Not $hModule Then Return SetError(1, 0, 0)
	$xCB = DllCallbackRegister('___EnumResNameProc', 'int', 'int_ptr;int_ptr;int_ptr;int_ptr')
	$aRet = DllCall('kernel32.dll', 'int', 'EnumResourceNamesW', 'ptr', $hModule, 'int', $iType, 'ptr', DllCallbackGetPtr($xCB), 'ptr', 0)
	DllCallbackFree($xCB)
	If $aRet[0] <> 1 Then Return SetError(2, 0, 0)
	Return SetError(0, 0, 1)
EndFunc   ;==>_ResourceEnumNames

; #FUNCTION# ====================================================================================================================
; Name...........: ___EnumResNameProc
; Description ...:
; Syntax.........:
; Parameters ....:
; Return values .:
; Author ........: smashly
; Modified.......:
; Remarks .......: Internal
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func ___EnumResNameProc($hModule, $pType, $pName, $lParam)
	Local $aSize = DllCall('kernel32.dll', 'int', 'GlobalSize', 'ptr', $pName), $tBuf
	If $aSize[0] Then
		$tBuf = DllStructCreate('wchar[' & $aSize[0] & ']', $pName)
		ReDim $aEN[UBound($aEN) + 1]
		$aEN[0] += 1
		$aEN[UBound($aEN) - 1] = DllStructGetData($tBuf, 1)
	Else
		ReDim $aEN[UBound($aEN) + 1]
		$aEN[0] += 1
		$aEN[UBound($aEN) - 1] = $pName
	EndIf
	Return 1
EndFunc   ;==>___EnumResNameProc
