Line length without TextWidth()

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Line length without TextWidth()

Post by AZJIO »

Is there a WinAPI function to get the length of a string. Using TextWidth() increases the executable by 40 kb. It was 60 kb, it became 100 kb.

Code: Select all

If CreateImage(0, 20, 20) And StartDrawing(ImageOutput(0))
	ForEach Drive()
		tmp = TextWidth(Drive())
		If tmp > TxtWidth
			TxtWidth = tmp
		EndIf
	Next
	StopDrawing()
	FreeImage(0)
EndIf
If TxtWidth < 520
	TxtWidth = 520
EndIf
SendMessage_(GadgetID(#Combo1), #CB_SETDROPPEDWIDTH, TxtWidth, 0)
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Line length without TextWidth()

Post by jacdelad »

GetTextExtentPoint32 should do this, but I haven't used it yet.
https://learn.microsoft.com/en-us/windo ... ntpoint32a
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Line length without TextWidth()

Post by RASHAD »

Why not ?

Code: Select all

 StartDrawing(WindowOutput(0))
 	DrawingFont(FontID(0))
	ForEach Drive()
		tmp = TextWidth(Drive())
		If tmp > TxtWidth
			TxtWidth = tmp
		EndIf
	Next
	StopDrawing()
EndIf
If TxtWidth < 520
	TxtWidth = 520
EndIf
SendMessage_(GadgetID(#Combo1), #CB_SETDROPPEDWIDTH, TxtWidth, 0)
Using Windows API

Code: Select all

hdc = StartDrawing(WindowOutput(0))
  SelectObject_(hdc, FontID(0))
	ForEach Drive()
	  txt$ = Drive()
	  GetTextExtentPoint32_(hdc,@txt$,Len(txt$),*sz.SIZE)
		If *sz\cx > TxtWidth
			TxtWidth = *sz\cx
		EndIf
	Next
StopDrawing()
EndIf
If TxtWidth < 520
	TxtWidth = 520
EndIf
SendMessage_(GadgetID(#Combo1), #CB_SETDROPPEDWIDTH, TxtWidth, 0)
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Line length without TextWidth()

Post by jacdelad »

Stupid question, but is it possible without using the internal 2DDrawing library (aka StartDrawing/StopDrawing)?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Line length without TextWidth()

Post by RASHAD »

Hi jacdelad

Code: Select all

hdc = GetDC_(WindowID(0))
  font = GetStockObject_(#DEFAULT_GUI_FONT)
  oldFont = SelectObject_(hdc, font)  
  SelectObject_(hdc, FontID(0))
	ForEach Drive()
	  txt$ = Drive()
	  GetTextExtentPoint32_(hdc,@txt$,Len(txt$),*sz.SIZE)
		If *sz\cx > TxtWidth
			TxtWidth = *sz\cx
		EndIf
	Next
  SelectObject_(hdc, oldFont)
  ReleaseDC_(WindowID(0), hdc)
EndIf
If TxtWidth < 520
	TxtWidth = 520
EndIf
SendMessage_(GadgetID(#Combo1), #CB_SETDROPPEDWIDTH, TxtWidth, 0)
Egypt my love
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: Line length without TextWidth()

Post by AZJIO »

StartDrawing added 10kb. The last example returned the dimensions to their original state, no overhead. Experimenting with BootSectGui
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Line length without TextWidth()

Post by mk-soft »

With Dummy Gadget

Code: Select all

;-TOP

Procedure GetTextWidth(String.s, FontID = #PB_Default)
  Static tmpGadget
  If Not tmpGadget Or Not IsGadget(tmpGadget)
    tmpGadget = TextGadget(#PB_Any, 0, 0, 0, 0, "")
  EndIf
  SetGadgetFont(tmpGadget, FontID)
  SetGadgetText(tmpGadget, String)
  ProcedureReturn GadgetWidth(tmpGadget, #PB_Gadget_RequiredSize)
EndProcedure

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("File")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    LoadFont(0, "Arial", 32)
    Debug GetTextWidth("Hello World!")
    Debug GetTextWidth("Hello World!", FontID(0))
    Debug GetTextWidth("Hello World!")
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Line length without TextWidth()

Post by RASHAD »

I will check BootSectGUI later
Next is a snippet to test

Code: Select all

LoadFont(0,"Consolas",12)
OpenWindow(0, 0, 0, 400, 300, "Text Testing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
hdc = GetDC_(WindowID(0))
  font = GetStockObject_(#DEFAULT_GUI_FONT)
  oldFont = SelectObject_(hdc, font)
  txt$ = "This is a TEST"
  LoadFont(0,"Consolas",12)  
  SelectObject_(hdc, FontID(0))	
	GetTextExtentPoint32_(hdc,@txt$,Len(txt$),sz.SIZE)
	Debug sz\cx
	LoadFont(0,"Segoe UI",12)
	SelectObject_(hdc, FontID(0))	
	GetTextExtentPoint32_(hdc,@txt$,Len(txt$),sz.SIZE)
	Debug sz\cx
SelectObject_(hdc, oldFont)
ReleaseDC_(WindowID(0), hdc)

Repeat        
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow
      Quit = 1

  EndSelect         
Until Quit = 1
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Line length without TextWidth()

Post by jacdelad »

Thanks RASHAD and mk-soft, this is great!
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply