Code: Select all
    If StartDrawing(WindowOutput(0))
      DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT))
      Length = TextWidth("Hahah") 
      StopDrawing() 
    EndIf

Code: Select all
    If StartDrawing(WindowOutput(0))
      DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT))
      Length = TextWidth("Hahah") 
      StopDrawing() 
    EndIf
Code: Select all
If OpenWindow(0,0,0,320,240,"test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  
  
  FontID = GetStockObject_(#DEFAULT_GUI_FONT)
  
  text$ = "haha, hoho"
  
  hDC = GetDC_(WindowID(0))
  SelectObject_(hDC, FontID)
  GetTextExtentPoint32_(hDC, text$, Len(text$), sz.SIZE) 
  ReleaseDC_(WindowID(0), hDC) 
  Debug sz\cx
  Debug sz\cy
  
  StartDrawing(WindowOutput(0)) 
  DrawingFont(FontID)
  Debug TextWidth(text$) 
  Debug TextHeight(text$)
  StopDrawing() 
  
  
EndIf

Yes, but not he uses API, and an API solution was already posted.netmaestro wrote:True, he missed a line:Code: Select all
If StartDrawing(WindowOutput(0)) DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT)) Length = TextWidth("Hahah") StopDrawing() EndIf
Code: Select all
Procedure writeTextGadget(GadgetLeft, GadgetTop, Text.s, GadgetNumber = #PB_Any, Flags = 0)
   ; creates a text-gadget sized according to its text
   ; returns gadget-number for #PB_Any or else gadget-id (as usual for gadget-creation)
   Protected ReturnValue
	
   ; to be able to use GetGadgetFont() when getting size gadget is created first and then resized
   ReturnValue = TextGadget(GadgetNumber, GadgetLeft, GadgetTop, 0, 0, Text, Flags)
   If GadgetNumber = #PB_Any
      GadgetNumber = ReturnValue
   EndIf
	
   If ReturnValue
      If StartDrawing(WindowOutput(GetActiveWindow()))
         DrawingFont(GetGadgetFont(GadgetNumber))
         ResizeGadget(GadgetNumber, #PB_Ignore, #PB_Ignore, TextWidth(Text), TextHeight(Text))
         StopDrawing()
         ProcedureReturn ReturnValue
      EndIf
   EndIf
	
   ProcedureReturn #False
EndProcedure