;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include-once
#include <GDIPlus.au3>
#include <WinAPIShellEx.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()
OnAutoItExitRegister("_Exit_GUICtrlMenu_CreateBitmap")

; #FUNCTION# ============================================================================================================
; Name...................:	_GUICtrlMenu_CreateBitmap
; Description .......:		Extracts the icon from $sFile and converts it to a HBitmap format
; Syntax................:	_GUICtrlMenu_CreateBitmap($sFile, $iIndex = 0, $iX = 16, $iY = 16)
; Parameters ......:		$sFile		- file name where the icon should be extracted from or an image should be loaded (*.dll, *.ico, *.exe, *.jpg, *.png, *.bmp, *.gif, *.tif)
;                  			$iIndex		- index of the icon from $sFile
;							$iW			- set the width of the extracted icon
;							$iH			- set the height of the extracted icon
;							$iBgColor	- set background color from _WinAPI_GetSysColor($COLOR_MENU) if $iBgColor = -1
; Return values .:			Success		- handle to a HBITMAP
;                 			Failure		- Returns 0 and sets error to 1-7
; Author ..............:	UEZ
; Version .............:	v0.71 Build 2018-08-17 beta
; Remarks ...........:		Don't forget to use _WinAPI_DeleteObject($<handle to the HBITMAP>) when closing to to release the resources
; =======================================================================================================================
Func _GUICtrlMenu_CreateBitmap($sFile, $iIndex = 0, $iW = 16, $iH = 16, $iBgColor = -1)
	Local $hIcon, $Ret, $hBitmap, $hContext, $bBinary = False
	Local $aChk = _GDIPlus_ImageGetFlags($sFile)
	If Not @error Then $bBinary = True
	If FileExists($sFile) Or $bBinary Then
		Local $sExt = StringMid($sFile, StringLen($sFile) - 3)
		If $bBinary Then $sExt = "0815"
		Switch $sExt
			Case ".dll", ".exe", ".ico"
				Return _WinAPI_GetFileIcon($sFile, $iIndex, $iW, $iH, $iBgColor)
			Case ".jpg", ".png", ".bmp", ".gif", ".tif", "0815"
				Local $hImage
				If Not $bBinary Then
					$hImage = _GDIPlus_ImageLoadFromFile($sFile)
					If @error Then Return SetError(4, @extended, 0)
				Else
					$hImage = $sFile
				EndIf
				$hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
				If @error Then Return SetError(5, @extended, 0)
				$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
				_GDIPlus_GraphicsSetInterpolationMode($hContext, $GDIP_INTERPOLATIONMODE_HIGHQUALITY)
				_GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iW, $iH)
				$hIcon =  _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
				_GDIPlus_GraphicsDispose($hContext)
				_GDIPlus_BitmapDispose($hBitmap)
				_GDIPlus_BitmapDispose($hImage)
				If Not $hIcon Then Return SetError(6, 0, 0)
			Case Else
				Return SetError(7, 0, 0)
		EndSwitch
		Return $hIcon
	Else
		Return SetError(1, 0, 0)
	EndIf
EndFunc   ;==>_GUICtrlMenu_CreateBitmap

Func _WinAPI_GetFileIcon($sFile, $iIndex, $iW, $iH, $iColor)
	Local $aRet, $hIcon, $hHBitmap
	Local $hDC, $hBackDC, $hBackSv

	$hIcon = _WinAPI_ShellExtractIcon($sFile, $iIndex, $iW, $iH)
	If @error Then Return SetError(6, @extended, 0)

	$hDC = _WinAPI_GetDC(0)
	$hBackDC = _WinAPI_CreateCompatibleDC($hDC)
	If $iColor = -1 Then $iColor = _WinAPI_GetSysColor($COLOR_MENU)
	$hHBitmap = _WinAPI_CreateSolidBitmap(0, $iColor, $iW, $iH)
	$hBackSv = _WinAPI_SelectObject($hBackDC, $hHBitmap)
	_WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, $iW, $iH)
	_WinAPI_DestroyIcon($hIcon)

	_WinAPI_SelectObject($hBackDC, $hBackSv)
	_WinAPI_ReleaseDC(0, $hDC)
	_WinAPI_DeleteDC($hBackDC)
	Return $hHBitmap
EndFunc   ;==>_GUICtrlMenu_CreateBitmap

Func _Exit_GUICtrlMenu_CreateBitmap()
	_GDIPlus_Shutdown()
EndFunc