Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePrint a string on the printer with word wrap
Keywordsprint, printer, word wrap, TextWidth
CategoriesGraphics
 
Add words to a string one at a time. Use TextWidth to see how wide the string is. When it is as wide as will fit, print it and start a new string.
 
' Print the text with wrapping.
Private Sub WrapText(ByVal txt As String, ByVal xmin As _
    Single, ByVal xmax As Single, ByVal ymin As Single, _
    ByVal draw_box As Boolean)
Dim X As Single
Dim Y As Single
Dim xmargin As Single
Dim ymargin As Single
Dim line_wid As Single
Dim new_line As String
Dim new_word As String

    ' Convert non-printable characters to spaces.
    NonPrintToSpace txt
    
    ' If we should draw a box, add a small margin.
    If draw_box Then
        xmargin = Printer.TextWidth("x") / 2
        ymargin = Printer.ScaleY(Printer.Font.size * 0.5, _
            vbPoints, Printer.ScaleMode)
        xmin = xmin + xmargin
        xmax = xmax - xmargin
        ymin = ymin + ymargin
    End If
    line_wid = xmax - xmin

    ' Start printing.
    Printer.CurrentY = ymin
    Printer.CurrentX = xmin
    new_word = GetWord(txt)
    Do
        ' Start with the last word examined.
        ' Note that this loop prints at least one
        ' word per line. That is important if the
        ' text contains a word too long to fit on
        ' a line.
        new_line = new_word
        Do
            ' Get the next word.
            new_word = GetWord(txt)
            If new_word = "" Then Exit Do

            ' See if the new word fits.
            If Printer.TextWidth( _
                new_line & " " & new_word) > _
                    line_wid _
                Then Exit Do

            ' It fits. Add it to the line.
            new_line = new_line & " " & new_word
        Loop

        ' Display the line. This moves CurrentX to
        ' zero and CurrentY to the next line.
        Printer.Print new_line
        If txt = "" Then Exit Do

        ' Reset CurrentX to our left margin.
        Printer.CurrentX = xmin
    Loop
    
    ' Draw the box if desired.
    If draw_box Then
        xmin = xmin - xmargin
        xmax = xmax + xmargin
        ymin = ymin - ymargin
        Printer.Line (xmin, ymin)- _
            (xmax, Printer.CurrentY + ymargin), , B
    End If
End Sub

' Convert non-printable characters into spaces.
Private Sub NonPrintToSpace(txt As String)
Dim i As Integer
Dim txtlen As Integer
Dim ch As String

    txtlen = Len(txt)
    For i = 1 To txtlen
        ch = Mid$(txt, i, 1)
        If ch < " " Or ch > "~" _
            Then Mid$(txt, i, 1) = " "
    Next i
End Sub

' Return the next word from this string. Remove
' the word from the string.
Private Function GetWord(txt As String) As String
Dim pos As Integer

    txt = Trim$(txt)
    pos = InStr(txt, " ")
    If pos < 1 Then
        GetWord = txt
        txt = ""
    Else
        GetWord = Left$(txt, pos - 1)
        txt = Trim$(Right$(txt, Len(txt) - pos))
    End If
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated