Label - Länge automatisch bestimmen

  • Hiho,

    ich würde gerne die Länge eines Label-Elements automatisch bestimmen. Zur Zeit verwende ich folgendes:

    [autoit]

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

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

    $Form1 = GUICreate("Form1", 300, 300)

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

    $Label1 = GUICtrlCreateLabelEx("Hello World!", 8, 8, 17, 11.5, 400, 0, "Arial")
    GUICtrlSetColor(-1, 0x008080)
    GUICtrlSetCursor(-1, 0)

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

    GUISetState(@SW_SHOW)

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

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit

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

    EndSwitch
    WEnd

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

    Func GUICtrlCreateLabelEx($text, $left, $top, $height = 17, $size = 8.5, $weight = 400, $attribute = 0, $font = "MS Sans Serif")
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
    $hFamily = _GDIPlus_FontFamilyCreate($font)
    $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $attribute)
    $hFormat = _GDIPlus_StringFormatCreate()
    $tLayout = _GDIPlus_RectFCreate(0, 0, @DesktopWidth, @DesktopHeight)

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

    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
    $width = Int(DllStructGetData($aInfo[0], 3))

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

    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hGDIFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()

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

    GUICtrlCreateLabel($text, $left, $top, $width, $height)
    GUICtrlSetFont(-1, $size, $weight, $attribute, $font)
    EndFunc

    [/autoit]

    Das funktioniert auch größtenteils, jedoch ist das Label noch länger, als nötig. Ferner habe ich hier und da noch Probleme mit ein paar anderen Schriftarten (z.B. "MS Sans Serif")

    Letztlich geht es einfach darum, die nötige Breite eines Labels automatisch bestimmen zu lassen. Wenn also jemand eine bessere Idee hat, dies umzusetzen, immer her damit :)

  • Ich würde als erstes einen Return-Wert setzen also sowas hier:

    [autoit]

    $Label = GUICtrlCreateLabel($text, $left, $top, $width, $height)
    GUICtrlSetFont(-1, $size, $weight, $attribute, $font)
    Return $Label

    [/autoit]


    und dann kannst du per ControlGetPos() die Position von Label1 rausfinden und somit ja auch die Breite/Höhe, etc.

    mfg
    Hauke

  • BugFix

    Ich habe deine Funktion btMeasureClick() mal gemopst. Aber irgendwie bin ich wohl zu blöd, da dein Generator "73 x 19 px" für den Text "Hello World", formatiert mit Arial 11pt ausgibt.

    Bei mir kommen am Ende jedoch "88 x 19 px" bei den selben Einstellungen raus. Die übrigen 15px sind auch jene, um die es mir geht. Woran kann das liegen?

    [autoit]

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

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

    $Form1 = GUICreate("Form1", 300, 300)

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

    _GDIPlus_Startup()

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

    Local $hFormat = _GDIPlus_StringFormatCreate(0)
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    Local $hFont = _GDIPlus_FontCreate($hFamily, 11, 0, 3)
    Local $tLayout = _GDIPlus_RectFCreate(15, 171, 0, 0)
    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form1)
    Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, "Hello World!", $hFont, $tLayout, $hFormat)
    Local $iWidth = Ceiling(DllStructGetData($aInfo[0], "Width"))
    Local $iHeight = Ceiling(DllStructGetData($aInfo[0], "Height"))

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

    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ShutDown()

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

    MsgBox(0,0,$iWidth & ' x ' & $iHeight) ;73x19 im TextMeter

    [/autoit]

    Scheint mir, als hätte ich irgendwo irgendetwas vergessen?