#include <GDIPlus.au3>
#include <File.au3>

Dim $Captcha
Global $width = 500, $height = 500, $CptText = _FileReadToArray(@DesktopDir & "\Text.txt", $Captcha), $hWnd, $rnd = Random(1, UBound($Captcha) - 1, 1)

$hWnd = GUICreate("GDI+ Captcha", $width, $height + 60, -1, -1, -1, 0x00000080)
$inpt = GUICtrlCreateInput("Captcha", 30, 510, 90, 30)
$btn = GUICtrlCreateButton("Check", 140, 510, 90, 30)
$redraw = GUICtrlCreateButton("Captcha erneuern", 260, 510, 90, 30)
GUISetState()

_GDIPlus_Startup()
Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic)
Global $Backbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

_DrawCaptcha()

While 1
	$msg = GUIGetMsg()
	Switch $msg
		Case -3
			_Dispose()
		Case $btn
			_CaptchaCheck()
		Case $redraw
			_DrawCaptcha(Random(1, 4, 1))
	EndSwitch
WEnd

Func _Dispose()
	_GDIPlus_ImageDispose($Backbuffer)
	_GDIPlus_BitmapDispose($hBitmap)
	_GDIPlus_GraphicsDispose($hGraphic)
	_GDIPlus_Shutdown()
	Exit
EndFunc

Func _DrawCaptcha($iIndex = "")
	Local $rndX = Random(1, $width)
	Local $rndY = Random(1, $height)
	If Not $iIndex Then
		_GDIPlus_GraphicsDrawString($Backbuffer, $Captcha[$rnd], $rndX, $rndY)
	Else
		_GDIPlus_GraphicsDrawString($Backbuffer, $Captcha[$iIndex], $rndX, $rndY)
	EndIf
	For $i = 0 To UBound($Captcha)
		_GDIPlus_GraphicsDrawLine($Backbuffer, $rndX - 7, $rndY - 5, $rndX + 20, $rndY + 15)
		_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $width, $height)
		$rndX += 5.7
		$rndY += 0.5
	Next
	_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $width, $height)
EndFunc

Func _CaptchaCheck()
	Local $inptTxt = GUICtrlRead($inpt)
	If $inptTxt == $Captcha[$rnd] Then
		MsgBox(0, "Korrekt", "Das war der richtige Captcha!")
	Else
		MsgBox(0, "Zugang verweigert", "Falscher Captcha!")
		_DrawCaptcha()
	EndIf
EndFunc