#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
Global $width = 633, $height = 400, $col, $color, $fop
Opt("TrayMenuMode", 1)
$hWnd = GUICreate("GDI+ Paint by PrideRage", 633, 447, -1, -1)
$clear = GUICtrlCreateButton("Leeren", 8, 416, 57, 25, $WS_GROUP)
$quit = GUICtrlCreateButton("Beenden", 568, 416, 57, 25, $WS_GROUP)
$brush = GUICtrlCreateButton("Pinsel", 208, 416, 57, 25, $WS_GROUP)
$del = GUICtrlCreateButton("Radierer", 280, 416, 57, 25, $WS_GROUP)
$sav = GUICtrlCreateButton("Speichern", 130, 416, 57, 25, $WS_GROUP)
$pencol = GUICtrlCreateButton("Pinsel Farbe", 352, 416, 65, 25, $WS_GROUP)
TraySetClick("8")
$titemq = TrayCreateItem("Beenden")
GUISetBkColor(0xFF000000)
GUISetState(@SW_SHOW)

_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
Global $backbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
Global $hPen = _GDIPlus_PenCreate(0xFFFFFFF, 3)
Global $hClearBr = _GDIPlus_BrushCreateSolid(0xFFFFFFF)

AdlibRegister("_GDIPlus_DrawClickedMouse", 10)

While 1
	$nMsg = GUIGetMsg()
	If TrayGetMsg() = $titemq Then
		_Dispose()
	EndIf
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			_Dispose()
		Case $quit
			_Dispose()
		Case $brush
			_GDIPlus_SetUserBrush("brush.pinsel")
		Case $del
			_GDIPlus_SetUserBrush("brush.deleter")
		Case $pencol
			$col = _ChooseColor(2)
			$color = StringReplace($col, "0x", "0xFF")
			_GDIPlus_PenSetColor($hPen, $color)
		Case $clear
			_GDIPlus_GraphicsClear($backbuffer)
			_GDIPlus_GraphicsFillRect($hGraphics, 0, 0, $width, $height, $hClearBr)
		Case $sav
			$fop = FileSaveDialog("Speichern unter...", @MyDocumentsDir, "JPEG (*.jpg;*.jpeg;)")
			$sCLSID = _GDIPlus_EncodersGetCLSID ("JPG")
			_GDIPlus_ImageSaveToFileEx($hBitmap, $fop, $sCLSID)
	EndSwitch
WEnd

Func _Dispose()
	AdlibUnRegister("_GDIPlus_DrawClickedMouse")
	_GDIPlus_PenDispose($hPen)
	_GDIPlus_BrushDispose($hClearBr)
	_GDIPlus_BitmapDispose($hBitmap)
	_GDIPlus_ImageDispose($backbuffer)
	_GDIPlus_GraphicsDispose($hGraphics)
	_GDIPlus_Shutdown()
	Exit
EndFunc

Func _GDIPlus_SetUserBrush($pen)
	Switch $pen
		Case "brush.pinsel"
			If $color <> "" Then
				_GDIPlus_PenSetWidth($hPen, 3)
				_GDIPlus_PenSetColor($hPen, $color)
			Else
				_GDIPlus_PenSetWidth($hPen, 3)
				_GDIPlus_PenSetColor($hPen, 0xFF000000)
			EndIf
		Case "brush.deleter"
			_GDIPlus_PenSetWidth($hPen, 70)
			_GDIPlus_PenSetColor($hPen, 0xFF000000)
	EndSwitch
EndFunc

Func _GDIPlus_DrawClickedMouse()
	Local $mpos = GUIGetCursorInfo()
	If IsArray($mpos) Then
		If $mpos[2] = 1 Then
			_GDIPlus_GraphicsDrawLine($backbuffer, $mpos[0], $mpos[1], $mpos[0] + 3, $mpos[1] + 3, $hPen)
			_GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $width, $height)
		EndIf
	EndIf
EndFunc