gdi+ text

  • hi,

    ich will ein text mit gdiplus in der mitte ausgerichtet eines Bereichs anzeigen lassen,
    des funzt au mit einem festgellegten Text, nur wie mach des das ein Variabler Text
    in der mitte in einem Bereich ausgerichtet ist

    Muss mit gdiplus sein, nicht mit GuiCtrlCreateLabel()

    Spoiler anzeigen
    [autoit]


    $Text = "Text"
    $Brush = _GDIPlus_BrushCreateSolid(0xFF000000)
    $Format = _GDIPlus_StringFormatCreate()
    $Family = _GDIPlus_FontFamilyCreate("Segoe UI" )
    $Font = _GDIPlus_FontCreate($Family, 8.5, 1)
    $Layout = _GDIPlus_RectFCreate(20, 135, 100, 20)
    _GDIPlus_GraphicsDrawStringEx($Graphic, $Text, $Font, $Layout, $Format, $Brush) ; der Variable Text soll von den 100 pixeln weite in der mitte stehen

    [/autoit]
  • Das kannst du über das StringFormat einstellen. Um den Text horizontal kannst du _GDIPlus_StringFormatSetAlign benutzen. Für die vertikale Ausrichtung braucht man allerdings eine Funktion namens _GDIPlus_StringFormatSetLineAlign aus der GDIP.au3 (nicht in den Standard-Includes). In der GDIP.au3 gibt es übrigens noch mehr solcher nützlicher Funktionen, kannst du dir ja mal anschauen (einfach per google suchen). Ich hab mal das Beispiel aus der Hilfe modifiziert damit es den Text vertikal sowie horizontal zentriert.

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Local $hWnd = GUICreate("GDI+ Example", 400, 300)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    _GDIPlus_GraphicsClear($hGraphics)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900)
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    Local $hFont = _GDIPlus_FontCreate($hFamily, 36)
    Local $hLayout = _GDIPlus_RectFCreate(0, 0, 400, 300)
    Local $hStringFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_StringFormatSetAlign($hStringFormat, 1)
    _GDIPlus_StringFormatSetLineAlign($hStringFormat, 1)
    _GDIPlus_GraphicsDrawStringEx($hGraphics, "AutoIt Rocks", $hFont, $hLayout, $hStringFormat, $hBrush)

    [/autoit] [autoit][/autoit] [autoit]

    Do
    Local $msg = GUIGetMsg()
    Until $msg = $GUI_EVENT_CLOSE

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_StringFormatDispose($hStringFormat)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GDIPlus_StringFormatSetLineAlign
    ; Description ...: Sets the line alignment of a StringFormat object in relation to the origin of a layout rectangle
    ; Syntax.........: _GDIPlus_StringFormatSetLineAlign($hStringFormat, $iStringAlign)
    ; Parameters ....: $hStringFormat - Pointer to a StringFormat object
    ; $iStringAlign - Type of line alignment to use:
    ; |0 - Alignment is towards the origin of the bounding rectangle
    ; |1 - Alignment is centered between origin and the height of the formatting rectangle
    ; |2 - Alignment is to the far extent (right side) of the formatting rectangle
    ; Return values .: Success - True
    ; Failure - False and either:
    ; |@error and @extended are set if DllCall failed
    ; |$GDIP_STATUS contains a non zero value specifying the error code
    ; Remarks .......: The line alignment setting specifies how to align the string vertically in the layout rectangle.
    ; The layout rectangle is used to position the displayed string
    ; Related .......: _GDIPlus_StringFormatGetLineAlign
    ; Link ..........; @@MsdnLink@@ GdipSetStringFormatLineAlign
    ; Example .......; No
    ; ===============================================================================================================================
    Func _GDIPlus_StringFormatSetLineAlign($hStringFormat, $iStringAlign)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetStringFormatLineAlign", "hwnd", $hStringFormat, "int", $iStringAlign)

    [/autoit] [autoit][/autoit] [autoit]

    If @error Then Return SetError(@error, @extended, False)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[0] = 0
    EndFunc ;==>_GDIPlus_StringFormatSetLineAlign

    [/autoit]