Page 1 of 2

PxLenght of Text in String/TextGadget

Posted: Mon May 26, 2003 11:13 am
by Searhin
Hi,

is there a way to determine the length (in pixels- not characters) of a text to be displayed in a StringGadget or TextGadget?

I want to display text of varying lengths in deactivated StringGadgets. Long Texts should be displayed in MultiLine Gadgets, short text in single lines to save space. Problem is, i dunno how to find out the number of lines i need for a given text.

thanx,
Searhin

Posted: Mon May 26, 2003 12:08 pm
by El_Choni

Code: Select all

Procedure TextWidth(text$) 
  GetTextExtentPoint32_(GetDC_(0), text$, Len(text$), sz.SIZE) 
  ProcedureReturn sz\cx 
EndProcedure

Posted: Mon May 26, 2003 2:18 pm
by Searhin
WOW!

thanx - that's even easier than i thought :lol:

Posted: Mon May 26, 2003 3:35 pm
by El_Choni
Have in mind that it gives width for text with the default GUI font. If you change font, you should use this (after UseFont() or SetGadgetFont()):

Code: Select all

Procedure TextWidth(text$, WindowID)
  hDC = GetDC_(WindowID)
  GetTextExtentPoint32_(hDC, text$, Len(text$), sz.SIZE)
  ReleaseDC_(WindowID, hDC)
  ProcedureReturn sz\cx 
EndProcedure

Posted: Mon May 26, 2003 4:55 pm
by Searhin
ah, okay - now it gives the correct results

thanx again

Posted: Mon May 26, 2003 8:41 pm
by Fred
None API based one:

Code: Select all

If StartDrawing(WindowOutput())
  Length = TextLength("Hahah")
  StopDrawing()
EndIf

Posted: Mon May 26, 2003 11:54 pm
by El_Choni
:oops:

I had asked for this function several times, and it was already there. Sorry.

Posted: Tue May 27, 2003 11:05 am
by Searhin
being most embarassed... :oops:

Posted: Tue May 27, 2003 3:03 pm
by Fred
No problem guys ;)

Posted: Tue May 27, 2003 5:06 pm
by GPI
A TextHeight(text$) - function would be nice.

Posted: Wed May 28, 2003 5:20 am
by PB
> A TextHeight(text$) - function would be nice.

Try this:

viewtopic.php?t=3801

Not sure if it does what you want, but the value of "tm\tmHeight" that it
returns might be the height of the text. Give it a try...

Posted: Wed May 28, 2003 12:45 pm
by El_Choni
This works with height, too:

Code: Select all

Procedure TextHeight(text$, WindowID)
  hDC = GetDC_(WindowID) 
  GetTextExtentPoint32_(hDC, text$, Len(text$), sz.SIZE) 
  ReleaseDC_(WindowID, hDC) 
  ProcedureReturn sz\cy
EndProcedure
Now, don't tell me there was also a TextHeight() command in PB ;)

Posted: Wed May 28, 2003 1:12 pm
by Fred
I guess it will be soon then ;)

Posted: Thu Oct 21, 2004 12:57 pm
by Gansta93
It would be better if we wouldn't have to do StartDrawing() and StopDrawing() to know the width in pixels of a text. It would be faster. Time is monay :-).

Posted: Tue Jul 18, 2006 9:44 pm
by Trond
Fred wrote:None API based one:

Code: Select all

If StartDrawing(WindowOutput())
  Length = TextLength("Hahah")
  StopDrawing()
EndIf
That doesn't do what he asked for because the default font inside StartDrawing() and StopDrawing() is different from the default GUI font.