voila ce que j'ai réussi à coder... ca ne fonctionne correctement que pour les chaines de caractere sans retour a la ligne
Code : Tout sélectionner
Procedure AutosizeCallback(WindowID.l, Message.l, wParam.l, lParam.l)
Protected WndProc.l, Gadget.l, Size.Size
Protected Text.s, Field.s, Count.l, i.l, w.l, h.l
WndProc = GetProp_(WindowID, "AutosizeWndProc")
If Message = #WM_CTLCOLORSTATIC And GetProp_(lParam, "AutosizeValue")
Gadget = GetDlgCtrlID_(lParam)
Text = GetGadgetText(Gadget)
;vire les retours charriot
Text = ReplaceString(Text, #CRLF$, #LF$)
Text = ReplaceString(Text, #CR$, #LF$)
Count = CountString(Text, #LF$) + 1
For i = 1 To Count
Field = StringField(Text, i, #LF$)
GetTextExtentPoint32_(wParam, Field, Len(Field), Size)
If w < Size\cx
w = Size\cx
Size\cx = 0
EndIf
Next i
h = Size\cy * Count
ResizeGadget(Gadget, #PB_Default, #PB_Default, w, h)
EndIf
ProcedureReturn CallWindowProc_(WndProc, WindowID, Message, wParam, lParam)
EndProcedure
Procedure SetTextGadgetAutosize(TextGadget.l, Autosize.l)
Protected GadgetID.l, WindowID.l, WndProc.l
If IsGadget(TextGadget)
GadgetID = GadgetID(TextGadget)
SetProp_(GadgetID, "AutosizeValue", Autosize)
WindowID = GetParent_(GadgetID)
If GetProp_(WindowID, "AutosizeWndProc") = #Null
WndProc = SetWindowLong_( WindowID, #GWL_WNDPROC, @AutosizeCallback() )
SetProp_(WindowID, "AutosizeWndProc", WndProc)
EndIf
InvalidateRect_(GadgetID, #Null, #True)
EndIf
EndProcedure
If OpenWindow(0,0,0,270,160,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"TextGadget") And CreateGadgetList(WindowID(0))
label.s = "Auto size"
label + #CRLF$ + "ceci est un exemple"
label + #CRLF$ + "c'est simple"
TextGadget(0, 10, 10, 250, 20, label)
TextGadget(1, 10, 50, 250, 20, label)
;change de police pour le fun
LoadFont(0, "Verdana", 18)
SetGadgetFont(0, UseFont(0))
SetGadgetFont(1, UseFont(0))
;change la couleur de la fenetre pour avoir un visuel
hBrush = CreateSolidBrush_($FFFFFF)
SetClassLong_(WindowID(), #GCL_HBRBACKGROUND, hBrush)
InvalidateRect_(WindowID(), #Null, #True)
;autosize le 2e gadget
SetTextGadgetAutosize(1, #True)
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
DeleteObject_(hBrush)
EndIf
[edit]
le bug semble venir de stringfield
[edit]
j'ai changé les #CRLF$ en #LF$ et ca fonctionne, je pense que c'est soit un bug de stringfield, soit un truc qui manque dans l'aide
[edit]
c'est ma faute, pas de bug dans stringfield ^^, je modifirai le code en conséquence
[edit]
j'avais oublié de virer les Debug
Dri