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

get font character glyph width

Status
Not open for further replies.

reg1965

Programmer
Mar 12, 2005
5
0
0
GB
hi all
is it possible in vb.net to obtain the true font character width without the leading and following spaces ie to allow for overhanging characters. i have tried graphics.measurestring but this is the total size of the true type font width. i havent tried graphics.measureCharacterRanges because you have to enter the bounds which if too small return max characters which will fit and wrapping info.
any help will be much appreciated
steve
 
I used this to get line wraps..
It looks at how wide the form is and then figures out where the last space was, backs up to that space, truncates the string to the point of the space and then adds an new line..

(actually it creates an array of strings that then get placed into seperate lables) - for the purpose of scrolling information up a form.


Code:
Dim i As New Bitmap(1, 1)
        Dim g As Graphics = Graphics.FromImage(i)
        Dim strMessage As String = dr("Description").ToString
        'We need to know when to line wrap..
        'So.. measure the string and when it gets to long add a new item to the array.
        Dim intWidth As Integer = Me.Width, intCurPos As Integer = 0, szStrWidth As SizeF
        Dim sToReturn As String
        Dim aryReturn(1) As String
        sToReturn = [red] Enter your string or variable here[/red]
        For intChar As Integer = 1 To strMessage.Length - 1
            szStrWidth = g.MeasureString(strMessage.Substring(intCurPos, intChar - intCurPos), f)
            If szStrWidth.Width > intWidth Then
                'Split the string (at the last space :)
                Dim iWhere As Integer = strMessage.LastIndexOf(" ", intChar) ', (intChar - intCurPos)) ' is the space?
                sToReturn &= strMessage.Substring(intCurPos, iWhere - intCurPos) & "|"
                intCurPos = iWhere + 1 'Trim of the space...
            End If
        Next
        sToReturn &= strMessage.Substring(intCurPos)
        dim sArray() as string = sToReturn.split("|")

HTH


Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top