PxLenght of Text in String/TextGadget

Just starting out? Need help? Post your questions and find answers here.
Searhin
User
User
Posts: 41
Joined: Mon May 26, 2003 10:53 am
Location: Germany

PxLenght of Text in String/TextGadget

Post 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
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Code: Select all

Procedure TextWidth(text$) 
  GetTextExtentPoint32_(GetDC_(0), text$, Len(text$), sz.SIZE) 
  ProcedureReturn sz\cx 
EndProcedure
El_Choni
Searhin
User
User
Posts: 41
Joined: Mon May 26, 2003 10:53 am
Location: Germany

Post by Searhin »

WOW!

thanx - that's even easier than i thought :lol:
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post 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
El_Choni
Searhin
User
User
Posts: 41
Joined: Mon May 26, 2003 10:53 am
Location: Germany

Post by Searhin »

ah, okay - now it gives the correct results

thanx again
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

None API based one:

Code: Select all

If StartDrawing(WindowOutput())
  Length = TextLength("Hahah")
  StopDrawing()
EndIf
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

:oops:

I had asked for this function several times, and it was already there. Sorry.
El_Choni
Searhin
User
User
Posts: 41
Joined: Mon May 26, 2003 10:53 am
Location: Germany

Post by Searhin »

being most embarassed... :oops:
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

No problem guys ;)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

A TextHeight(text$) - function would be nice.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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...
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post 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 ;)
El_Choni
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I guess it will be soon then ;)
Gansta93
Enthusiast
Enthusiast
Posts: 238
Joined: Wed Oct 20, 2004 7:16 pm
Location: The Village
Contact:

Post 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 :-).
Be seeing you! :-)

Gansta93
If you speak french, you can visite Le Monde de Gansta93 (Gansta93's World)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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