﻿#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $sPicfile = StringRegExpReplace(@AutoItExe, '(.+\\).+', '$1') & 'Examples\Helpfile\Extras\Tech.bmp'

_GDIPlus_Startup() ; GDI+ starten

Global $iW = 640, $iH = 480
Global $hGui = GUICreate('Test', $iW, $iH)
Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui) ; Graphic von der GUI
Global $hBackBuffer = _GDIPlus_BitmapCreateFromScan0($iW, $iH) ; Backbuffer-Bitmap erstellen
Global $hGfxBB = _GDIPlus_ImageGetGraphicsContext($hBackBuffer) ; Graphic vom Backbuffer
Global $hBGImage = _GDIPlus_BitmapCreateFromFile($sPicfile) ; Hintergrundbild laden
_GDIPlus_GraphicsDrawImageRect($hGfxBB, $hBGImage, 0, 0, $iW, $iH) ; Hintergrundbild auf den Backbuffer zeichnen

; Beispiele (alle auf den Backbuffer zeichnen):
; Ausrichtung: Horizontal, nicht gedreht, aber auf drei Zeilen, unterstrichen und kursiv, zentriert
Global $sText = 'Das ist ein Test mit ganz viel Text und noch mehr Text für die nächsten Zeilen'
_GDIPlus_GraphicsDrawStringRotate($hGfxBB, $sText, 0, 1, 90, 0, 360, 100, 0xFF000000, 'Times New Roman', 20, 6)

; Ausrichtung: Vertikal, um 90 Grad gedreht, Normalschrift, auf zwei Zeilen, rechts ausgerichtet
_GDIPlus_GraphicsDrawStringRotate($hGfxBB, '90 Grad. Das ist ein Test', 1, 2, $iW - 80, 120, 160, Default, 0xFF0000FF, 'Verdana', 16, 0)

; Ausrichtung: Horizontal, um 180 Grad gedreht, Fettschrift
_GDIPlus_GraphicsDrawStringRotate($hGfxBB, '180 Grad. Das ist ein Test', 2, 0, 120, $iH - 50, Default, Default, 0xFF00FF00, 'Verdana', 20, 1)

; Ausrichtung: Vertikal, um 270 Grad gedreht, durchgestrichen
_GDIPlus_GraphicsDrawStringRotate($hGfxBB, '270 Grad. Das ist ein Test', 3, 0, 10, 120, Default, Default, 0xFFFF0000, 'Verdana', 16, 8)

GUIRegisterMsg($WM_PAINT, '_WM_PAINT') ; WM_PAINT registrieren
GUISetState()

While True
	Switch GUIGetMsg()
		Case $GUI_EVENT_CLOSE
			ExitLoop
		Case $GUI_EVENT_RESTORE ; beim Restore wird kein WM_PAINT ausgeloest,
			_WM_PAINT(0, 0, 0, 0) ; weswegen wir das von Hand aufrufen muessen
	EndSwitch
WEnd
_GDIPlus_GraphicsDispose($hGfxBB) ; Graphic vom Backbuffer freigeben
_GDIPlus_ImageDispose($hBackBuffer) ; Backbuffer-Bitmap freigeben
_GDIPlus_ImageDispose($hBGImage) ; Hintergrundbild freigeben
_GDIPlus_GraphicsDispose($hGraphic) ; Graphic von der GUI freigeben
_GDIPlus_Shutdown() ; GDI+ beenden
Exit

Func _WM_PAINT($hWnd, $iMsg, $wParam, $lParam) ; wenn Windows ein WM_PAINT sendet,
	_GDIPlus_GraphicsDrawImage($hGraphic, $hBackBuffer, 0, 0) ; den Backbuffer auf die GUI zeichnen
	Return $GUI_RUNDEFMSG
EndFunc


;===============================================================================
; Name:             : _GDIPlus_GraphicsDrawStringRotate
; Description:		: Funktion zum zeichnen eines Strings auf eine Graphic, mit
;                     Drehung um 90, 180, 270 Grad.
; Syntax:           : _GDIPlus_GraphicsDrawStringRotate($hGraphic, $sString[, $iRotate][, $iAlign][, $iLeft][, $iTop] _
;                    [, $iW][, $iH][, $iColor][, $sFontname][, $iFontSize][, $iFontWeight])
; Parameter(s):		:
;                     $hGraphic: Handle der Graphic, auf die gezeichnet werden soll
;                     $sString: Der String, der gezeichnet werden soll
;                     $iRotate: Drehung (0 = keine Drehung, 1 = 90 Grad, 2 = 180 Grad, 3 = 270 Grad)
;                     $iAlign: Textausrichtung (0 = Links, 1 = Zentriert, 2 = Rechts)
;                     $iLeft und $iTop: Position, an der der String gezeichnet werden soll
;                     $iW und $iH: Ein Rechteck, in dem der String eingepasst wird (Width, Height)
;                                  Bei Verwendung von "Default" wird die Groesse automatisch ermittelt
;                     $iColor: Die Farbe, mit der der String gezeichnet werden soll (ARGB)
;                     $sFontname: Name des Fonts, der fuer den String verwendet werden soll
;                     $iFontsize: Die Fontgroesse
;                     $iFontWeight: 0 = Normal, 1 = Fett, 2 = Kursiv, 4 = Unterstrichen, 8 = Durchgestrichen
; Author(s):		: Oscar (www.autoit.de)
; Version / Date:   : 1.1.0.0 / 05.02.2020
Func _GDIPlus_GraphicsDrawStringRotate(Const ByRef $hGraphic, Const ByRef $sString, $iRotate = 0, $iAlign = 0, $iLeft = 0, $iTop = 0, _
		$iW = Default, $iH = Default, $iColor = 0xFF000000, $sFontname = 'Arial', $iFontSize = 12, $iFontWeight = 0)
	Local $hBitmap, $hGfxCtx, $hFormat, $hBrush, $hFamily, $hFont, $tLayout, $aInfo, $hRotate
	Local $tGraphic = DllStructCreate('long width; long height', $hGraphic + 24)
	$hBitmap = _GDIPlus_BitmapCreateFromScan0($tGraphic.width, $tGraphic.height)
	$hGfxCtx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
	_GDIPlus_GraphicsSetTextRenderingHint($hGfxCtx, 4)
	$hFormat = _GDIPlus_StringFormatCreate()
	_GDIPlus_StringFormatSetAlign($hFormat, $iAlign)
	$hBrush = _GDIPlus_BrushCreateSolid($iColor)
	$hFamily = _GDIPlus_FontFamilyCreate($sFontname)
	$hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, $iFontWeight)
	$tLayout = _GDIPlus_RectFCreate(0, 0, ($iW = Default ? $tGraphic.width : $iW), ($iH = Default ? $tGraphic.height : $iH))
	$aInfo = _GDIPlus_GraphicsMeasureString($hGfxCtx, $sString, $hFont, $tLayout, $hFormat)
	_GDIPlus_GraphicsDrawStringEx($hGfxCtx, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
	$hRotate = _GDIPlus_BitmapCloneArea($hBitmap, $aInfo[0].X, $aInfo[0].Y, $aInfo[0].Width, $aInfo[0].Height, $GDIP_PXF32ARGB)
	_GDIPlus_ImageRotateFlip($hRotate, $iRotate)
	_GDIPlus_GraphicsDrawImage($hGraphic, $hRotate, $iLeft, $iTop)
	_GDIPlus_FontDispose($hFont)
	_GDIPlus_FontFamilyDispose($hFamily)
	_GDIPlus_StringFormatDispose($hFormat)
	_GDIPlus_BrushDispose($hBrush)
	_GDIPlus_GraphicsDispose($hGfxCtx)
	_GDIPlus_ImageDispose($hBitmap)
	_GDIPlus_ImageDispose($hRotate)
EndFunc   ;==>_GDIPlus_GraphicsDrawStringRotate

