Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help determining string length in pixels

Status
Not open for further replies.

vfp7guy

Programmer
Jul 2, 2002
45
CA
Can anybody help me with this one.
On a standard form, I have a label that I dynamically change & refresh the .Caption value depending on some selected criteria from a multi-selection grid. The .Caption value will be a comma-delimited character string of abbreviated codes - typically 2-3 characters each.
e.g., "Tst2, BR, M, CTH".

The label size in pixels is 164 x 17, and uses MS Sans Serif (9) font.

My problem arises if the user has selected more codes than will display in this fixed-length label.

I want to be able to determine whether or not the character string will fit in the 164 display width (based on the actual characters in the string and the fact that MS Sans Serif is a variable-width character font), and, if it won't fit, I want to truncate the string at just the correct character position and add a continuation ellipsis (...) at the end so that the user knows that there are additional codes that have been selected.

Any ideas here?
 
Check TXTWIDTH() function in HELP

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Thanks Boris ... that works great.
I remember trying this before in an older version of VFP, but it wasn't always accurate. It seemed to take some average sizing for a variable-pitch font, but I now get accurate sizing in VFP9.
 
More on this ... found after incorporating into my app.

It is still workable, but with a little modification.
TXTWIDTH() actually returns the number of characters "of average size" for that particular string based on the Font used. I had to use FONTMETRIC(6,"MS SANS SERIF",9) to get the average size in pixels used for my font. Return was 5.

I divided my label width (164 pixels) by 5 to get 32 "average" characters maximum width, and loop through the TXTWIDTH() function, truncating any oversize string a character at a time until the returnvalue was < 32.

It works OK now ...
 
Use the TextWidth method and multiply by FontMetric(6, ...) to get the exact width.

Tamar
 
VFP7Guy,

I see you've solved the problem, but if anyone else needs to do this, here is a little routine that will do the job:

Code:
FUNCTION GetTextSize

* Determines the width in pixels of a given text string,
* based on a given font, font style and point size.

* Parameters: text string, font name, size in points, 
* font style in format used by FONTMETRIC() 
* (e.g. "B" for bold, "BI" for bold italic;
* defaults to normal).	

LPARAMETERS tcString, tcFont, tnSize, tcStyle

LOCAL lnTextWidth, lnAvCharWidth

IF EMPTY(tcStyle)
  tcStyle = ""
ENDIF 
	
lnTextWidth = TXTWIDTH(tcString, tcFont, tnSize, tcStyle)
lnAvCharWidth = FONTMETRIC(6, tcFont, tnSize, tcStyle)

RETURN lnTextWidth * lnAvCharWidth

ENDFUNC

Hope this is of interest.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Me fails to see how use of average character width could get exact pixel results.
However, it may be just accurate enough for your task at hand.
If not, I suggest using windows API call for determining exact pixel length
People say things goes worse for Italic fonts, so you may want this as well:
 
Tsh73,

Me fails to see how use of average character width could get exact pixel results.

For body text (that is, most normal text), the average character width is indeed a reliable measure. In fact, it has been used by topographers for centuries as a convenient way of estimating the length of a piece of text.

People say things goes worse for Italic fonts

Not so. FONTMETRIC()takes account of the font, and makes a suitable adjustment for both bold and talic.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Thanks Mike (and everyone else) for the input ... it works perfectly now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top