Guest User

GIFAnimation.au3

a guest
Apr 21st, 2016
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 68.62 KB | None | 0 0
  1. ;.......script written by trancexx (trancexx at yahoo dot com)
  2.  
  3. #include-once
  4.  
  5. ; #INDEX# ========================================================================================
  6. ; Title ...............: GIFAnimation v2.1.2
  7. ; File Name............: GIFAnimation.au3
  8. ; Min. AutoIt Version..: v3.3.2.0
  9. ; Description .........: Display GIF in AutoIt
  10. ; Author... ...........: trancexx
  11. ; Dll .................: kernel32.dll, user32.dll, gdi32.dll, comctl32.dll, ole32.dll, gdiplus.dll
  12. ; ================================================================================================
  13.  
  14. ; #CONSTANTS# ===============================================================================
  15. ; Pseudo-handles for DLLs to use
  16. Global Const $hGIFDLL__KERNEL32 = DllOpen("kernel32.dll")
  17. Global Const $hGIFDLL__USER32 = DllOpen("user32.dll")
  18. Global Const $hGIFDLL__GDI32 = DllOpen("gdi32.dll")
  19. Global Const $hGIFDLL__COMCTL32 = DllOpen("comctl32.dll")
  20. Global Const $hGIFDLL__OLE32 = DllOpen("ole32.dll")
  21. Global Const $hGIFDLL__GDIPLUS = DllOpen("gdiplus.dll")
  22. ;============================================================================================
  23. ; #GLOBAL# ==================================================================================
  24. ; GIF associative string. Don't try to edit the content, this is very important internal variable!
  25. Global $sGIF__ASSOCSTRING_INTERNAL = ";"
  26. ;============================================================================================
  27.  
  28. ; #CURRENT# =================================================================================
  29. ;_GIF_DeleteGIF
  30. ;_GIF_ExitAnimation
  31. ;_GIF_GetCurrentFrame
  32. ;_GIF_GetDimension
  33. ;_GIF_GetSize
  34. ;_GIF_PauseAnimation
  35. ;_GIF_RefreshGIF
  36. ;_GIF_ResumeAnimation
  37. ;_GIF_ValidateGIF
  38. ;_GUICtrlCreateGIF
  39. ;_GUICtrlSetGIF
  40. ; ===========================================================================================
  41.  
  42. ; #FUNCTION# ;===============================================================================
  43. ; Name...........: _GIF_DeleteGIF
  44. ; Description ...: Deletes GIF control
  45. ; Syntax.........: _GIF_DeleteGIF($iGIFId)
  46. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  47. ; Return values .: Returns 1
  48. ; Author ........: trancexx
  49. ;============================================================================================
  50. Func _GIF_DeleteGIF($iGIFId, $fDelCtrl = True)
  51.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  52.     ; Read $pGIF (shorter version - only handles and memory pointers)
  53.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  54.             "ptr CodeBuffer;" & _
  55.             "hwnd ControlHandle;" & _
  56.             "handle ImageList;" & _
  57.             "bool ExitFlag;", _
  58.             $pGIF)
  59.     ; GIFThread. Will alow it to exit gracefully for it to close all internally opened handles
  60.     Local $hGIFThread = DllStructGetData($tGIF, "GIFThread")
  61.     If $hGIFThread Then
  62.         ; Maybe it's suspended
  63.         _GIF_ResumeThread($hGIFThread)
  64.         ; Set exit Flag
  65.         DllStructSetData($tGIF, "ExitFlag", 1)
  66.         ; Thread will exit now. Wait for it to exit
  67.         _GIF_WaitForSingleObject($hGIFThread)
  68.         _GIF_CloseHandle($hGIFThread) ; closing the handle
  69.     EndIf
  70.     ; CodeBuffer
  71.     Local $pCodeBuffer = DllStructGetData($tGIF, "CodeBuffer")
  72.     If $pCodeBuffer Then _GIF_MemGlobalFree($pCodeBuffer)
  73.     ; ImageList
  74.     Local $hImageList = DllStructGetData($tGIF, "ImageList")
  75.     If $hImageList Then _GIF_ImageList_Destroy($hImageList)
  76.     ; The rest of the cleaning job
  77.     _GIF_MemGlobalFree($pGIF)
  78.     ; Not sure if GUICtrlDelete will delete associated Bitmap. Will do that by myself...
  79.     _GIF_DeleteObject(GUICtrlSendMsg($iGIFId, 370, 0, 0)) ; STM_SETIMAGE
  80.     ; Delete control
  81.     If $fDelCtrl Then GUICtrlDelete($iGIFId)
  82.     ; Remove this entry from global assoc string
  83.     $sGIF__ASSOCSTRING_INTERNAL = StringReplace($sGIF__ASSOCSTRING_INTERNAL, $iGIFId & "|" & $pGIF & ";", "")
  84.     Return 1
  85. EndFunc   ;==>_GIF_DeleteGIF
  86.  
  87. ; #FUNCTION# ;===============================================================================
  88. ; Name...........: _GIF_ExitAnimation
  89. ; Description ...: Exits animation of GIF control
  90. ; Syntax.........: _GIF_ExitAnimation($iGIFId)
  91. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  92. ; Return values .: Returns 1
  93. ; Author ........: trancexx
  94. ;============================================================================================
  95. Func _GIF_ExitAnimation($iGIFId)
  96.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  97.     ; Read $pGIF (shorter version - only handles and memory pointers)
  98.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  99.             "ptr CodeBuffer;" & _
  100.             "hwnd ControlHandle;" & _
  101.             "handle ImageList;" & _
  102.             "bool ExitFlag;" & _
  103.             "bool Transparent;" & _
  104.             "dword CurrentFrame;", _
  105.             $pGIF)
  106.     ; GIFThread. Will alow it to exit gracefully for it to close all internally opened handles
  107.     Local $hGIFThread = DllStructGetData($tGIF, "GIFThread")
  108.     If $hGIFThread Then
  109.         ; Maybe it's suspended
  110.         _GIF_ResumeThread($hGIFThread)
  111.         ; Set exit Flag
  112.         DllStructSetData($tGIF, "ExitFlag", 1)
  113.         ; Thread will exit now. Wait for it to exit
  114.         _GIF_WaitForSingleObject($hGIFThread)
  115.         _GIF_CloseHandle($hGIFThread) ; closing the handle
  116.         DllStructSetData($tGIF, "GIFThread", 0)
  117.     EndIf
  118.     ; CodeBuffer
  119.     Local $pCodeBuffer = DllStructGetData($tGIF, "CodeBuffer")
  120.     If $pCodeBuffer Then _GIF_MemGlobalFree($pCodeBuffer)
  121.     DllStructSetData($tGIF, "CodeBuffer", 0)
  122.     ; ImageList
  123.     Local $hImageList = DllStructGetData($tGIF, "ImageList")
  124.     If $hImageList Then _GIF_ImageList_Destroy($hImageList)
  125.     DllStructSetData($tGIF, "ImageList", 0)
  126.     ; Set value of CurrentFrame to 0
  127.     DllStructSetData($tGIF, "CurrentFrame", 0)
  128.     ; Refresh the control
  129.     GUICtrlSetState($iGIFId, GUICtrlGetState($iGIFId))
  130.     Return 1
  131. EndFunc   ;==>_GIF_ExitAnimation
  132.  
  133. ; #FUNCTION# ;===============================================================================
  134. ; Name...........: _GIF_GetCurrentFrame
  135. ; Description ...: Retrieves index of currently displayed frame of the GIF control
  136. ; Syntax.........: _GIF_GetCurrentFrame($iGIFId)
  137. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  138. ; Return values .: Returns frame index
  139. ; Author ........: trancexx
  140. ;============================================================================================
  141. Func _GIF_GetCurrentFrame($iGIFId)
  142.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  143.     ; Read $pGIF (shorter version)
  144.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  145.             "ptr CodeBuffer;" & _
  146.             "hwnd ControlHandle;" & _
  147.             "handle ImageList;" & _
  148.             "bool ExitFlag;" & _
  149.             "bool Transparent;" & _
  150.             "dword CurrentFrame;", _
  151.             $pGIF)
  152.     Return DllStructGetData($tGIF, "CurrentFrame")
  153. EndFunc   ;==>_GIF_GetCurrentFrame
  154.  
  155. ; #FUNCTION# ;===============================================================================
  156. ; Name...........: _GIF_GetDimension
  157. ; Description ...: Returns array of GIF image dimension
  158. ; Syntax.........: _GIF_GetDimension($vGIF [, $vAdditionalData])
  159. ; Parameters ....: $vGIF - filename of the GIF file
  160. ;                  $vAdditionalData - optional parameter used if the GIF if embedded resource inside PE file.
  161. ;                                     resurce type, name and language (all separated by semicolon ';')
  162. ;                                     language is optional
  163. ; Return values .: Success - Returns array which first element [0] is width,
  164. ;                                                second element [1] is height
  165. ;                  Failure - Returns array which first element [0] is 0 (zero),
  166. ;                                                second element [1] is 0 (zero),
  167. ;                          - Sets @error:
  168. ;                           |1 - invalid input or Gdip failure
  169. ; Author ........: trancexx
  170. ;============================================================================================
  171. Func _GIF_GetDimension($vGIF, $vAdditionalData = "")
  172.     Local $aOut[2] = [0, 0] ; preset output
  173.     Local $vData
  174.     ; Process passed
  175.     If IsBinary($vGIF) Then ; GIF binary data passed
  176.         $vData = $vGIF
  177.     Else
  178.         If $vAdditionalData Then ; likely resource
  179.             Local $aData = StringSplit($vAdditionalData, ";", 2)
  180.             If UBound($aData) < 3 Then ReDim $aData[3]
  181.             $vData = _GIF_ResourceGetAsRaw($vGIF, $aData[0], $aData[1], $aData[2])
  182.             If @error Then
  183.                 $vData = $vGIF ; it's not resource, $vAdditionalData was wrong
  184.             Else
  185.                 ; RT_BITMAP is stored without BMP header. Add it:
  186.                 If $aData[0] = 2 Then $vData = _GIF_MakeBitmapFromRT_BITMAP($vData)
  187.             EndIf
  188.         Else
  189.             $vData = $vGIF ; maybe $vGIF is the file name
  190.         EndIf
  191.     EndIf
  192.     Local $hGDIP ; Gdip engine
  193.     Local $hMemGlobal ; Memory
  194.     ; Bitmap object
  195.     Local $pBitmap, $iWidth, $iHeight
  196.     If IsString($vData) Then
  197.         $pBitmap = _GIF_CreateBitmapFromFile($hGDIP, $vData, $iWidth, $iHeight)
  198.         If @error Then $pBitmap = _GIF_CreateBitmapFromBinaryImage($hGDIP, $hMemGlobal, Binary($vData), $iWidth, $iHeight)
  199.         If @error Then
  200.             $vData = FileRead($vData)
  201.             $pBitmap = _GIF_CreateBitmapFromBinaryImage($hGDIP, $hMemGlobal, $vData, $iWidth, $iHeight)
  202.         EndIf
  203.     Else
  204.         $pBitmap = _GIF_CreateBitmapFromBinaryImage($hGDIP, $hMemGlobal, $vData, $iWidth, $iHeight)
  205.     EndIf
  206.     If @error Then Return SetError(1, 0, $aOut) ; Nothing worked. Invalid input or Gdip failure.
  207.     _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  208.     $aOut[0] = $iWidth
  209.     $aOut[1] = $iHeight
  210.     Return $aOut
  211. EndFunc   ;==>_GIF_GetDimension
  212.  
  213. ; #FUNCTION# ;===============================================================================
  214. ; Name...........: _GIF_GetSize
  215. ; Description ...: Returns array of GIF Control dimension
  216. ; Syntax.........: _GIF_GetSize($iGIFId)
  217. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  218. ; Return values .: Success - Returns array which first element [0] is width,
  219. ;                                                second element [1] is height,
  220. ;                  Failure - Returns array which first element [0] is 0 (zero),
  221. ;                                                second element [1] is 0 (zero),
  222. ;                           Sets @error to 1
  223. ; Author ........: trancexx
  224. ;============================================================================================
  225. Func _GIF_GetSize($iGIFId)
  226.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  227.     Local $aOut[2] = [0, 0] ; preset output
  228.     ; Read $pGIF (shorter version)
  229.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  230.             "ptr CodeBuffer;" & _
  231.             "hwnd ControlHandle;" & _
  232.             "handle ImageList;", _
  233.             $pGIF)
  234.     ; ImageList
  235.     Local $hImageList = DllStructGetData($tGIF, "ImageList")
  236.     If $hImageList Then Return _GIF_ImageList_GetIconSize($hImageList)
  237.     Local $hControl = DllStructGetData($tGIF, "ControlHandle")
  238.     If Not $hControl Then Return SetError(1, 0, $aOut)
  239.     Local $aArray = WinGetPos($hControl)
  240.     If @error Then Return SetError(1, 0, $aOut)
  241.     $aOut[0] = $aArray[2]
  242.     $aOut[1] = $aArray[3]
  243.     Return $aOut
  244. EndFunc   ;==>_GIF_GetSize
  245.  
  246. ; #FUNCTION# ;===============================================================================
  247. ; Name...........: _GIF_PauseAnimation
  248. ; Description ...: Pauses animation of GIF control
  249. ; Syntax.........: _GIF_PauseAnimation($iGIFId)
  250. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  251. ; Return values .: Returns 1
  252. ; Author ........: trancexx
  253. ;============================================================================================
  254. Func _GIF_PauseAnimation($iGIFId)
  255.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  256.     ; Read $pGIF (short version - only GIFThread)
  257.     Local $tGIF = DllStructCreate("handle GIFThread;", $pGIF)
  258.     ; GIFThread
  259.     Local $hGIFThread = DllStructGetData($tGIF, "GIFThread")
  260.     If Not $hGIFThread Then Return SetExtended(1, 1)
  261.     If _GIF_SuspendThread($hGIFThread) Then _GIF_ResumeThread($hGIFThread) ; make sure not to increment suspended count of the thread
  262.     Return 1
  263. EndFunc   ;==>_GIF_PauseAnimation
  264.  
  265. ; #FUNCTION# ;===============================================================================
  266. ; Name...........: _GIF_RefreshGIF
  267. ; Description ...: Refreshes GIF control
  268. ; Syntax.........: _GIF_RefreshGIF($iGIFId)
  269. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  270. ; Return values .: Success - Returns 1. Refreshes GIF control
  271. ;                  Failure - Sets @error:
  272. ;                  |1 - GIF Control doesn't exist
  273. ;                  |2 - Couldn't get DC for the control
  274. ; Remarks........: Meant to be used with GUIRegisterMsg function
  275. ; Author ........: trancexx
  276. ;============================================================================================
  277. Func _GIF_RefreshGIF($iGIFId)
  278.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  279.     ; Make structure at $pGIF address
  280.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  281.             "ptr CodeBuffer;" & _
  282.             "hwnd ControlHandle;" & _
  283.             "handle ImageList;" & _
  284.             "bool ExitFlag;" & _
  285.             "bool Transparent;" & _
  286.             "dword CurrentFrame;", _
  287.             $pGIF)
  288.     ; If there is no $pGIF error will occur. Great opportunity to return
  289.     If @error Then Return SetError(1, 0, 0)
  290.     ; Is it animated at all
  291.     If Not DllStructGetData($tGIF, "ImageList") Then Return 1
  292.     ; Get DC
  293.     Local $hDC = _GIF_GetDC(DllStructGetData($tGIF, "ControlHandle"))
  294.     If @error Then Return SetError(2, 0, 0)
  295.     Local $iColorRef = -1
  296.     If DllStructGetData($tGIF, "Transparent") Then $iColorRef = _GIF_GetUnderlyingColor(DllStructGetData($tGIF, "ControlHandle")) ; if transparent gif then set bk color to GUI's bk color
  297.     _GIF_ImageList_DrawEx(DllStructGetData($tGIF, "ImageList"), DllStructGetData($tGIF, "CurrentFrame"), $hDC, 0, 0, 0, 0, $iColorRef, -1, 0)
  298.     _GIF_ReleaseDC(0, $hDC)
  299.     Return 1
  300. EndFunc   ;==>_GIF_RefreshGIF
  301.  
  302. ; #FUNCTION# ;===============================================================================
  303. ; Name...........: _GIF_ResumeAnimation
  304. ; Description ...: Resumes stopped animation of GIF control
  305. ; Syntax.........: _GIF_ResumeAnimation($iGIFId)
  306. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  307. ; Return values .: Returns 1
  308. ; Author ........: trancexx
  309. ;============================================================================================
  310. Func _GIF_ResumeAnimation($iGIFId)
  311.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  312.     ; Read $pGIF (short version - only GIFThread)
  313.     Local $tGIF = DllStructCreate("handle GIFThread;", $pGIF)
  314.     ; GIFThread
  315.     Local $hGIFThread = DllStructGetData($tGIF, "GIFThread")
  316.     If Not $hGIFThread Then Return SetExtended(1, 1)
  317.     If _GIF_ResumeThread($hGIFThread) = 2 Then _GIF_SuspendThread($hGIFThread) ; make sure not to increment non-suspended count of the thread
  318.     Return 1
  319. EndFunc   ;==>_GIF_ResumeAnimation
  320.  
  321. ; #FUNCTION# ;===============================================================================
  322. ; Name...........: _GIF_ValidateGIF
  323. ; Description ...: Removes the GIF Control from the update region of the main window and in the same time upates the control
  324. ; Syntax.........: _GIF_ValidateGIF($iGIFId)
  325. ; Parameters ....: $iGIFId - Control ID. Returned by _GUICtrlCreateGIF function
  326. ; Return values .: Success - Returns 1
  327. ;                  Failure - Sets @error to 1
  328. ; Remarks........: Meant to be used with GUIRegisterMsg function
  329. ; Author ........: trancexx
  330. ;============================================================================================
  331. Func _GIF_ValidateGIF($iGIFId)
  332.     Local $pGIF = _GIF_GetGIFAssoc($iGIFId)
  333.     ; Make structure at $pGIF address
  334.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  335.             "ptr CodeBuffer;" & _
  336.             "hwnd ControlHandle;" & _
  337.             "handle ImageList;" & _
  338.             "bool ExitFlag;" & _
  339.             "bool Transparent;" & _
  340.             "dword CurrentFrame;" & _
  341.             "dword NumberOfFrames;", _
  342.             $pGIF)
  343.     ; If there is no $pGIF error will occur. Great opportunity to return
  344.     If @error Then Return SetError(1, 0, 0)
  345.     ; Check to see if it's needed to proceed. If not then just return sucess
  346.     If DllStructGetData($tGIF, "NumberOfFrames") < 2 Then Return 1
  347.     ; Is it animated at all
  348.     If Not DllStructGetData($tGIF, "GIFThread") Then Return 1
  349.     ; Redraw the control
  350.     _GIF_RedrawWindow(DllStructGetData($tGIF, "ControlHandle"), 0, 0, 1280) ; RDW_UPDATENOW | RDW_FRAME
  351.     _GIF_RefreshGIF($iGIFId)
  352.     Return 1
  353. EndFunc   ;==>_GIF_ValidateGIF
  354.  
  355. ; #FUNCTION# ;===============================================================================
  356. ; Name...........: _GUICtrlCreateGIF
  357. ; Description ...: Creates GIF control for the GUI
  358. ; Syntax.........: _GUICtrlCreateGIF($vGIF, $vAdditionalData, $iLeft, $iTop [, $iWidth = Default [, $iHeight = Default [, $iRenderingStyle = Default [, $iForcedARGB = Default ]]]])
  359. ; Parameters ....: $vGIF - filename of the GIF to be loaded
  360. ;                  $vAdditionalData - resurce type, name and language (all separated by semicolon ';') if the GIF is stored as resource inside PE file
  361. ;                                     language is optional
  362. ;                  $iLeft - left side coordinate of the control
  363. ;                  $iTop - the top of the control
  364. ;                  $iWidth - [optional] the width of the control. Default is the width of the image.
  365. ;                  $iHeight - [optional] the height of the control. Default is the height of the image.
  366. ;                  $iRenderingStyle - [optional] Advanced optional parameter. Coresponds to style paremeter of ImageList_DrawEx function.Default is ILD_NORMAL for images with defined transparency.
  367. ;                  $iForcedARGB - [optional] Advanced optional parameter. Use with semi-transparent images. Default is 0xFF000000 (opaque black).
  368. ; Return values .: Success - Returns controlID of the new control
  369. ;                          - Sets @extended to 1 if GIF contains only one frame (no animation)
  370. ;                  Failure - Sets @error:
  371. ;                  |1 - if @extended is not set then failure is likely due to invalid input
  372. ;                       if @extended = 1 then Gdip failure occured
  373. ;                       0 (zero) is returned in both cases
  374. ;                  |2 - Needed memory allocation failed. Returns ControlID.
  375. ;                  |3 - Memory protection step failed. Returns ControlID.
  376. ;                  |4 - Some of the functions are not accessible for unknown reasons. Check @extended to determine which one. Returns ControlID.
  377. ;                  |5 - Couldn't create thread for the animation. Returns ControlID.
  378. ; Author ........: trancexx
  379. ;============================================================================================
  380. Func _GUICtrlCreateGIF($vGIF, $vAdditionalData, $iLeft, $iTop, $iWidth = Default, $iHeight = Default, $iRenderingStyle = Default, $iForcedARGB = Default, $hGIFControl = 0)
  381.     If $iWidth = -1 Then $iWidth = Default
  382.     If $iHeight = -1 Then $iHeight = Default
  383.     Local $vData
  384.     ; Process passed
  385.     If IsBinary($vGIF) Then ; GIF binary data passed
  386.         $vData = $vGIF
  387.     Else
  388.         If $vAdditionalData Then ; likely resource
  389.             Local $aData = StringSplit($vAdditionalData, ";", 2)
  390.             If UBound($aData) < 3 Then ReDim $aData[3]
  391.             $vData = _GIF_ResourceGetAsRaw($vGIF, $aData[0], $aData[1], $aData[2])
  392.             If @error Then
  393.                 $vData = $vGIF ; it's not resource, $vAdditionalData was wrong
  394.             Else
  395.                 ; RT_BITMAP is stored without BMP header. Add it:
  396.                 If $aData[0] = 2 Then $vData = _GIF_MakeBitmapFromRT_BITMAP($vData)
  397.             EndIf
  398.         Else
  399.             $vData = $vGIF ; maybe $vGIF is the file name
  400.         EndIf
  401.     EndIf
  402.     ; Generate pGIF
  403.     Local $iWidthDef, $iHeightDef
  404.     If Not IsKeyword($iWidthDef) = 1 Then $iWidthDef = $iWidth
  405.     If Not IsKeyword($iHeightDef) = 1 Then $iHeightDef = $iHeight
  406.     Local $pGIF = _GIF_Create_pGIF($vData, $iWidthDef, $iHeightDef, $hGIFControl, $iLeft, $iTop, $iForcedARGB)
  407.     If @error Then ; try some more options
  408.         $vData = FileRead($vData)
  409.         $pGIF = _GIF_Create_pGIF($vData, $iWidthDef, $iHeightDef, $hGIFControl, $iLeft, $iTop, $iForcedARGB)
  410.         If @error Then ; finally maybe it's GIF content passed as string
  411.             $pGIF = _GIF_Create_pGIF(Binary($vGIF), $iWidthDef, $iHeightDef, $hGIFControl, $iLeft, $iTop, $iForcedARGB)
  412.             If @error Then Return SetError(1, @extended = True, 0) ; Nothing worked, Invalid input or Gdip failure.
  413.         EndIf
  414.     EndIf
  415.     ; Arrange/parse that space
  416.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  417.             "ptr CodeBuffer;" & _
  418.             "hwnd ControlHandle;" & _
  419.             "handle ImageList;" & _
  420.             "bool ExitFlag;" & _
  421.             "bool Transparent;" & _
  422.             "dword CurrentFrame;" & _
  423.             "dword NumberOfFrames;", _
  424.             $pGIF)
  425.     ; Read NumberOfFrames to know the size of the structure
  426.     Local $iFrameCount = DllStructGetData($tGIF, "NumberOfFrames")
  427.     ; Make it complete now:
  428.     $tGIF = DllStructCreate("handle GIFThread;" & _
  429.             "ptr CodeBuffer;" & _
  430.             "hwnd ControlHandle;" & _
  431.             "handle ImageList;" & _
  432.             "bool ExitFlag;" & _
  433.             "bool Transparent;" & _
  434.             "dword CurrentFrame;" & _
  435.             "dword NumberOfFrames;" & _
  436.             "dword FrameDelay[" & $iFrameCount & "];", _
  437.             $pGIF)
  438.     ; Set default resizing value for this control
  439.     GUICtrlSetResizing($hGIFControl, 802)
  440.     ; Save Control handle
  441.     DllStructSetData($tGIF, "ControlHandle", GUICtrlGetHandle($hGIFControl))
  442.     ; If there is just one frame there is no animation. Return control id
  443.     If $iFrameCount = 1 Then
  444.         ; Expand global assoc string
  445.         $sGIF__ASSOCSTRING_INTERNAL &= $hGIFControl & "|" & $pGIF & ";"
  446.         Return SetExtended(1, $hGIFControl)
  447.     EndIf
  448.     ; Allocate enough memory space for machine code
  449.     Local $iSizeCodeBuffer = 157
  450.     If @AutoItX64 Then $iSizeCodeBuffer = 220 ; x64 code is little longer
  451.     Local $pCodeBuffer = _GIF_MemGlobalAlloc($iSizeCodeBuffer, 64) ; GPTR
  452.     If @error Then Return SetError(2, 0, $hGIFControl) ; Couldn't allocate
  453.     ; Save that value
  454.     DllStructSetData($tGIF, "CodeBuffer", $pCodeBuffer)
  455.     ; Set proper 'protection'
  456.     _GIF_VirtualProtect($pCodeBuffer, $iSizeCodeBuffer, 64) ; MEM_EXECUTE_READWRITE
  457.     If @error Then Return SetError(3, 0, $hGIFControl) ; Couldn't set MEM_EXECUTE_READWRITE
  458.     Local $tCodeBuffer = DllStructCreate("byte[" & $iSizeCodeBuffer & "]", $pCodeBuffer)
  459.     ; Get functions addresses
  460.     Local $pImageList_DrawEx = _GIF_GetAddress(_GIF_GetModuleHandle("comctl32.dll"), "ImageList_DrawEx")
  461.     If @error Then Return SetError(4, 1, $hGIFControl) ; Couldn't get address of ImageList_DrawEx function
  462.     Local $pSleep = _GIF_GetAddress(_GIF_GetModuleHandle("kernel32.dll"), "Sleep")
  463.     If @error Then Return SetError(4, 2, $hGIFControl) ; Couldn't get address of Sleep function
  464.     Local $pGetPixel = _GIF_GetAddress(_GIF_GetModuleHandle("gdi32.dll"), "GetPixel")
  465.     If @error Then Return SetError(4, 3, $hGIFControl) ; Couldn't get address of GetPixel function
  466.     Local $hUser32 = _GIF_GetModuleHandle("user32.dll")
  467.     Local $pGetDC = _GIF_GetAddress($hUser32, "GetDC")
  468.     If @error Then Return SetError(4, 4, $hGIFControl) ; Couldn't get address of GetDC function
  469.     Local $pReleaseDC = _GIF_GetAddress($hUser32, "ReleaseDC")
  470.     If @error Then Return SetError(4, 5, $hGIFControl) ; Couldn't get address of ReleaseDC function
  471.     ; Read from $tGIF
  472.     Local $hImageList = DllStructGetData($tGIF, "ImageList")
  473.     Local $hControl = DllStructGetData($tGIF, "ControlHandle")
  474.     ; Define style for ImageList_DrawEx function
  475.     Local $iStyle
  476.     If $iRenderingStyle = Default Then
  477.         $iStyle = 1
  478.         If DllStructGetData($tGIF, "Transparent") Then $iStyle = 0 ; correct value if needed
  479.     Else
  480.         $iStyle = $iRenderingStyle
  481.     EndIf
  482.     ; Write code
  483.     If @AutoItX64 Then
  484.         DllStructSetData($tCodeBuffer, 1, _
  485.                 "0x" & _
  486.                 "4883EC" & _GIF_SwapEndian(88, 1) & _                                    ; sub rsp, 88d ;<- expand the stack
  487.                 "" & _ ; 10 START
  488.                 "4831F6" & _                                                             ; xor rsi, rsi ;<- rsi = 0
  489.                 "" & _ ; 20 MAIN LOOP
  490.                 "" & _ ; 25 SET CurrentFrame
  491.                 "8BC6" & _                                                               ; mov aax, asi ;<- esi is current frame index. Copy it to eax
  492.                 "A3" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "CurrentFrame"), 8) & _    ; mov $pCurrentFrame, eax ;<- from eax to structure
  493.                 "" & _ ; 30 GET CONTROL'S DC
  494.                 "48B9" & _GIF_SwapEndian($hControl, 8) & _                               ; mov rcx,  Control handle
  495.                 "48B8" & _GIF_SwapEndian($pGetDC, 8) & _                                 ; mov rax, GetDC ;<- set rax to address of GetDC function
  496.                 "FFD0" & _                                                               ; call rax ;<- calling rax (function)
  497.                 "" & _ ; 50 SAVE DC (to release later)
  498.                 "4889C3" & _                                                             ; mov rbx, rax ;<- storing DC into rbx
  499.                 "" & _ ; 60 GET BACKGROUND COLOR (upper left pixel)
  500.                 "49C7C0" & _GIF_SwapEndian(0, 4) & _                                     ; mov r8, $iTop ;<- Y coordinate (0)
  501.                 "BA" & _GIF_SwapEndian(0, 4) & _                                         ; mov rdx, $iTop ;<- X coordinate (0)
  502.                 "4889C1" & _                                                             ; mov rcx, rax ;<- DC handle (currently rax=rbx, so I could have set it to rbx too with no difference)
  503.                 "48B8" & _GIF_SwapEndian($pGetPixel, 8) & _                              ; mov rax, GetPixel ;<- set rax to address of GetPixel function
  504.                 "FFD0" & _                                                               ; call rax ;<- calling rax (function)
  505.                 "" & _ ; 65 CHECK for CLR_INVALID
  506.                 "3D" & _GIF_SwapEndian(-1, 4) & _                                        ; cmp eax, -1
  507.                 "75" & _GIF_SwapEndian(2, 1) & _                                         ; jne 2d ;<- if not equal then jump forward 2 bytes; (to 70)
  508.                 "8BC7" & _                                                               ; mov eax, edi ; If CLR_INVALID then retrieve the color value from memory (edi register).
  509.                 "" & _ ; 70
  510.                 "8BF8" & _                                                               ; mov edi, eax ;<- from eax to edi This is to save the last valid value.
  511.                 "" & _ ; 80
  512.                 "89442438" & _                                                           ; mov [rsp+56], eax ;<- SET $iBkColRef parameter
  513.                 "B8" & _GIF_SwapEndian($iStyle, 4) & _                                   ; mov eax, $iStyle
  514.                 "89442448" & _                                                           ; mov [rsp+72], eax ;<- SET $iStyle parameter
  515.                 "4989D8" & _                                                             ; mov r8, rbx ; <- SET $hDC parameter
  516.                 "49C7C1" & _GIF_SwapEndian(0, 4) & _                                     ; mov r9, 0 ; <- SET $iXPos parameter
  517.                 "89F2" & _                                                               ; mov edx, esi ; <- SET $iImageIndex parameter
  518.                 "48B9" & _GIF_SwapEndian($hImageList, 8) & _                             ; mov rcx,  $hImageList handle ; <- SET $hImageList parameter
  519.                 "" & _ ; 100 DRAW FINALLY
  520.                 "48B8" & _GIF_SwapEndian($pImageList_DrawEx, 8) & _                      ; mov rax, $pImageList_DrawEx ;<- set rax to address of ImageList_DrawEx function
  521.                 "FFD0" & _                                                               ; call rax ;<- calling rax (function)
  522.                 "" & _ ; 110 RELEASE DC
  523.                 "4889DA" & _                                                             ; mov rdx, rbx ;<- previously stored DC
  524.                 "48B9" & _GIF_SwapEndian($hControl, 8) & _                               ; mov rcx, Control handle ;<- can be NULL too
  525.                 "48B8" & _GIF_SwapEndian($pReleaseDC, 8) & _                             ; mov rax, ReleaseDC ;<- set rax to address of ReleaseDC function
  526.                 "FFD0" & _                                                               ; call rax ;<- calling rax (function)
  527.                 "" & _ ; 115 CHECK IF EXIT FLAG IS SET
  528.                 "A1" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "ExitFlag"), 8) & _        ; mov eax, Address of ExitFlag
  529.                 "85C0" & _                                                               ; test eax, eax
  530.                 "75" & _GIF_SwapEndian(46, 1) & _                                        ; jne 46d ;<- if set then jump forward 46 bytes; (to 160)
  531.                 "" & _ ; 120 SLEEP FOR GIF TIME DELAY VALUE
  532.                 "48BB" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "FrameDelay"), 8) & _    ; mov rbx, Address of FrameDelay
  533.                 "488B0CB3" & _                                                           ; mov rcx, qword[rbx+4*rsi] ;<- move pointer for every new frame to exact position
  534.                 "48B8" & _GIF_SwapEndian($pSleep, 8) & _                                 ; mov rax, Sleep ;<- set rax to address of Sleep function
  535.                 "FFD0" & _                                                               ; call rax ;<- calling rax (function)
  536.                 "" & _ ; 130 INCREMENTATING 'POSITION INDEX'
  537.                 "FFC6" & _                                                               ; inc esi ;<- increment esi by value of 1
  538.                 "" & _ ; 140 CHECK 'POSITION'
  539.                 "81FE" & _GIF_SwapEndian($iFrameCount, 4) & _                            ; cmp esi, $iFrameCount ;<- compare esi with $iFrameCount
  540.                 "" & _ ; 150 DEAL WITH DIFFERENT CASES
  541.                 "74" & _GIF_SwapEndian(5, 1) & _                                         ; je 5d ;<- if equal jump forward five bytes
  542.                 "E9" & _GIF_SwapEndian(-200, 4) & _                                      ; jmp -200d ;<- jump back 200 bytes (to 20)
  543.                 "E9" & _GIF_SwapEndian(-208, 4) & _                                      ; jmp -208d ;<- jump back 208 bytes (to 10)
  544.                 "" & _ ; 160 SET EXIT CODE TO 0 BEFORE THE END
  545.                 "4831C0" & _                                                             ; xor rax, rax ;<- rax = 0
  546.                 "4883C4" & _GIF_SwapEndian(88, 1) & _                                    ; add rsp, 88d ;<- shrink stack (88 bytes)
  547.                 "C3" _                                                                   ; ret ;<- Return.
  548.                 )
  549.     Else
  550.         DllStructSetData($tCodeBuffer, 1, _
  551.                 "0x" & _
  552.                 "" & _ ; 10 START
  553.                 "33F6" & _                                                               ; xor esi, esi ;<- esi = 0
  554.                 "" & _ ; 20 MAIN LOOP
  555.                 "" & _ ; 25 SET CurrentFrame
  556.                 "8BC6" & _                                                               ; mov eax, esi ;<- esi is current frame index. Copy it to eax
  557.                 "A3" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "CurrentFrame"), 4) & _    ; mov $pCurrentFrame, eax ;<- from eax to structure
  558.                 "68" & _GIF_SwapEndian($iStyle, 4) & _                                   ; push Style
  559.                 "68" & _GIF_SwapEndian(-1, 4) & _                                        ; push FgColRef
  560.                 "" & _ ; GET CONTROL'S DC
  561.                 "68" & _GIF_SwapEndian($hControl, 4) & _                                 ; push Control handle
  562.                 "B8" & _GIF_SwapEndian($pGetDC, 4) & _                                   ; mov eax, GetDC ;<- set eax to address of GetDC function
  563.                 "FFD0" & _                                                               ; call eax ;<- calling eax (function)
  564.                 "" & _ ; 50 SAVE DC (to release later)
  565.                 "8BD8" & _                                                               ; mov ebx, eax ;<- storing DC into ebx
  566.                 "" & _ ; 60 GET BACKGROUND COLOR (upper left pixel)
  567.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push Y coordinate
  568.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push X coordinate
  569.                 "53" & _                                                                 ; push ebx ;<- DC handle (currently eax=ebx, so I could have pushed eax too with no difference)
  570.                 "B8" & _GIF_SwapEndian($pGetPixel, 4) & _                                ; mov eax, GetPixel ;<- set eax to address of GetPixel function
  571.                 "FFD0" & _                                                               ; call eax ;<- calling eax (function)
  572.                 "" & _ ; 65 CHECK for CLR_INVALID
  573.                 "3D" & _GIF_SwapEndian(-1, 4) & _                                        ; cmp eax, -1
  574.                 "75" & _GIF_SwapEndian(2, 1) & _                                         ; jne 2d ;<- if not equal then jump forward 2 bytes; (to 70)
  575.                 "8BC7" & _                                                               ; mov eax, edi ; If CLR_INVALID then retrieve the color value from memory (edi register).
  576.                 "" & _ ; 70
  577.                 "8BF8" & _                                                               ; mov edi, eax ;<- from eax to edi This is to save the last valid value.
  578.                 "" & _ ; 80
  579.                 "50" & _                                                                 ; push eax ; push retrieved background color on the stack
  580.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push Height
  581.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push Width
  582.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push YPos
  583.                 "68" & _GIF_SwapEndian(0, 4) & _                                         ; push XPos
  584.                 "53" & _                                                                 ; push ebx ;<- push DC (again)
  585.                 "56" & _                                                                 ; push esi ;<- this is ImageList's index of the frame to render
  586.                 "68" & _GIF_SwapEndian($hImageList, 4) & _                               ; push ImageList
  587.                 "" & _ ; 100 DRAW FINALLY
  588.                 "B8" & _GIF_SwapEndian($pImageList_DrawEx, 4) & _                        ; mov eax, ImageList_DrawEx ;<- set eax to address of ImageList_DrawEx function
  589.                 "FFD0" & _                                                               ; call eax ;<- calling eax (function)
  590.                 "" & _ ; 110 RELEASE DC
  591.                 "53" & _                                                                 ; push ebx ;<- DC is found in ebx
  592.                 "68" & _GIF_SwapEndian($hControl, 4) & _                                 ; push Control handle ;<- can be NULL also
  593.                 "B8" & _GIF_SwapEndian($pReleaseDC, 4) & _                               ; mov eax, ReleaseDC ;<- set eax to address of ReleaseDC function
  594.                 "FFD0" & _                                                               ; call eax ;<- calling eax (function)
  595.                 "" & _ ; 115 CHECK IF EXIT FLAG IS SET
  596.                 "A1" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "ExitFlag"), 4) & _        ; mov eax, Address of ExitFlag
  597.                 "85C0" & _                                                               ; test eax, eax
  598.                 "75" & _GIF_SwapEndian(35, 1) & _                                        ; jne 35d ;<- if set then jump forward 35 bytes; (to 160)
  599.                 "" & _ ; 120 SLEEP FOR GIF TIME DELAY VALUE
  600.                 "BB" & _GIF_SwapEndian(DllStructGetPtr($tGIF, "FrameDelay"), 4) & _      ; mov ebx, Address of FrameDelay
  601.                 "8B0CB3" & _                                                             ; mov ecx, dword[ebx+4*esi] ;<- move pointer for every new frame to exact position
  602.                 "51" & _                                                                 ; push ecx ;<- push TimeDelay value
  603.                 "B8" & _GIF_SwapEndian($pSleep, 4) & _                                   ; mov eax, Sleep ;<- set eax to address of Sleep function
  604.                 "FFD0" & _                                                               ; call eax ;<- calling eax (function)
  605.                 "" & _ ; 130 INCREMENTATING 'POSITION INDEX'
  606.                 "46" & _                                                                 ; inc esi ;<- increment esi by value of 1
  607.                 "" & _ ; 140 CHECK 'POSITION'
  608.                 "81FE" & _GIF_SwapEndian($iFrameCount, 4) & _                            ; cmp esi, $iFrameCount ;<- compare esi with $iFrameCount
  609.                 "" & _ ; 150 DEAL WITH DIFFERENT CASES
  610.                 "74" & _GIF_SwapEndian(5, 1) & _                                         ; je 5d ;<- if equal jump forward five bytes
  611.                 "E9" & _GIF_SwapEndian(-147, 4) & _                                      ; jmp -147d ;<- jump back 147 bytes (to 20)
  612.                 "E9" & _GIF_SwapEndian(-154, 4) & _                                      ; jmp -154d ;<- jump back 154 bytes (to 10)
  613.                 "" & _ ; 160 SET EXIT CODE TO 0 BEFORE THE END
  614.                 "33C0" & _                                                               ; xor eax, eax ;<- eax = 0
  615.                 "C3" _                                                                   ; ret ;<- Return.
  616.                 )
  617.             EndIf
  618.     ; Create thread in which to run the code
  619.     Local $hThread = _GIF_CreateThread($pCodeBuffer)
  620.     If @error Then Return SetError(5, 0, $hGIFControl) ; Couldn't create thread
  621.     ; Save that handle
  622.     DllStructSetData($tGIF, "GIFThread", $hThread)
  623.     ; Redraw parent window
  624.     _GIF_InvalidateRect(_GIF_GetParent($hControl))
  625.     ; Expand global assoc string
  626.     $sGIF__ASSOCSTRING_INTERNAL &= $hGIFControl & "|" & $pGIF & ";"
  627.     ; All went well. Return Control Identifier
  628.     Return $hGIFControl
  629. EndFunc   ;==>_GUICtrlCreateGIF
  630.  
  631. ; #FUNCTION# ;===============================================================================
  632. ; Name...........: _GUICtrlSetGIF
  633. ; Description ...: Sets the image for a control
  634. ; Syntax.........: _GUICtrlSetGIF($iControld, $vGIF, [$vAdditionalData = Default, [, $iRenderingStyle = Default [, $iForcedARGB = Default]]])
  635. ; Parameters ....: $iControld - The control identifier (controlID)
  636. ;                  $vGIF - filename of the GIF to be loaded
  637. ;                  $vAdditionalData - resurce type, name and language (all separated by semicolon ';') if the GIF is stored as resource inside PE file
  638. ;                                     language is optional. Default is empty string (no additional data).
  639. ;                  $iRenderingStyle - [optional] Advanced optional parameter. Coresponds to style paremeter of ImageList_DrawEx function.Default is ILD_NORMAL for images with defined transparency.
  640. ;                  $iForcedARGB - [optional] Advanced optional parameter. Use with semi-transparent images. Default is 0xFF000000 (opaque black).
  641. ; Return values .: Success - Returns True.
  642. ;                          - Sets @extended to 1 if GIF contains only one frame (no animation)
  643. ;                  Failure - Returns False.
  644. ;                          - Sets @error:
  645. ;                  |1 - if @extended is not set then failure is likely due to invalid input
  646. ;                       if @extended = 1 then Gdip failure occured.
  647. ;                  |2 - Needed memory allocation failed.
  648. ;                  |3 - Memory protection step failed.
  649. ;                  |4 - Some of the functions are not accessible for unknown reasons. Check @extended to determine which one.
  650. ;                  |5 - Couldn't create thread for the animation.
  651. ;                  |6 - Control ID invalid.
  652. ; Author ........: trancexx
  653. ;============================================================================================
  654. Func _GUICtrlSetGIF($iControld, $vGIF, $vAdditionalData = Default, $iRenderingStyle = Default, $iForcedARGB = Default)
  655.     Local $aCtrlPos = WinGetPos(GUICtrlGetHandle($iControld))
  656.     If @error Then Return SetError(6, 0, False)
  657.     If $vAdditionalData = Default Then $vAdditionalData = ""
  658.     If $aCtrlPos[2] = 0 Then $aCtrlPos[2] = Default
  659.     If $aCtrlPos[3] = 0 Then $aCtrlPos[3] = Default
  660.     _GIF_DeleteGIF($iControld, False)
  661.     _GUICtrlCreateGIF($vGIF, $vAdditionalData, $aCtrlPos[0], $aCtrlPos[1], $aCtrlPos[2], $aCtrlPos[3], $iRenderingStyle, $iForcedARGB, $iControld)
  662.     Return SetError(@error, @extended, Not @error)
  663. EndFunc   ;==>_GUICtrlSetGIF
  664.  
  665. ; #INTERNAL FUNCTIONS DOWN BELOW# ;==========================================================
  666. Func _GIF_CloseHandle($hHandle)
  667.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "bool", "CloseHandle", "handle", $hHandle)
  668.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  669.     Return 1
  670. EndFunc   ;==>_GIF_CloseHandle
  671.  
  672. Func _GIF_WaitForSingleObject($hHandle, $iMiliSec = -1)
  673.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "dword", "WaitForSingleObject", "handle", $hHandle, "dword", $iMiliSec)
  674.     If @error Then Return SetError(1, 0, 0)
  675.     Return $aCall[0]
  676. EndFunc   ;==>_GIF_WaitForSingleObject
  677.  
  678. Func _GIF_CreateThread($pAddress)
  679.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "handle", "CreateThread", _
  680.             "ptr", 0, _
  681.             "dword_ptr", 0, _
  682.             "ptr", $pAddress, _
  683.             "ptr", 0, _
  684.             "dword", 0, _
  685.             "dword*", 0)
  686.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  687.     Return $aCall[0]
  688. EndFunc   ;==>_GIF_CreateThread
  689.  
  690. Func _GIF_SuspendThread($hTread)
  691.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "dword", "SuspendThread", "handle", $hTread)
  692.     If @error Or $aCall[0] = -1 Then Return SetError(1, 0, -1)
  693.     Return $aCall[0]
  694. EndFunc   ;==>_GIF_SuspendThread
  695.  
  696. Func _GIF_ResumeThread($hTread)
  697.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "dword", "ResumeThread", "handle", $hTread)
  698.     If @error Or $aCall[0] = -1 Then Return SetError(1, 0, -1)
  699.     Return $aCall[0]
  700. EndFunc   ;==>_GIF_ResumeThread
  701.  
  702. Func _GIF_VirtualProtect($pAddress, $iSize, $iProtection)
  703.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "bool", "VirtualProtect", "ptr", $pAddress, "dword_ptr", $iSize, "dword", $iProtection, "dword*", 0)
  704.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  705.     Return 1
  706. EndFunc   ;==>_GIF_VirtualProtect
  707.  
  708. Func _GIF_GetAddress($hModule, $vFuncName)
  709.     Local $sType = "str"
  710.     If IsNumber($vFuncName) Then $sType = "int" ; if ordinal value passed
  711.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "ptr", "GetProcAddress", "handle", $hModule, $sType, $vFuncName)
  712.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  713.     Return $aCall[0]
  714. EndFunc   ;==>_GIF_GetAddress
  715.  
  716. Func _GIF_GetModuleHandle($vModule = 0)
  717.     Local $sType = "wstr"
  718.     If Not $vModule Then $sType = "ptr"
  719.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "ptr", "GetModuleHandleW", $sType, $vModule)
  720.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  721.     Return $aCall[0]
  722. EndFunc   ;==>_GIF_GetModuleHandle
  723.  
  724. Func _GIF_GetBkColor($hWnd)
  725.     Local $hDC = _GIF_GetDC($hWnd)
  726.     If @error Then Return SetError(1, 0, -1)
  727.     Local $aCall = DllCall($hGIFDLL__GDI32, "dword", "GetBkColor", "handle", $hDC)
  728.     If @error Or $aCall[0] = -1 Then
  729.         _GIF_ReleaseDC($hWnd, $hDC)
  730.         Return SetError(2, 0, -1)
  731.     EndIf
  732.     _GIF_ReleaseDC($hWnd, $hDC)
  733.     Return $aCall[0]
  734. EndFunc   ;==>_GIF_GetBkColor
  735.  
  736. Func _GIF_GetDC($hWnd)
  737.     Local $aCall = DllCall($hGIFDLL__USER32, "handle", "GetDC", "hwnd", $hWnd)
  738.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  739.     Return $aCall[0]
  740. EndFunc   ;==>_GIF_GetDC
  741.  
  742. Func _GIF_ReleaseDC($hWnd, $hDC)
  743.     Local $aCall = DllCall($hGIFDLL__USER32, "int", "ReleaseDC", "hwnd", $hWnd, "handle", $hDC)
  744.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  745.     Return 1
  746. EndFunc   ;==>_GIF_ReleaseDC
  747.  
  748. Func _GIF_InvalidateRect($hWnd, $pRect = 0, $fErase = True)
  749.     Local $aCall = DllCall($hGIFDLL__USER32, "bool", "InvalidateRect", _
  750.             "hwnd", $hWnd, _
  751.             "ptr", $pRect, _
  752.             "bool", $fErase)
  753.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  754.     Return 1
  755. EndFunc   ;==>_GIF_InvalidateRect
  756.  
  757. Func _GIF_ValidateRect($hWnd, $pRect = 0)
  758.     Local $aCall = DllCall($hGIFDLL__USER32, "bool", "ValidateRect", "hwnd", $hWnd, "ptr", $pRect)
  759.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  760.     Return 1
  761. EndFunc   ;==>_GIF_ValidateRect
  762.  
  763. Func _GIF_RedrawWindow($hWnd, $pRect, $hRegion, $iFlags)
  764.     Local $aCall = DllCall($hGIFDLL__USER32, "bool", "RedrawWindow", "hwnd", $hWnd, "ptr", $pRect, "handle", $hRegion, "dword", $iFlags)
  765.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  766.     Return $aCall[0]
  767. EndFunc   ;==>_GIF_RedrawWindow
  768.  
  769. Func _GIF_GetUnderlyingColor($hWnd)
  770.     Local $hParent = _GIF_GetParent($hWnd)
  771.     If @error Then Return SetError(1, 0, -1)
  772.     Local $iColor = _GIF_GetBkColor($hParent)
  773.     Return SetError(@error, 0, $iColor)
  774. EndFunc   ;==>_GIF_GetUnderlyingColor
  775.  
  776. Func _GIF_GetParent($hWnd)
  777.     Local $aCall = DllCall($hGIFDLL__USER32, "hwnd", "GetParent", "hwnd", $hWnd)
  778.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  779.     Return $aCall[0]
  780. EndFunc   ;==>_GIF_GetParent
  781.  
  782. Func _GIF_Create_pGIF($bBinary, ByRef $iWidth, ByRef $iHeight, ByRef $hGIFControl, $iLeft = 0, $iTop = 0, $iARGB = Default)
  783.     If $iARGB = Default Then $iARGB = 0xFF000000
  784.     Local $hGDIP ; Gdip engine
  785.     Local $hMemGlobal ; Memory
  786.     ; Bitmap object
  787.     Local $pBitmap, $iWidthReal, $iHeightReal
  788.     If IsBinary($bBinary) Then
  789.         $pBitmap = _GIF_CreateBitmapFromBinaryImage($hGDIP, $hMemGlobal, $bBinary, $iWidthReal, $iHeightReal)
  790.     Else
  791.         $pBitmap = _GIF_CreateBitmapFromFile($hGDIP, $bBinary, $iWidthReal, $iHeightReal)
  792.     EndIf
  793.     If @error Then
  794.         Local $iErr = @error
  795.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  796.         Return SetError(1, $iErr, 0)
  797.     EndIf
  798.     Local $fDoResize
  799.     If $iWidth = Default Then
  800.         $iWidth = $iWidthReal
  801.     Else
  802.         $fDoResize = True
  803.     EndIf
  804.     If $iHeight = Default Then
  805.         $iHeight = $iHeightReal
  806.     Else
  807.         $fDoResize = True
  808.     EndIf
  809.     ; Get number of frame dimensions
  810.     Local $iFrameDimensionsCount = _GIF_GdipImageGetFrameDimensionsCount($pBitmap)
  811.     If @error Then
  812.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  813.         Return SetError(2, 0, 0)
  814.     EndIf
  815.     ; GUID
  816.     Local $tGUID = DllStructCreate("dword;word;word;byte[8]")
  817.     Local $pGUID = DllStructGetPtr($tGUID)
  818.     ; Get the identifiers for the frame dimensions
  819.     _GIF_GdipImageGetFrameDimensionsList($pBitmap, $pGUID, $iFrameDimensionsCount)
  820.     If @error Then
  821.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  822.         Return SetError(3, 0, 0)
  823.     EndIf
  824.     ; Number of frames
  825.     Local $iFrameCount = _GIF_GdipImageGetFrameCount($pBitmap, $pGUID)
  826.     If @error Then
  827.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  828.         Return SetError(4, 0, 0)
  829.     EndIf
  830.     ; Allocate needed global memory
  831.     Local $pGIF = _GIF_MemGlobalAlloc(4 * (8 + 4 * @AutoItX64 + $iFrameCount), 64) ; GPTR
  832.     If @error Then
  833.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  834.         Return SetError(3, 0, 0)
  835.     EndIf
  836.     Local $tGIF = DllStructCreate("handle GIFThread;" & _
  837.             "ptr CodeBuffer;" & _
  838.             "hwnd ControlHandle;" & _
  839.             "handle ImageList;" & _
  840.             "bool ExitFlag;" & _
  841.             "bool Transparent;" & _
  842.             "dword CurrentFrame;" & _
  843.             "dword NumberOfFrames;" & _
  844.             "dword FrameDelay[" & $iFrameCount & "];", _
  845.             $pGIF)
  846.     DllStructSetData($tGIF, "GIFThread", 0)
  847.     DllStructSetData($tGIF, "ControlHandle", 0)
  848.     DllStructSetData($tGIF, "ExitFlag", 0)
  849.     DllStructSetData($tGIF, "CurrentFrame", 0)
  850.     ; Set number of frames
  851.     DllStructSetData($tGIF, "NumberOfFrames", $iFrameCount)
  852.     ; Make GIF Control
  853.     Local $fNewControl = False
  854.     If Not $hGIFControl Then
  855.         $fNewControl = True
  856.         $hGIFControl = GUICtrlCreatePic("", $iLeft, $iTop, $iWidth, $iHeight)
  857.     EndIf
  858.     ; If frame count is 1 then free what needs to be freed and return $pGIF. This requires no animation.
  859.     If $iFrameCount = 1 Then
  860.         Local $hGIFBitmap = _GIF_GdipCreateHBITMAPFromBitmap($pBitmap, $iARGB)
  861.         If $fDoResize Then _GIF_ResizeBitmap($hGIFBitmap, $iWidth, $iHeight)
  862.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  863.         ; Render default image
  864.         _GIF_DeleteObject(GUICtrlSendMsg($hGIFControl, 370, 0, $hGIFBitmap)) ; STM_SETIMAGE
  865.         _GIF_DeleteObject($hGIFBitmap)
  866.         Return $pGIF
  867.     EndIf
  868.     ; Make ImageList
  869.     Local $hImageList = _GIF_ImageList_Create($iWidth, $iHeight, 32, $iFrameCount) ; ILC_COLOR32
  870.     If @error Then
  871.         If $fNewControl Then GUICtrlDelete($hGIFControl)
  872.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal, $pGIF)
  873.         Return SetError(4, 0, 0)
  874.     EndIf
  875.     ; Set imagelist
  876.     DllStructSetData($tGIF, "ImageList", $hImageList)
  877.     Local $hBitmap
  878.     For $j = 0 To $iFrameCount - 1 ; for all frames
  879.         ; Select the frame
  880.         _GIF_GdipImageSelectActiveFrame($pBitmap, $pGUID, $j)
  881.         If @error Then ContinueLoop
  882.         ; Fill the array with HBITMAPs
  883.         $hBitmap = _GIF_GdipCreateHBITMAPFromBitmap($pBitmap, $iARGB)
  884.         If $fDoResize Then _GIF_ResizeBitmap($hBitmap, $iWidth, $iHeight)
  885.         ; Add it to the ImageList
  886.         _GIF_ImageList_Add($hImageList, $hBitmap)
  887.         If $j = 0 Then ; Export "static" GIF (first frame)
  888.             ; Render default image
  889.             _GIF_DeleteObject(GUICtrlSendMsg($hGIFControl, 370, 0, $hBitmap)) ; STM_SETIMAGE
  890.             _GIF_DeleteObject($hBitmap)
  891.         EndIf
  892.         ; Delete it
  893.         _GIF_DeleteObject($hBitmap)
  894.     Next
  895.     ; Get size of PropertyTagFrameDelay
  896.     Local $iPropertyItemSize = _GIF_GdipGetPropertyItemSize($pBitmap, 0x5100) ; PropertyTagFrameDelay
  897.     If @error Then
  898.         If $fNewControl Then GUICtrlDelete($hGIFControl)
  899.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal, $pGIF)
  900.         Return SetError(5, 0, 0)
  901.     EndIf
  902.     ; Raw structure for the call
  903.     Local $tRawPropItem = DllStructCreate("byte[" & $iPropertyItemSize & "]")
  904.     ; Fill the structure
  905.     _GIF_GdipGetPropertyItem($pBitmap, 0x5100, $iPropertyItemSize, DllStructGetPtr($tRawPropItem)) ; PropertyTagFrameDelay
  906.     If @error Then
  907.         If $fNewControl Then GUICtrlDelete($hGIFControl)
  908.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal, $pGIF)
  909.         Return SetError(6, 0, 0)
  910.     EndIf
  911.     ; Formatted structure in place of the raw
  912.     Local $tPropItem = DllStructCreate("int Id;" & _
  913.             "dword Length;" & _
  914.             "word Type;" & _
  915.             "ptr Value", _
  916.             DllStructGetPtr($tRawPropItem))
  917.     ; Read the "Length"
  918.     Local $iSize = DllStructGetData($tPropItem, "Length") / 4 ; 'Delay Time' is dword type. 4 is size of dword.
  919.     ; Make new structure for the data
  920.     Local $tPropertyData = DllStructCreate("dword[" & $iSize & "]", DllStructGetData($tPropItem, "Value"))
  921.     ; Read and fill array
  922.     Local $iDelay
  923.     For $j = 1 To $iFrameCount ; loop through all frames
  924.         $iDelay = DllStructGetData($tPropertyData, 1, $j) * 10 ; 1 = 10 msec
  925.         ; Corrections
  926.         If Not $iDelay Then $iDelay = 130 ; interpreting 0 as 130 ms
  927.         If $iDelay < 50 Then $iDelay = 50 ; will slow it down to prevent more extensive cpu usage
  928.         ; Set delay
  929.         DllStructSetData($tGIF, "FrameDelay", $iDelay, $j)
  930.     Next
  931.     ; Determining trancparency
  932.     Local $fTransparent = True ; preset
  933.     ; Get pixel color
  934.     Local $iPixelColor = _GIF_GdipBitmapGetPixel($pBitmap, 0, 0) ; upper left pixel
  935.     ; Color is ARGB. If A is set then it's not transparent
  936.     If BitShift($iPixelColor, 24) Then $fTransparent = False
  937.     ; Set transparency
  938.     DllStructSetData($tGIF, "Transparent", $fTransparent)
  939.     ; Free
  940.     _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  941.     ; Return sucess (pointer to allocated memory)
  942.     Return $pGIF
  943. EndFunc   ;==>_GIF_Create_pGIF
  944.  
  945. Func _GIF_CreateStreamOnHGlobal($hGlobal, $iFlag = 1)
  946.     Local $aCall = DllCall($hGIFDLL__OLE32, "long", "CreateStreamOnHGlobal", "handle", $hGlobal, "int", $iFlag, "ptr*", 0)
  947.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  948.     Return $aCall[3]
  949. EndFunc   ;==>_GIF_CreateStreamOnHGlobal
  950.  
  951. Func _GIF_GetObject($hObject, $iSize, $pObject)
  952.     Local $aCall = DllCall($hGIFDLL__GDI32, "int", "GetObject", "handle", $hObject, "int", $iSize, "ptr", $pObject)
  953.     If @error Then Return SetError(1, 0, 0)
  954.     Return $aCall[0]
  955. EndFunc   ;==>_GIF_GetObject
  956.  
  957. Func _GIF_DeleteObject($hObject)
  958.     Local $aCall = DllCall($hGIFDLL__GDI32, "bool", "DeleteObject", "handle", $hObject)
  959.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  960.     Return 1
  961. EndFunc   ;==>_GIF_DeleteObject
  962.  
  963. Func _GIF_ImageList_GetIconSize($hImageList)
  964.     Local $aOut[2] = [0, 0]
  965.     Local $aCall = DllCall($hGIFDLL__COMCTL32, "bool", "ImageList_GetIconSize", "handle", $hImageList, "int*", 0, "int*", 0)
  966.     If @error Or Not $aCall[0] Then Return SetError(1, 0, $aOut)
  967.     $aOut[0] = $aCall[2]
  968.     $aOut[1] = $aCall[3]
  969.     Return $aOut
  970. EndFunc   ;==>_GIF_ImageList_GetIconSize
  971.  
  972. Func _GIF_ImageList_Create($iWidth, $iHeight, $iFlag, $iInitial, $iGrow = 0)
  973.     Local $aCall = DllCall($hGIFDLL__COMCTL32, "handle", "ImageList_Create", _
  974.             "int", $iWidth, _
  975.             "int", $iHeight, _
  976.             "dword", $iFlag, _
  977.             "int", $iInitial, _
  978.             "int", $iGrow)
  979.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  980.     Return $aCall[0]
  981. EndFunc   ;==>_GIF_ImageList_Create
  982.  
  983. Func _GIF_ImageList_Add($hImageList, $hBitmap)
  984.     Local $aCall = DllCall($hGIFDLL__COMCTL32, "int", "ImageList_Add", _
  985.             "handle", $hImageList, _
  986.             "handle", $hBitmap, _
  987.             "handle", 0)
  988.     If @error Or $aCall[0] = -1 Then Return SetError(1, 0, 0)
  989.     Return $aCall[0]
  990. EndFunc   ;==>_GIF_ImageList_Add
  991.  
  992. Func _GIF_ImageList_Destroy($hImageList)
  993.     Local $aCall = DllCall($hGIFDLL__COMCTL32, "bool", "ImageList_Destroy", "handle", $hImageList)
  994.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  995.     Return 1
  996. EndFunc   ;==>_GIF_ImageList_Destroy
  997.  
  998. Func _GIF_ImageList_DrawEx($hImageList, $iImageIndex, $hDC, $iXPos = 0, $iYPos = 0, $iWidth = 0, $iHeight = 0, $iBkColRef = -1, $iFgColRef = -1, $iStyle = 0)
  999.     Local $aCall = DllCall($hGIFDLL__COMCTL32, "bool", "ImageList_DrawEx", _
  1000.             "handle", $hImageList, _
  1001.             "int", $iImageIndex, _
  1002.             "handle", $hDC, _
  1003.             "int", $iXPos, _
  1004.             "int", $iYPos, _
  1005.             "int", $iWidth, _
  1006.             "int", $iHeight, _
  1007.             "dword", $iBkColRef, _
  1008.             "dword", $iFgColRef, _
  1009.             "dword", $iStyle)
  1010.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1011.     Return 1
  1012. EndFunc   ;==>_GIF_ImageList_DrawEx
  1013.  
  1014. Func _GIF_CreateBitmapFromFile(ByRef $hGDIP, $sFile, ByRef $iWidth, ByRef $iHeight)
  1015.     ; Initialize Gdip
  1016.     $hGDIP = _GIF_GdiplusStartup()
  1017.     If @error Then Return SetError(1, 0, 0)
  1018.     ; Create bitmap object out of the file
  1019.     Local $pBitmap = _GIF_GdipLoadImageFromFile($sFile)
  1020.     If @error Then
  1021.         _GIF_GdiplusShutdown($hGDIP)
  1022.         Return SetError(2, 0, 0)
  1023.     EndIf
  1024.     ; Get dimension
  1025.     _GIF_GdipGetImageDimension($pBitmap, $iWidth, $iHeight)
  1026.     If @error Then
  1027.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP)
  1028.         Return SetError(3, 0, 0)
  1029.     EndIf
  1030.     Return $pBitmap
  1031. EndFunc   ;==>_GIF_CreateBitmapFromFile
  1032.  
  1033. Func _GIF_CreateBitmapFromBinaryImage(ByRef $hGDIP, ByRef $hMemGlobal, $bBinary, ByRef $iWidth, ByRef $iHeight)
  1034.     $bBinary = Binary($bBinary)
  1035.     ; Determine the size of binary data
  1036.     Local $iSize = BinaryLen($bBinary)
  1037.     ; Allocate global moveable memory in requred size
  1038.     $hMemGlobal = _GIF_MemGlobalAlloc($iSize, 2); GMEM_MOVEABLE
  1039.     If @error Then Return SetError(1, 0, 0)
  1040.     ; Get pointer to it
  1041.     Local $pMemory = _GIF_MemGlobalLock($hMemGlobal)
  1042.     If @error Then
  1043.         _GIF_MemGlobalFree($hMemGlobal)
  1044.         Return SetError(2, 0, 0)
  1045.     EndIf
  1046.     ; Make structure at that address
  1047.     Local $tBinary = DllStructCreate("byte[" & $iSize & "]", $pMemory)
  1048.     ; Fill it
  1049.     DllStructSetData($tBinary, 1, $bBinary)
  1050.     ; Create stream
  1051.     Local $pStream = _GIF_CreateStreamOnHGlobal($pMemory, 0)
  1052.     If @error Then
  1053.         _GIF_MemGlobalFree($hMemGlobal)
  1054.         Return SetError(3, 0, 0)
  1055.     EndIf
  1056.     ; Unlock memory (almost irrelevant)
  1057.     _GIF_MemGlobalUnlock($pMemory)
  1058.     ; Initialize Gdip
  1059.     $hGDIP = _GIF_GdiplusStartup()
  1060.     If @error Then
  1061.         _GIF_MemGlobalFree($hMemGlobal)
  1062.         Return SetError(4, 0, 0)
  1063.     EndIf
  1064.     ; Create bitmap object out of the stream
  1065.     Local $pBitmap = _GIF_GdipCreateBitmapFromStream($pStream)
  1066.     If @error Then
  1067.         _GIF_GdiplusShutdown($hGDIP)
  1068.         _GIF_MemGlobalFree($hMemGlobal)
  1069.         Return SetError(5, 0, 0)
  1070.     EndIf
  1071.     ; Get dimension
  1072.     _GIF_GdipGetImageDimension($pBitmap, $iWidth, $iHeight)
  1073.     If @error Then
  1074.         _GIF_FreeGdipAndMem($pBitmap, $hGDIP, $hMemGlobal)
  1075.         Return SetError(6, 0, 0)
  1076.     EndIf
  1077.     ; Call Release on stream object
  1078.     DllCallAddress("dword", DllStructGetData(DllStructCreate("ptr QueryInterface; ptr AddRef; ptr Release;", DllStructGetData(DllStructCreate("ptr pObj;", $pStream), "pObj")), "Release"), "ptr", $pStream)
  1079.     ; Return success
  1080.     Return $pBitmap
  1081. EndFunc   ;==>_GIF_CreateBitmapFromBinaryImage
  1082.  
  1083. Func _GIF_ResizeBitmap(ByRef $hBitmap, $iNewWidth, $iNewHeight)
  1084.     ; First to get available data for the bitmap
  1085.     Local $tBMP = DllStructCreate("long Type;long Width;long Height;long WidthBytes;word Planes;word BitsPixel;ptr Bits;")
  1086.     _GIF_GetObject($hBitmap, DllStructGetSize($tBMP), DllStructGetPtr($tBMP))
  1087.     ; Create bitmap object out of binary
  1088.     Local $pBitmap = _GIF_GdipCreateBitmapFromScan0(DllStructGetData($tBMP, "Width"), DllStructGetData($tBMP, "Height"), DllStructGetData($tBMP, "WidthBytes"), 0x26200A, DllStructGetData($tBMP, "Bits"))
  1089.     _GIF_GdipImageRotateFlip($pBitmap, 6) ; GdipCreateBitmapFromScan0 creates top-to-down bitmap and bmBits is the other way around, flip vertically therefore
  1090.     ; New empty bitmap in desired dimension
  1091.     Local $pNewBitmap = _GIF_GdipCreateBitmapFromScan0($iNewWidth, $iNewHeight)
  1092.     Local $hGraphics = _GIF_GdipGetImageGraphicsContext($pNewBitmap)
  1093.     ; Draw the "old" bitmap here
  1094.     _GIF_GdipDrawImageRect($hGraphics, $pBitmap, 0, 0, $iNewWidth, $iNewHeight)
  1095.     ; Create bitmap out of new bitmap object
  1096.     Local $hNewBitmap = _GIF_GdipCreateHBITMAPFromBitmap($pNewBitmap)
  1097.     ; Clean
  1098.     _GIF_GdipDisposeImage($pBitmap)
  1099.     _GIF_GdipDeleteGraphics($hGraphics)
  1100.     _GIF_DeleteObject($hBitmap)
  1101.     _GIF_GdipDisposeImage($pNewBitmap)
  1102.     ; Replace old with new
  1103.     $hBitmap = $hNewBitmap
  1104.     ; That's it!
  1105.     Return 1
  1106. EndFunc   ;==>_GIF_ResizeBitmap
  1107.  
  1108. Func _GIF_GdiplusStartup()
  1109.     Local $tGdiplusStartupInput = DllStructCreate("dword GdiplusVersion;" & _
  1110.             "ptr DebugEventCallback;" & _
  1111.             "int SuppressBackgroundThread;" & _
  1112.             "int SuppressExternalCodecs")
  1113.     DllStructSetData($tGdiplusStartupInput, "GdiplusVersion", 1)
  1114.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdiplusStartup", "dword_ptr*", 0, "ptr", DllStructGetPtr($tGdiplusStartupInput), "ptr", 0)
  1115.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1116.     Return $aCall[1]
  1117. EndFunc   ;==>_GIF_GdiplusStartup
  1118.  
  1119. Func _GIF_GdiplusShutdown($hGDIP)
  1120.     DllCall($hGIFDLL__GDIPLUS, "none", "GdiplusShutdown", "dword_ptr", $hGDIP)
  1121. EndFunc   ;==>_GIF_GdiplusShutdown
  1122.  
  1123. Func _GIF_GdipDisposeImage($hImage)
  1124.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipDisposeImage", "handle", $hImage)
  1125.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1126.     Return 1
  1127. EndFunc   ;==>_GIF_GdipDisposeImage
  1128.  
  1129. Func _GIF_GdipGetImageDimension($pBitmap, ByRef $iWidth, ByRef $iHeight)
  1130.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipGetImageDimension", "ptr", $pBitmap, "float*", 0, "float*", 0)
  1131.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1132.     $iWidth = $aCall[2]
  1133.     $iHeight = $aCall[3]
  1134. EndFunc   ;==>_GIF_GdipGetImageDimension
  1135.  
  1136. Func _GIF_GdipImageGetFrameDimensionsCount($pBitmap)
  1137.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipImageGetFrameDimensionsCount", "ptr", $pBitmap, "dword*", 0)
  1138.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1139.     Return $aCall[2]
  1140. EndFunc   ;==>_GIF_GdipImageGetFrameDimensionsCount
  1141.  
  1142. Func _GIF_GdipImageGetFrameDimensionsList($pBitmap, $pGUID, $iFrameDimensionsCount)
  1143.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipImageGetFrameDimensionsList", _
  1144.             "ptr", $pBitmap, _
  1145.             "ptr", $pGUID, _
  1146.             "dword", $iFrameDimensionsCount)
  1147.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1148.     Return 1
  1149. EndFunc   ;==>_GIF_GdipImageGetFrameDimensionsList
  1150.  
  1151. Func _GIF_GdipImageGetFrameCount($pBitmap, $pGUID)
  1152.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipImageGetFrameCount", _
  1153.             "ptr", $pBitmap, _
  1154.             "ptr", $pGUID, _
  1155.             "dword*", 0)
  1156.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1157.     Return $aCall[3]
  1158. EndFunc   ;==>_GIF_GdipImageGetFrameCount
  1159.  
  1160. Func _GIF_GdipImageSelectActiveFrame($pBitmap, $pGUID, $iFrameIndex)
  1161.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipImageSelectActiveFrame", _
  1162.             "ptr", $pBitmap, _
  1163.             "ptr", $pGUID, _
  1164.             "dword", $iFrameIndex)
  1165.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1166.     Return 1
  1167. EndFunc   ;==>_GIF_GdipImageSelectActiveFrame
  1168.  
  1169. Func _GIF_GdipGetPropertyItemSize($pBitmap, $iPropID)
  1170.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipGetPropertyItemSize", _
  1171.             "ptr", $pBitmap, _
  1172.             "ptr", $iPropID, _
  1173.             "dword*", 0)
  1174.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1175.     Return $aCall[3]
  1176. EndFunc   ;==>_GIF_GdipGetPropertyItemSize
  1177.  
  1178. Func _GIF_GdipGetPropertyItem($pBitmap, $iPropID, $iSize, $pBuffer)
  1179.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipGetPropertyItem", _
  1180.             "ptr", $pBitmap, _
  1181.             "dword", $iPropID, _
  1182.             "dword", $iSize, _
  1183.             "ptr", $pBuffer)
  1184.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1185.     Return 1
  1186. EndFunc   ;==>_GIF_GdipGetPropertyItem
  1187.  
  1188. Func _GIF_GdipBitmapGetPixel($pBitmap, $iX, $iY)
  1189.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipBitmapGetPixel", _
  1190.             "ptr", $pBitmap, _
  1191.             "int", $iX, _
  1192.             "int", $iY, _
  1193.             "dword*", 0)
  1194.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1195.     Return $aCall[4]
  1196. EndFunc   ;==>_GIF_GdipBitmapGetPixel
  1197.  
  1198. Func _GIF_GdipLoadImageFromFile($sFile)
  1199.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipLoadImageFromFile", "wstr", $sFile, "ptr*", 0)
  1200.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1201.     Return $aCall[2]
  1202. EndFunc   ;==>_GIF_GdipLoadImageFromFile
  1203.  
  1204. Func _GIF_GdipCreateBitmapFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x26200A, $pScan0 = 0)
  1205.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "dword", $iPixelFormat, "ptr", $pScan0, "ptr*", 0)
  1206.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1207.     Return $aCall[6]
  1208. EndFunc   ;==>_GIF_GdipCreateBitmapFromScan0
  1209.  
  1210. Func _GIF_GdipCreateBitmapFromStream($pStream)
  1211.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipCreateBitmapFromStream", "ptr", $pStream, "ptr*", 0)
  1212.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1213.     Return $aCall[2]
  1214. EndFunc   ;==>_GIF_GdipCreateBitmapFromStream
  1215.  
  1216. Func _GIF_GdipCreateHBITMAPFromBitmap($pBitmap, $iARGB = 0xFF000000)
  1217.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipCreateHBITMAPFromBitmap", "ptr", $pBitmap, "handle*", 0, "dword", $iARGB)
  1218.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1219.     Return $aCall[2]
  1220. EndFunc   ;==>_GIF_GdipCreateHBITMAPFromBitmap
  1221.  
  1222. Func _GIF_GdipGetImageGraphicsContext($hImage)
  1223.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipGetImageGraphicsContext", "ptr", $hImage, "ptr*", 0)
  1224.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1225.     Return $aCall[2]
  1226. EndFunc   ;==>_GIF_GdipGetImageGraphicsContext
  1227.  
  1228. Func _GIF_GdipDrawImageRect($hGraphics, $hImage, $iX, $iY, $iWidth, $iHeight)
  1229.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipDrawImageRectI", "ptr", $hGraphics, "ptr", $hImage, "int", $iX, "int", $iY, "int", $iWidth, "int", $iHeight)
  1230.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1231.     Return 1
  1232. EndFunc   ;==>_GIF_GdipDrawImageRect
  1233.  
  1234. Func _GIF_GdipDeleteGraphics($hGraphics)
  1235.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipDeleteGraphics", "handle", $hGraphics)
  1236.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1237.     Return 1
  1238. EndFunc   ;==>_GIF_GdipDeleteGraphics
  1239.  
  1240. Func _GIF_GdipImageRotateFlip($hImage, $iType)
  1241.     Local $aCall = DllCall($hGIFDLL__GDIPLUS, "dword", "GdipImageRotateFlip", "handle", $hImage, "dword", $iType)
  1242.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1243.     Return 1
  1244. EndFunc   ;==>_GIF_GdipImageRotateFlip
  1245.  
  1246. Func _GIF_FreeGdipAndMem($pBitmap = 0, $hGDIP = 0, $hMem = 0, $pGIF = 0)
  1247.     If $pBitmap Then _GIF_GdipDisposeImage($pBitmap)
  1248.     If $hGDIP Then _GIF_GdiplusShutdown($hGDIP)
  1249.     If $hMem Then _GIF_MemGlobalFree($hMem)
  1250.     If $pGIF Then _GIF_MemGlobalFree($pGIF)
  1251. EndFunc   ;==>_GIF_FreeGdipAndMem
  1252.  
  1253. Func _GIF_MemGlobalAlloc($iSize, $iFlag)
  1254.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "handle", "GlobalAlloc", "dword", $iFlag, "dword_ptr", $iSize)
  1255.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1256.     Return $aCall[0]
  1257. EndFunc   ;==>_GIF_MemGlobalAlloc
  1258.  
  1259. Func _GIF_MemGlobalFree($hMem)
  1260.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "ptr", "GlobalFree", "handle", $hMem)
  1261.     If @error Or $aCall[0] Then Return SetError(1, 0, 0)
  1262.     Return 1
  1263. EndFunc   ;==>_GIF_MemGlobalFree
  1264.  
  1265. Func _GIF_MemGlobalLock($hMem)
  1266.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "ptr", "GlobalLock", "handle", $hMem)
  1267.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1268.     Return $aCall[0]
  1269. EndFunc   ;==>_GIF_MemGlobalLock
  1270.  
  1271. Func _GIF_MemGlobalUnlock($hMem)
  1272.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "bool", "GlobalUnlock", "handle", $hMem)
  1273.     If @error Then Return SetError(1, 0, 0)
  1274.     If $aCall[0] Or _GIF_GetLastError() Then Return $aCall[0]
  1275.     Return 1
  1276. EndFunc   ;==>_GIF_MemGlobalUnlock
  1277.  
  1278. Func _GIF_GetLastError()
  1279.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "dword", "GetLastError")
  1280.     If @error Then Return SetError(1, 0, -1)
  1281.     Return $aCall[0]
  1282. EndFunc   ;==>_GIF_GetLastError
  1283.  
  1284. Func _GIF_FindResourceEx($hModule, $vResType, $vResName, $iResLang = 0)
  1285.     Local $sTypeType = "wstr"
  1286.     If $vResType == Number($vResType) Then $sTypeType = "int"
  1287.     Local $sNameType = "wstr"
  1288.     If $vResName == Number($vResName) Then $sNameType = "int"
  1289.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "handle", "FindResourceExW", _
  1290.             "handle", $hModule, _
  1291.             $sTypeType, $vResType, _
  1292.             $sNameType, $vResName, _
  1293.             "int", $iResLang)
  1294.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1295.     Return $aCall[0]
  1296. EndFunc   ;==>_GIF_FindResourceEx
  1297.  
  1298. Func _GIF_SizeofResource($hModule, $hResource)
  1299.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "int", "SizeofResource", "handle", $hModule, "handle", $hResource)
  1300.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1301.     Return $aCall[0]
  1302. EndFunc   ;==>_GIF_SizeofResource
  1303.  
  1304. Func _GIF_LoadResource($hModule, $hResource)
  1305.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "handle", "LoadResource", "handle", $hModule, "handle", $hResource)
  1306.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1307.     Return $aCall[0]
  1308. EndFunc   ;==>_GIF_LoadResource
  1309.  
  1310. Func _GIF_LockResource($hResource)
  1311.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "ptr", "LockResource", "handle", $hResource)
  1312.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1313.     Return $aCall[0]
  1314. EndFunc   ;==>_GIF_LockResource
  1315.  
  1316. Func _GIF_LoadLibraryEx($sModule, $iFlag = 0)
  1317.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "handle", "LoadLibraryExW", "wstr", $sModule, "handle", 0, "dword", $iFlag)
  1318.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1319.     Return $aCall[0]
  1320. EndFunc   ;==>_GIF_LoadLibraryEx
  1321.  
  1322. Func _GIF_FreeLibrary($hModule)
  1323.     Local $aCall = DllCall($hGIFDLL__KERNEL32, "bool", "FreeLibrary", "handle", $hModule)
  1324.     If @error Or Not $aCall[0] Then Return SetError(1, 0, 0)
  1325.     Return $aCall[0]
  1326. EndFunc   ;==>_GIF_FreeLibrary
  1327.  
  1328. Func _GIF_ResourceGetAsRaw($sModule, $vResType, $vResName, $iResLang = 0)
  1329.     ; Load the module
  1330.     Local $hModule = _GIF_LoadLibraryEx($sModule, 2) ; LOAD_LIBRARY_AS_DATAFILE
  1331.     If @error Then Return SetError(1, 0, "")
  1332.     ; Find specified resource inside it
  1333.     Local $hResource = _GIF_FindResourceEx($hModule, $vResType, $vResName, $iResLang)
  1334.     If @error Then
  1335.         _GIF_FreeLibrary($hModule)
  1336.         Return SetError(2, 0, "")
  1337.     EndIf
  1338.     ; Determine the size of the resource
  1339.     Local $iSizeOfResource = _GIF_SizeofResource($hModule, $hResource)
  1340.     If @error Then
  1341.         _GIF_FreeLibrary($hModule)
  1342.         Return SetError(3, 0, "")
  1343.     EndIf
  1344.     ; Load it
  1345.     $hResource = _GIF_LoadResource($hModule, $hResource)
  1346.     If @error Then
  1347.         _GIF_FreeLibrary($hModule)
  1348.         Return SetError(4, 0, "")
  1349.     EndIf
  1350.     ; Get pointer
  1351.     Local $pResource = _GIF_LockResource($hResource)
  1352.     If @error Then
  1353.         _GIF_FreeLibrary($hModule)
  1354.         Return SetError(5, 0, "")
  1355.     EndIf
  1356.     ; Make structure at that address
  1357.     Local $tBinary = DllStructCreate("byte[" & $iSizeOfResource & "]", $pResource)
  1358.     ; Collect data (this should be done before freeing the module)
  1359.     Local $bBinary = DllStructGetData($tBinary, 1)
  1360.     ; Free
  1361.     _GIF_FreeLibrary($hModule)
  1362.     ; Return data
  1363.     Return $bBinary
  1364. EndFunc   ;==>_GIF_ResourceGetAsRaw
  1365.  
  1366. Func _GIF_MakeBitmapFromRT_BITMAP($bBinary)
  1367.     Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
  1368.     DllStructSetData($tBinary, 1, $bBinary)
  1369.     ; First dword is the size of the bitmap header
  1370.     Local $iHeaderSize = DllStructGetData(DllStructCreate("dword HeaderSize", DllStructGetPtr($tBinary)), "HeaderSize")
  1371.     Local $tBitmap, $iMultiplier
  1372.     ; Check the size of the header (different formats)
  1373.     Switch $iHeaderSize
  1374.         Case 40
  1375.             $tBitmap = DllStructCreate("dword HeaderSize;" & _
  1376.                     "dword Width;" & _
  1377.                     "dword Height;" & _
  1378.                     "word Planes;" & _
  1379.                     "word BitPerPixel;" & _
  1380.                     "dword CompressionMethod;" & _
  1381.                     "dword Size;" & _
  1382.                     "dword Hresolution;" & _
  1383.                     "dword Vresolution;" & _
  1384.                     "dword Colors;" & _
  1385.                     "dword ImportantColors", _
  1386.                     DllStructGetPtr($tBinary))
  1387.             $iMultiplier = 4
  1388.         Case 12
  1389.             $tBitmap = DllStructCreate("dword HeaderSize;" & _
  1390.                     "word Width;" & _
  1391.                     "word Height;" & _
  1392.                     "word Planes;" & _
  1393.                     "word BitPerPixel", _
  1394.                     DllStructGetPtr($tBinary))
  1395.             $iMultiplier = 3
  1396.         Case Else
  1397.             Return SetError(1, 0, 0) ; unsupported format
  1398.     EndSwitch
  1399.     Local $iExponent = DllStructGetData($tBitmap, "BitPerPixel")
  1400.     ; Construct the new BMP
  1401.     Local $tDIB = DllStructCreate("align 2;char Identifier[2];" & _
  1402.             "dword BitmapSize;" & _
  1403.             "short;" & _
  1404.             "short;" & _
  1405.             "dword BitmapOffset;" & _
  1406.             "byte Body[" & BinaryLen($bBinary) & "]")
  1407.     ; Fill the structure
  1408.     DllStructSetData($tDIB, "Identifier", "BM")
  1409.     DllStructSetData($tDIB, "BitmapSize", BinaryLen($bBinary) + 14)
  1410.     ; Size of the bitmap. This data can be read from the read data, but somtimes it's not there and must be calculated.
  1411.     Local $iRawBitmapSize = DllStructGetData($tBitmap, "Size")
  1412.     If $iRawBitmapSize Then ; data is found
  1413.         DllStructSetData($tDIB, "BitmapOffset", BinaryLen($bBinary) - $iRawBitmapSize + 14)
  1414.     Else ; complication
  1415.         If $iExponent = 24 Then ; 24 bit per pixel is special case
  1416.             DllStructSetData($tDIB, "BitmapOffset", $iHeaderSize + 14)
  1417.         Else ; this is pure math
  1418.             Local $iWidth = DllStructGetData($tBitmap, "Width")
  1419.             Local $iHeight = DllStructGetData($tBitmap, "Height")
  1420.             $iRawBitmapSize = 4 * Floor(($iWidth * $iExponent + 31) / 32) * $iHeight ; boundary
  1421.             Local $iOffset1 = BinaryLen($bBinary) - $iRawBitmapSize + 14
  1422.             Local $iOffset2 = 2 ^ $iExponent * $iMultiplier + $iHeaderSize + 14
  1423.             ; Get correct offset
  1424.             If $iOffset2 < $iOffset1 Then
  1425.                 DllStructSetData($tDIB, "BitmapOffset", $iOffset2)
  1426.             Else
  1427.                 DllStructSetData($tDIB, "BitmapOffset", $iOffset1 - 2)
  1428.             EndIf
  1429.         EndIf
  1430.     EndIf
  1431.     ; Set actual bitmap data
  1432.     DllStructSetData($tDIB, "Body", $bBinary)
  1433.     ; Return the BMP data
  1434.     Return DllStructGetData(DllStructCreate("byte[" & DllStructGetSize($tDIB) & "]", DllStructGetPtr($tDIB)), 1)
  1435. EndFunc   ;==>_GIF_MakeBitmapFromRT_BITMAP
  1436.  
  1437. Func _GIF_SwapEndian($iValue, $iSize = 0)
  1438.     If $iSize Then
  1439.         Local $sPadd = "00000000"
  1440.         Return Hex(BinaryMid($iValue, 1, $iSize)) & StringLeft($sPadd, 2 * ($iSize - BinaryLen($iValue)))
  1441.     EndIf
  1442.     Return Hex(Binary($iValue))
  1443. EndFunc   ;==>_GIF_SwapEndian
  1444.  
  1445. Func _GIF_GetGIFAssoc($iGIFId)
  1446.     Local $aArray = StringRegExp($sGIF__ASSOCSTRING_INTERNAL, "(?i);" & $iGIFId & "\|(.*?);", 3)
  1447.     If @error Then Return 0
  1448.     Return Ptr($aArray[0])
  1449. EndFunc   ;==>_GIF_GetGIFAssoc
  1450. ;============================================================================================
Add Comment
Please, Sign In to add comment