SetTextGadgetAutosize(Gadget, State)

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

SetTextGadgetAutosize(Gadget, State)

Post by Dr. Dri »

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.

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
Dri ;)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Post by dobro »

Another methode
no api , + short , and colour code :D


  ; PureBasic Visual Designer v3.92 build 1460

;- Window Constants
Declare Open_message(text$,taille)

Enumeration
     #message
     #Police
     #Button_0
     #Text_0
EndEnumeration

text$= "hello " + Chr (10)+ Chr (13)+ "comment vas tu ?" ; <----------------------------ICI ADD text
taille=35 ; <----------------------------ICI change
Open_message(text$,taille)



; ******************* boucle principale ****************
Repeat ; Start of the event loop
    Event = WaitWindowEvent () ; This line waits until an event is received from Windows
     WindowID = EventWindowID () ; The Window where the event is generated, can be used in the gadget procedures
     GadgetID = EventGadgetID () ; Is it a gadget event?
     EventType = EventType () ; The event type
     If Event = #PB_EventGadget
         If GadgetID = #Button_0
             End
         EndIf
     EndIf
Until Event = #PB_Event_CloseWindow ; End of the event loop
End
;**********************************



Procedure Open_message(text$,taille)
     FontID = LoadFont ( #Police , "Comic Sans MS" , taille, #PB_Font_Bold )
    long=( Len (text$)*taille)
     If OpenWindow ( #message , 10, 10,long, 50+long/8, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar , "message" )
         If CreateGadgetList ( WindowID ())
             ButtonGadget ( #Button_0 , 0, WindowHeight ()-30, 60, 30, "ok" )
             TextGadget ( #Text_0 , 30, (taille/8 ), 140+long, taille*200, text$)
             SetGadgetFont ( #Text_0 , FontID )
         EndIf
     EndIf
EndProcedure
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

dobro:

Did you color your code yourself, or did you have a tool to do that?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

The color looks nice, but what about all the extra bandwidth used?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Bonne_den_kule wrote:dobro:
Did you color your code yourself, or did you have a tool to do that?
It's a tool from the french forum : http://purebasic.hmt-forum.com/viewtopic.php?t=3875
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply