#include <WindowsConstants.au3>
#Include <GDIPlus.au3>
#Include <Misc.au3>
#include <GUIConstantsEx.au3>

Global $aButton[6]
Global $dll = DllOpen("user32.dll")
Global $Color = "0xFF0000"
Global $bcolor = "0xFFBBBBBB"
Global $hGraphic, $PenWidth
Global $hPen, $MX, $MY, $MXOld, $MYOld, $WinPos

_GDIPlus_Startup ()

$w = 170
$h = 170

$gui = GUICreate("Test Windows regions", $w, $h + 24, -1, -1, $WS_POPUP, $WS_EX_TOOLWINDOW)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND (WinGetHandle($gui))
$Bitmap = _GDIPlus_BitmapCreateFromGraphics($w, $h, $hGraphic) ;create bitmap
$Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap) ;create buffer
$PenWidth = 6
;~ $hPen = _GDIPlus_PenCreate ('0xFF' & StringRight($Color,6),$PenWidth)
$Brush = _GDIPlus_BrushCreateSolid('0xFF' & StringRight($Color,6))

$WinPos=WinGetPos($gui)

;Left - Top - Width - Height
$aButton[0] = GUICtrlCreateButton("1", 0, 0, 24, 24, -1, $WS_EX_TOPMOST)
$aButton[1] = GUICtrlCreateButton("2", 24, 0, 24, 24)
$aButton[2] = GUICtrlCreateButton("", 48, 0, 24, 24)
GUICtrlSetBkColor($aButton[2], $Color)
$aButton[3] = GUICtrlCreateButton("4", 72, 0, 24, 24)
$aButton[4] = GUICtrlCreateButton("Exit", 100, 0, 24, 24)
$aButton[5] = GUICtrlCreatePic("", 0, 24, $w, $h, BitOR(-1, $WS_GROUP,$WS_CLIPSIBLINGS))
GUISetState(@SW_SHOW)

_GDIPlus_GraphicsClear($Buffer, $bcolor)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap, 0, 24, $w, $h)


$pos = WinGetPos($gui)
_GuiHole($gui, 0, 0, $w - 20, $h - 20)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $aButton[4]
            ; Clean up resources
;~             _GDIPlus_PenDispose ($hPen)
			_GDIPlus_BrushDispose($Brush)
            _GDIPlus_GraphicsDispose ($hGraphic)
            _GDIPlus_Shutdown ()
            DllClose($dll)
            Exit
        Case $aButton[0]

            ;transparent code by smashly / Malkey - modified by UEZ
            $hImage = _GDIPlus_BitmapCloneArea($Bitmap, 0, 0, $w, $h, $GDIP_PXF32ARGB)
            $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $w, $h, $GDIP_ILMWRITE, $GDIP_PXF32ARGB)

            $iStride = DllStructGetData($tBitmapData, "stride")
            $iScan0 = DllStructGetData($tBitmapData, "Scan0")
            $tPixel = DllStructCreate("int", $iScan0 + (0 * $iStride) + (0 * 4))

            $iFirstPixel = DllStructGetData($tPixel, 1)
            $iTransPixel = BitAND($iFirstPixel, 0x00FFFFFF)

            $iFirstPixel = StringRegExpReplace(Hex($iFirstPixel, 8), "(.{2})(.{2})(.{2})(.{2})", "\4\3\2\1")
            $iTransPixel = StringTrimRight($iFirstPixel, 2) & "00"
            $v_BufferA = DllStructCreate("byte[" & $h * $w * 4 & "]", $iScan0) ; Create DLL structure for all pixels
            $AllPixels = DllStructGetData($v_BufferA, 1)
            $sREResult1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ")
            $sPix = "0x" & StringStripWS(StringRegExpReplace($sREResult1, "(" & $iFirstPixel & ")", $iTransPixel), 8)
            $AllPixels = DllStructSetData($v_BufferA, 1, $sPix)

            _GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)
            _GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & "\Test.PNG", _GDIPlus_EncodersGetCLSID("PNG"))
            _GDIPlus_ImageDispose ($hImage)

            If Not @error Then
                MsgBox(0, '', 'Bild als Transparent gespeichert nach ' & @ScriptDir & "\Test.png!")
            Else
                MsgBox(0, '', 'Error!')
            EndIf


        Case $aButton[1]
            _GDIPlus_ImageSaveToFileEx($Bitmap, @ScriptDir & "\Test.PNG", _GDIPlus_EncodersGetCLSID("PNG"))
            If Not @error Then
                MsgBox(0, '', 'Bild gespeichert nach ' & @ScriptDir & "\Test.png!")
            Else
                MsgBox(0, '', 'Error!')
            EndIf
        Case $aButton[2]
            $Color = _ChooseColor(2)
            $r = BitAND($Color, 0xFF0000) / 0x10000
            $g = BitAND($Color, 0x00FF00) / 0x100
            $b = BitAND($Color, 0x0000FF)
            $pc = "0xFF" & Hex($r, 2) & Hex($g, 2) & Hex($b, 2)
;~             _GDIPlus_PenSetColor($hPen, $pc)
			_GDIPlus_BrushSetSolidColor($Brush, $pc)
            GUICtrlSetBkColor($aButton[2], $Color)
        Case $aButton[3]
            MsgBox(0, '', 'Button4 geklickt')
        Case $aButton[5]
            Draw()
    EndSwitch
WEnd

;Thx an Bugfix
Func _GuiHole($h_win, $i_x, $i_y, $i_sizew, $i_sizeh)
    Local $outer_rgn, $inner_rgn, $combined_rgn
    $combined_rgn = _WinAPI_CreateRectRgn(0, 0, 0, 0)
    _WinAPI_DeleteObject($outer_rgn)
    _WinAPI_DeleteObject($inner_rgn)
    _AddCtrlRegion($combined_rgn, $aButton)
    _WinAPI_SetWindowRgn($h_win, $combined_rgn)
EndFunc   ;==>_GuiHole

Func _AddCtrlRegion($full_rgn, $ctrl_id)
    Local $ctrl_pos, $ctrl_rgn
    If IsArray($ctrl_id) Then
        For $i = 0 To UBound($ctrl_id) -1
            $ctrl_pos = ControlGetPos($gui, "", $ctrl_id[$i])
            $ctrl_rgn = _WinAPI_CreateRectRgn($ctrl_pos[0], $ctrl_pos[1], $ctrl_pos[0] + $ctrl_pos[2], $ctrl_pos[1] + $ctrl_pos[3])
            _WinAPI_CombineRgn($full_rgn, $full_rgn, $ctrl_rgn, $RGN_OR)
            _WinAPI_DeleteObject($ctrl_rgn)
        Next
    Else
        $ctrl_pos = ControlGetPos($gui, "", $ctrl_id)
        $ctrl_rgn = _WinAPI_CreateRectRgn($ctrl_pos[0], $ctrl_pos[1], $ctrl_pos[0] + $ctrl_pos[2], $ctrl_pos[1] + $ctrl_pos[3])
        _WinAPI_CombineRgn($full_rgn, $full_rgn, $ctrl_rgn, $RGN_OR)
        _WinAPI_DeleteObject($ctrl_rgn)
    EndIf
EndFunc   ;==>_AddCtrlRegion


Func Draw()
    ;_GDIPlus_PenSetDashCap($hPen, $GDIP_DASHCAPROUND)
    ;_GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLEDASH)
    $MXOld=MouseGetPos(0) - $WinPos[0]
    $MYOld=MouseGetPos(1) - $WinPos[1] - 24 ;- Knopf Höhe
    ; Loop until user exits
    While _IsPressed("01", $dll)
        $MX=MouseGetPos(0) - $WinPos[0]
        $MY=MouseGetPos(1) - $WinPos[1] - 24
;~         _GDIPlus_GraphicsDrawLine ($Buffer, $MXOld, $MYOld, $MX, $MY, $hPen)
		_GDIPlus_GraphicsFillEllipse($Buffer, $MX-($PenWidth/2), $MY-($PenWidth/2), $PenWidth, $PenWidth, $Brush)
        _GDIPlus_GraphicsDrawImageRect($hGraphic, $Bitmap, 0, 24, $w, $h)
        $MXOld=$MX
        $MYOld=$MY
    Wend
EndFunc

Func _GDIPlus_BrushSetSolidColor($hBrush, $iARGB = 0xFF000000)
    Local $aResult

    $aResult = DllCall($ghGDIPDll, "int", "GdipSetSolidFillColor", "hwnd", $hBrush, "int", $iARGB)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_GDIPlus_BrushSetSolidColor
