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>
$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)
GUISetState(@SW_SHOW)
[/autoit][autoit][/autoit][autoit]While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
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)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
$width = Int(DllStructGetData($aInfo[0], 3))
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hGDIFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
GUICtrlCreateLabel($text, $left, $top, $width, $height)
GUICtrlSetFont(-1, $size, $weight, $attribute, $font)
EndFunc
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 ![]()