#include <GDIPlus.au3>
#include <WinAPIEx.au3>
#Include <APIConstants.au3>
#Include <WindowsConstants.au3>
#include <Misc.au3>
$hForm = GUICreate('Battle sity', 800, 600, -1, -1, BitOR($WS_POPUP, $WS_CLIPSIBLINGS))
GUISetBkColor(0x5EBC36)
$iTank = GUICtrlCreatePic('', 20, 20, 32, 32)
__Object_SetImage(GUICtrlGetHandle($iTank), @ScriptDir & '\tank.png', 32, 32)
GUISetState()
While 1
ConsoleWrite('Track state: ' & __Object_Track($hForm, $iTank) & @CRLF)
Switch GUIGetMsg()
Case -3
Exit
EndSwitch
Wend
Func __Object_SetImage($hCtrl, $sPath, $iWidth = -1, $iHeight = -1, $iAngle = 0)
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sPath)
If ($iWidth = -1) Then $iWidth = _GDIPlus_ImageGetWidth($hImage)
If ($iHeight = -1) Then $iHeight = _GDIPlus_ImageGetHeight($hImage)
$hThumb = _GDIPlus_GetImageThumbnail($hImage, $iWidth, $iHeight)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hThumb)
__sprite_Rotate($hCtrl, $hBitmap, $iAngle)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
EndFunc
Func __Object_Track($hWnd, $iObject, $iTrack = 2)
$iPos = ControlGetPos(GUICtrlGetHandle($iObject), '', '')
$iCoord = WinGetPos($hWnd)
If _IsPressed(25) Then
If ($iPos[0] >= 0) Then
__Object_SetImage(GUICtrlGetHandle($iObject), @ScriptDir & '\tank.png', $iPos[2], $iPos[3], 90)
GUICtrlSetPos($iObject, $iPos[0] - $iTrack, $iPos[1], $iPos[2], $iPos[3])
Return 1
EndIf
ElseIf _IsPressed(26) Then
If ($iPos[1] >= 0) Then
__Object_SetImage(GUICtrlGetHandle($iObject), @ScriptDir & '\tank.png', $iPos[2], $iPos[3], 0)
GUICtrlSetPos($iObject, $iPos[0], $iPos[1] - $iTrack, $iPos[2], $iPos[3])
Return 1
EndIf
ElseIf _IsPressed(27) Then
If ($iPos[0] <= ($iCoord[2] - $iPos[2])) Then
__Object_SetImage(GUICtrlGetHandle($iObject), @ScriptDir & '\tank.png', $iPos[2], $iPos[3], 270)
GUICtrlSetPos($iObject, $iPos[0] + $iTrack, $iPos[1], $iPos[2], $iPos[3])
Return 1
EndIf
ElseIf _IsPressed(28) Then
If ($iPos[1] <= ($iCoord[3] - $iPos[3])) Then
__Object_SetImage(GUICtrlGetHandle($iObject), @ScriptDir & '\tank.png', $iPos[2], $iPos[3], 180)
GUICtrlSetPos($iObject, $iPos[0], $iPos[1] + $iTrack, $iPos[2], $iPos[3])
Return 1
EndIf
EndIf
Return 0
EndFunc
Func __sprite_Rotate($hWnd, $hBitmap, $iAngle)
Local $tRECT, $tSIZE, $W[2], $H[2], $hDC, $hDestDC, $hDestSv, $hSrcDC, $hSrcSv, $hBrush, $hPen, $hMask, $hBmp, $hObj
Local $aPoint[3][2]
If Not IsHWnd($hWnd) Then
$hWnd = GUICtrlGetHandle($hWnd)
If Not $hWnd Then
Return 0
EndIf
EndIf
$tRECT = _WinAPI_GetClientRect($hWnd)
$W[0] = DllStructGetData($tRECT, 3) - DllStructGetData($tRECT, 1)
$H[0] = DllStructGetData($tRECT, 4) - DllStructGetData($tRECT, 2)
$tSIZE = _WinAPI_GetBitmapDimension($hBitmap)
$W[1] = DllStructGetData($tSIZE, 1)
$H[1] = DllStructGetData($tSIZE, 2)
$aPoint[0][0] = ($W[0] - $W[1]) / 2
$aPoint[0][1] = ($H[0] - $H[1]) / 2
$aPoint[1][0] = $aPoint[0][0] + $W[1]
$aPoint[1][1] = $aPoint[0][1]
$aPoint[2][0] = $aPoint[0][0]
$aPoint[2][1] = $aPoint[0][1] + $H[1]
$hDC = _WinAPI_GetDC($hWnd)
$hDestDC = _WinAPI_CreateCompatibleDC($hDC)
$hMask = _WinAPI_CreateBitmap($W[0], $H[0], 1, 1)
$hDestSv = _WinAPI_SelectObject($hDestDC, $hMask)
$hBrush = _WinAPI_SelectObject($hDestDC, _WinAPI_GetStockObject($DC_BRUSH))
$hPen = _WinAPI_SelectObject($hDestDC, _WinAPI_GetStockObject($DC_PEN))
_WinAPI_SetDCBrushColor($hDestDC, 0xFFFFFF)
_WinAPI_SetDCPenColor($hDestDC, 0xFFFFFF)
_WinAPI_Ellipse($hDestDC, _WinAPI_CreateRectEx($aPoint[0][0] + 43, $aPoint[0][1] + 43, $aPoint[1][0] - 86, $aPoint[2][1] - 86))
$hBmp = _WinAPI_CreateCompatibleBitmapEx($hDC, $W[0], $H[0], 0xFFFFFF)
_WinAPI_SelectObject($hDestDC, $hBrush)
_WinAPI_SelectObject($hDestDC, $hPen)
_WinAPI_SelectObject($hDestDC, $hBmp)
$hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
$hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBitmap)
_WinAPI_RotatePoints($aPoint, $W[0] / 2, $H[0] / 2, $iAngle)
_WinAPI_PlgBlt($hDestDC, $aPoint, $hSrcDC, 0, 0, $W[1], $H[1])
_WinAPI_RotatePoints($aPoint, $W[0] / 2, $H[0] / 2, -2 * $iAngle)
_WinAPI_PlgBlt($hDestDC, $aPoint, $hSrcDC, 0, 0, $W[1], $H[1], $hMask)
_WinAPI_SelectObject($hDestDC, $hDestSv)
_WinAPI_DeleteDC($hDestDC)
_WinAPI_SelectObject($hSrcDC, $hSrcSv)
_WinAPI_DeleteDC($hSrcDC)
_WinAPI_DeleteObject($hMask)
_WinAPI_ReleaseDC($hWnd, $hDC)
$hObj = _SendMessage($hWnd, 0x0172, 0, $hBmp)
If $hObj Then
_WinAPI_DeleteObject($hObj)
EndIf
$hObj = _SendMessage($hWnd, 0x0173)
If $hObj <> $hBmp Then
_WinAPI_DeleteObject($hBmp)
EndIf
Return 1
EndFunc
Func _GDIPlus_GetImageThumbnail($hImage, $iWidth, $iHeight)
Local $Ret = DllCall($ghGDIPDll, 'uint', 'GdipGetImageThumbnail', 'ptr', $hImage, 'uint', $iWidth, 'uint', $iHeight, 'ptr*', 0, 'ptr', 0, 'ptr', 0)
If (@error) Or ($Ret[0]) Then
Return SetError(@error, @extended, 0)
EndIf
Return $Ret[4]
EndFunc