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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text width problem

Status
Not open for further replies.

happyabc

IS-IT--Management
Mar 10, 2004
164
IN

This function is supposed to return the max possible width of a string consisting of the given nof. characters but it it is returning too large a value:

Public Function MaxTextWidth(hwndWindow As Long, lngNofCharacters As Long) As Single

'-------------------------------------------------------------------
'returns the max. width ( in twips ) that would be occupied by the given nof. characters
'-------------------------------------------------------------------

On Error GoTo errHandler

Const strProcedure As String = "MaxTextWidth"

Dim cdsTmp As TEXTMETRIC
Dim lngReturn As Long
lngReturn = GetDC(hwndWindow)
If lngReturn = 0 Then
Err.Raise GetLastError(), pProgID(strProcedure)
Else 'got DC handle
'so proceed to get the text metrics
If GetTextMetrics(lngReturn, cdsTmp) = 0 Then
Err.Raise GetLastError(), pProgID(strProcedure)
Else
MaxTextWidth = cdsTmp.tmMaxCharWidth * lngNofCharacters * Screen.TwipsPerPixelX
End If
End If

Exit Function

errHandler:

Dim lngErrNumber As Long: lngErrNumber = Err.Number
Dim strErrSource As String: strErrSource = Err.Source
Dim strErrDescription As String: strErrDescription = Err.Description
Dim strErrHelpFile As String: strErrHelpFile = Err.HelpFile
Dim strErrHelpContext As String: strErrHelpContext = Err.HelpContext

Select Case HandleErrors
Case bbRetry
Resume
Case bbIgnore
Resume Next
Case Else
Err.Raise lngErrNumber, strErrSource, strErrDescription
End Select
End Function

Why doesn't it work?
 
It does not work because most fonts that we generally use in Windows are proportional fonts. This means that each character takes no more than the necessary amount of width than it needs to be legible and look good (according to the font designer's aesthetics). eg.

iiiii

Both of the above consist of 5 characters in the same font, but the second line is more than double the width of the first.

GetTextmetrics.tmMaxCharWidth returns the width widest character in the specified font, thus the measurement returned will always be wider than the actual string (except if your string only consists of the widest charcter).

GetTextmetrics.tmAveCharWidth is an improvement, but still won't get you accurate results

(A search in this forum, however, should find you my solution that works accurately for any mixture of fonts you fancy in a line of text)
 
hap, do you remeber your thread222-806549? You might be able to incorporate some of what is in that thread.

zemp
 
Zemp: in that thread strongm showed a technique for finding the width of *certain strings*. Now I am having problem about a *certain numbers of characters*.

strongm: I'm searching.
 
Nope. Can't find any such thread ( other than may own referred to by Zemp but I've pointed out that the question is different ). 222-588023 is a superset of my new question but that was answered by CClint and the solution does not meet my needs as:
1. my grid will be charged again and again, so each column's width might be different every time
2. I can't risk some data not being displayed in certain columns ( which is what will happen when the "average character's" width is used.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top