Page 1 sur 1

[R] Adapter la taille d'un Label à son contenu

Posté : ven. 13 mai 2016 12:28
par GMH
Bonjour,

J'ai trouvé une belle fonction pour adapter la laille d'un Label à la longueur de son contenu, à l'adresse https://www.autoitscript.com/forum/topi ... -fit-text/

Mais cette manière de faire perturbe le résultat de la fonction GUICtrlSetTip . Les bulles au survol de la souris ne correspondent plus aux Labels.

Mon code fait le travail suivant :

1. Lecture d'une phrase en langue étrangère dans un fichier texte.
2. Découpage de la phrase en mots, affichage de chaque mot dans un label
3. Appel à la fonction _Control_SetWidth2Text pour que la lecture de la phrase reconstituée par la suite de labels soit plus agréable à l'oeil. Cela uniformise les espaces entre les mots.
4. Lecture de la phrase traduite en français.
5. Découpage de la phrase française en mots, affichage de chaque mot dans une bulle (fonction GUICtrlSetTip)

Code : Tout sélectionner

For $i = 1 To UBound($Tablo_Grec) - 1
	GUICtrlCreateLabel($Tablo_Grec[$i], $MargeH, $MargeV, 300, 30) ;,"",$WS_EX_TRANSPARENT )
	GUICtrlSetFont(-1, 15, 400, 0, "Courier") ; police à chasse fixe pour la présentation
	_Control_SetWidth2Text($hGUI, "", -1)
	GUICtrlSetTip( -1, $Tablo_Francais[$i] ) ; bulle d'aide
	$MargeH = $MargeH + (StringLen($Tablo_Grec[$i]) * 11) ; calcul de la position du prochain Label (code pas au point !)
Next

Func _Control_SetWidth2Text($hWin, $sWinText, $hCtrl, $iPad = 0);author: ResNullius, based on PaulIA/GaryFrost (listview udf)
    Local $hWnd, $hDC, $hFont, $iMax, $tSize, $sText, $aCtrlPos, $ctrlX, $ctrlY, $ctrlW, $ctrlH
    If Not IsHWnd($hCtrl) Then
        $hWnd = ControlGetHandle($hWin, $sWinText, $hCtrl)
    Else
        $hWnd = $hCtrl
    EndIf
    If IsHWnd($hWnd) Then
        $hFont = _SendMessage($hWnd, $WM_GETFONT)
        $hDC = _WinAPI_GetDC($hWnd)
        _WinAPI_SelectObject($hDC, $hFont)
        $sText = ControlGetText($hWin, $sWinText, $hCtrl)
        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sText & " ")
        If DllStructGetData($tSize, "X") > $iMax Then
            $iMax = DllStructGetData($tSize, "X") + $iPad
            $aCtrlPos = ControlGetPos($hWin, $sWinText, $hCtrl)
            $ctrlX = $aCtrlPos[0]
            $ctrlY = $aCtrlPos[1]
            $ctrlW = $aCtrlPos[2]
            $ctrlH = $aCtrlPos[3]
            ControlMove($hWin, $sWinText, $hCtrl, $ctrlX, $ctrlY, $iMax, $ctrlH)
        EndIf
        _WinAPI_SelectObject($hDC, $hFont)
        _WinAPI_ReleaseDC($hWnd, $hDC)
    EndIf
EndFunc;==>_Control_SetWidth2Text
Peut-être existe-t-il mieux que cette fonction.
Je vous remercie de vos avis et conseils.

Re: [..] Adapter la taille d'un Label à son contenu

Posté : ven. 13 mai 2016 15:14
par walkson
Bonjour,
Il y a aussi une possibilité en GDI qui est d'ailleurs abordée à la fin du post que vous indiquez. J'ai complété le code avec la hauteur.

Code : Tout sélectionner

#NoTrayIcon
Opt("GuiOnEventMode", 1)

#include <GuiConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>
$fontName = "Baskerville Old Face"
$fontSize = 9
;$text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT   #"
$text = "This is loooooooooooong textttttttttt   #"


$guiMain = GUICreate("", 50 , 200)
GUISetOnEvent($gui_event_close, "quit")
GUISetFont($fontSize, 400, 0, $fontName, $guiMain)
$len = getTextLengthInPixel($text, $fontName, $fontSize, 2) ; bold
WinMove($guiMain,"",50,Default,$len[0][0]+50,$len[0][1]+50)
GUICtrlCreateLabel($text, 20, 5, $len[0][0], $len[0][1], $ss_leftnowordwrap)
GUICtrlSetFont(-1, $fontSize, 600)
GUICtrlSetBkColor(-1,0xFFFF00)

GUISetState()
While True
    Sleep(1000)
WEnd

Func getTextLengthInPixel($text, $font, $size = 9, $style = 0)
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain)
    $hFamily = _GDIPlus_FontFamilyCreate($font)
    $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style)
    $hFormat = _GDIPlus_StringFormatCreate()
    $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)

    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
	$iLength = DllStructGetData($aInfo[0], "Width")+ 30
	$iHeight = DllStructGetData($aInfo[0], "Height")
    print(Int($iLength))
	print($iHeight)
	Local $array[1][2] = [[Int($iLength),Int($iHeight)]]
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hGDIFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Return $array
EndFunc

Func print($s)
    ConsoleWrite($s & @CRLF)
EndFunc

Func quit()
    Exit
EndFunc

Re: [..] Adapter la taille d'un Label à son contenu

Posté : ven. 13 mai 2016 16:30
par GMH
Le rendu est de meilleure qualité avec votre code. Merci à vous.
Toutefois, je bagarre avec ma boucle.

Re: [..] Adapter la taille d'un Label à son contenu

Posté : ven. 13 mai 2016 18:42
par walkson

Code : Tout sélectionner

#NoTrayIcon
Opt("GuiOnEventMode", 1)

#include <GuiConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>
$fontName = "Arial" ;"Baskerville Old Face"
$fontSize = 9
;$text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT   #"
$text = "This is a long text"
$split = StringSplit($text," ")
$text1 = "Ceci est un long texte"
$split1 = StringSplit($text1," ")
$guiMain = GUICreate("", 50 , 200)
GUISetOnEvent($gui_event_close, "quit")
GUISetFont($fontSize, 400, 0, $fontName, $guiMain)
$len = getTextLengthInPixel($text, $fontName, $fontSize, 2) ; bold
WinMove($guiMain,"",50,Default,$len[0][0]+200,$len[0][1]+50)
$left = 10
For $i = 1 To $split[0]
	If $split[$i] = "" Then ContinueLoop
$len = getTextLengthInPixel($split[$i], $fontName, $fontSize, 2)
GUICtrlCreateLabel($split[$i], $left, 5, $len[0][0], $len[0][1], $ss_leftnowordwrap)
GUICtrlSetFont(-1, $fontSize, 600)
GUICtrlSetBkColor(-1,0xFFFF00)
GUICtrlSetTip(-1,$split1[$i])
$left = $left + $len[0][0] + 5
Next

GUISetState()
While True
    Sleep(1000)
WEnd

Func getTextLengthInPixel($text, $font, $size = 9, $style = 0)
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain)
    $hFamily = _GDIPlus_FontFamilyCreate($font)
    $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style)
    $hFormat = _GDIPlus_StringFormatCreate()
    $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)

    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
	$iLength = DllStructGetData($aInfo[0], "Width")+ 30
	$iHeight = DllStructGetData($aInfo[0], "Height")
    print(Int($iLength))
	print($iHeight)
	Local $array[1][2] = [[Int($iLength),Int($iHeight)]]
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hGDIFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Return $array
EndFunc

Func print($s)
    ConsoleWrite($s & @CRLF)
EndFunc

Func quit()
    Exit
EndFunc
;_Control_SetWidth2Text($Gui, "", $label3, 5);5 pixels of trailing pad
Func _Control_SetWidth2Text($hWin, $sWinText, $hCtrl, $iPad = 0);author: ResNullius, based on PaulIA/GaryFrost (listview udf)
    Local $hWnd, $hDC, $hFont, $iMax, $tSize, $sText, $aCtrlPos, $ctrlX, $ctrlY, $ctrlW, $ctrlH
    If Not IsHWnd($hCtrl) Then
        $hWnd = ControlGetHandle($hWin, $sWinText, $hCtrl)
    Else
        $hWnd = $hCtrl
    EndIf
    If IsHWnd($hWnd) Then
        $hFont = _SendMessage($hWnd, $WM_GETFONT)
        $hDC = _WinAPI_GetDC($hWnd)
        _WinAPI_SelectObject($hDC, $hFont)
        $sText = ControlGetText($hWin, $sWinText, $hCtrl)
        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sText & " ")
        If DllStructGetData($tSize, "X") > $iMax Then
            $iMax = DllStructGetData($tSize, "X") + $iPad
            $aCtrlPos = ControlGetPos($hWin, $sWinText, $hCtrl)
            $ctrlX = $aCtrlPos[0]
            $ctrlY = $aCtrlPos[1]
            $ctrlW = $aCtrlPos[2]
            $ctrlH = $aCtrlPos[3]
            ControlMove($hWin, $sWinText, $hCtrl, $ctrlX, $ctrlY, $iMax, $ctrlH)
        EndIf
        _WinAPI_SelectObject($hDC, $hFont)
        _WinAPI_ReleaseDC($hWnd, $hDC)
    EndIf
EndFunc;==>_Control_SetWidth2Text
Après ça, on comprend tout de suite que je ne suis pas prof d'anglais :P

Re: [..] Adapter la taille d'un Label à son contenu

Posté : ven. 13 mai 2016 19:22
par GMH
Après ça, on comprend tout de suite que je ne suis pas prof d'anglais
Moi non plus... Je suis prof de langues anciennes (latin/grec) !

Je suis admiratif de l'efficacité de votre code, bien meilleur que le mien. Grand merci à vous !