SetTextGadgetAutosize(Gadget, State)
Posted: Mon Dec 26, 2005 11:17 pm
Code updated For 5.20+
Those functions make a (PB native) TextGagdet resize automatically. It even works with multiline strings and custom fonts. In this example the window takes the size of the gadget.
Dri 
Those functions make a (PB native) TextGagdet resize automatically. It even works with multiline strings and custom fonts. In this example the window takes the size of the gadget.
Code: Select all
Procedure AutosizeCallback(WindowID, Message, wParam, lParam)
Protected WndProc, Gadget, Size.Size
Protected Text.s, Field.s, Count, i, w, h
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, Autosize)
Protected GadgetID, WindowID, WndProc
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,"Autosize",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
label.s = "Auto size"
label + #CRLF$ + "ceci est un exemple"
label + #CRLF$ + "c'est simple"
TextGadget(0, 0, 0, 250, 20, label)
;change de police pour le fun
LoadFont(0, "Verdana", 28) ;<- change values ;)
SetGadgetFont(0, FontID(0))
;autosize le gadget
SetTextGadgetAutosize(0, #True)
Repeat
If WindowWidth(0) <> GadgetWidth(0) Or WindowHeight(0) <> GadgetHeight(0)
ResizeWindow( 0,0,0,GadgetWidth(0), GadgetHeight(0) )
EndIf
Until WaitWindowEvent()=#PB_Event_CloseWindow
DeleteObject_(hBrush)
EndIf
