#include <GuiConstants.au3>
#include <GdiPlus.au3>

; hex transparanz: http://stackoverflow.com/questions/23201134/transparent-argb-hex-value



OnAutoItExitRegister("_ExitRegister")
GUIRegisterMsg($WM_PAINT, "WM_PAINT")


Global $GUI_Width = 600
Global $GUI_Height = 350
Global $Gdi_x1 = 0
Global $Gdi_y1 = 0
Global $Gdi_x2 = $GUI_Width
Global $Gdi_y2 = $GUI_Height


_GDIPlus_Startup()


#Region ### MAIN_GUI ####

; INIT_MAIN_GUI

Global $mainGui = GUICreate("", $GUI_Width, $GUI_Height, -1, -1)

; END_OF_INIT_MAIN_GUI


; INIT_GDI

Global $graphic = _GDIPlus_GraphicsCreateFromHWND($mainGui) ; frontbuffer
Global $bitmap = _GDIPlus_BitmapCreateFromGraphics($Gdi_x2, $Gdi_y2, $graphic) ; x2, y2
Global $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap) ; backbuffer

Global $gdiImage1 = _GDIPlus_ImageLoadFromFile("btn1_norm.png")
Global $gdiBrush1 = _GDIPlus_BrushCreateSolid(0xFFBDBDBD)
Global $gdiBrush2 = _GDIPlus_BrushCreateSolid(0xFFEE2C2C)

; END_OF_INIT_GDI


GUISetState(@SW_SHOW)

#EndRegion ### MAIN_GUI ####


While 1

	_Main()

WEnd


Func _Main()

	_GuiControls()

	_GdiDraw() ; You can use this function out of a loop if nothing should be changed in the Gdi Section

	Sleep(50)
EndFunc


Func _GuiControls()
	Local $nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit

	EndSwitch
EndFunc


Func _GdiDraw()
	_GDIPlus_GraphicsClear($buffer) ; Inhalt des Backbuffer`s wird gelöscht
	_GDIPlus_GraphicsFillRect($buffer, $Gdi_x1, $Gdi_y1, $Gdi_x2, $Gdi_y2, $gdiBrush1) ; draw map background


	_GDIPlus_GraphicsFillRect($buffer, 50, 50, 4, 4, $gdiBrush2); draw example dot
	_GDIPlus_GraphicsDrawImageRect($buffer, $gdiImage1, 85, 125, 108, 31) ; draw example image


	_GDIPlus_GraphicsDrawImage($graphic, $bitmap, $Gdi_x1, $Gdi_y1) ; Backbuffer wird an den Frontbuffer übergeben
EndFunc



Func _ExitRegister()

	; -i- function will be automatically executed when script will shutting down


	; GDI_CLEAR

	; clear grahpic
	_GDIPlus_BitmapDispose($bitmap)
	_GDIPlus_GraphicsDispose($buffer)
    _GDIPlus_GraphicsDispose($graphic) ; Grafik Objekt freigeben

	; clear image
	_GDIPlus_ImageDispose($gdiImage1)

	; clear brush
	_GDIPlus_BrushDispose($gdiBrush1)
	_GDIPlus_BrushDispose($gdiBrush2)

	; shutdown
	_GDIPlus_Shutdown()

	; END_OF_GDI_CLEAR

EndFunc

Func WM_PAINT()
	_WinAPI_RedrawWindow($mainGui, 0, 0, $RDW_UPDATENOW)
	_GDIPlus_GraphicsDrawImageRect($graphic, $bitmap, $Gdi_x1, $Gdi_y1, $Gdi_x2, $Gdi_y2)
	_WinAPI_RedrawWindow($mainGui, 0, 0, $RDW_VALIDATE)
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_PAINT