PxLenght of Text in String/TextGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

True, he missed a line:

Code: Select all

    If StartDrawing(WindowOutput(0))
      DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT))
      Length = TextWidth("Hahah") 
      StopDrawing() 
    EndIf
BERESHEIT
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

it works for me, just use DrawingFont() to calculate sizes from the correct font.

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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

too fast for me netmaestro :wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I have speed but I don't think I'll ever have the legendary Flype thoroughness!
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

netmaestro wrote:True, he missed a line:

Code: Select all

    If StartDrawing(WindowOutput(0))
      DrawingFont(GetStockObject_(#DEFAULT_GUI_FONT))
      Length = TextWidth("Hahah") 
      StopDrawing() 
    EndIf
Yes, but not he uses API, and an API solution was already posted.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

@trond
So just use FontID = GetGadgetFont(#Gadget) (when it's possible).
maybe a Get/SetWindowFont() would be useful ?

@netmaestro
ah yes, you're right. iwas not sure but now that you said it :
http://www.googlefight.com/index.php?la ... netmaestro
:lol:

i have to say that the 'trond' skills easily beats me :
http://www.googlefight.com/index.php?la ... ord2=trond

but, hey, who's the boss ?
http://www.googlefight.com/index.php?la ... ord2=trond

:wink:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Flype wrote:@trond
So just use FontID = GetGadgetFont(#Gadget) (when it's possible).

:wink:
Ingenious, thank you.
andrasch
New User
New User
Posts: 9
Joined: Wed Sep 21, 2005 2:43 pm
Location: Germany

Post by andrasch »

tried to make a handy procedure out of it, may be you like it.

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
Post Reply