advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
How do you feel about VB's native printing capabilities? Should Microsoft have gone to greate lengths to provide printer support? Do you rely on third party controls such as Crystal Reports to provide better printing support in your applications? How do you feel about this roll-your-own approach? What else do you need? Let us know in the VB Classic discussion forum.
Partners & Affiliates
advertisement
advertisement
Expand Your VB6 Printing Repertoire—Part I (cont'd)
Simple Printing with VB6
With those basics in mind, you can start an exploration of the basic printing mechanisms of VB6 with a simple project that prints strings and lines. It's the SimplePrinting project, whose main form is shown in Figure 4. The Preview Text and Print Text buttons generate the same output on the form and the printer respectively. They both call the CreateSimplePrintout() subroutine to print the text you see on the form in Figure 4.


 
Figure 4. Simple Print Preview: The figure shows the main form from the SimplePrinting project, with aligned text.
Both buttons use the same PRN object variable, as described earlier in the Printout Preview section. You declare the variable at the form's level with the following statement:

 Dim PRN As Object
The code behind the two buttons is quite simple. It sets up the PRN object and then sets the ScaleMode property of the output device to 6, so that you can express dimensions and distances in millimeters. The CreateSimplePrintout() subroutine draws the various graphics elements using the PRN object. If PRN represents the Printer object, the output is sent to the printer. If the same variable represents the Form object, you'll get a preview of the printout on the application's main form.

 Private Sub cmdPrinter_Click()
Set PRN = Printer
PRN.ScaeMode = 6
CreateSimplePrintout
Printer.EndDoc
End Sub
Private Sub cmdPrnForm_Click()
Set PRN = frmPrintout
PRN.ScaleMode = 6
CreateSimplePrintout
End Sub
Rather than showing the listing of the CreateSimplePrintout() subroutine, it's useful to go through the groups of statements that generate the various elements on the form. To print text you call the Print method passing the string to be printed as an argument. The Print method prints the string at the current location on a single line. If the string doesn't fit in the width of the page, only part of it will print; the Print method doesn't wrap long strings on multiple lines. As a result, you must make sure that the string will fit in the available area, or break it into multiple lines from within your code and call the Print method for each line.

After printing the string, the Print method resets the current location to the beginning of the following line on the page. The CurrentX property value is set to 0 and the CurrentY property value is increased by the height of the current line. Of course, you can override those values to set the current location of each line from within your code.

The Printer object uses its current font to render the string on the printed page. You can change the font by setting the Printer's Font property. Once set, a font remains in effect until you change it. For example, the sample code prints the title and the first two lines shown in Figure 4 using the following statements:

 PRN.CurrentX = 30: PRN.CurrentY = 15
' Set title font
Dim fnt As New StdFont
fnt.Name = "Verdana": fnt.Size = 14: fnt.Bold = True
Set PRN.Font = fnt
PRN.Print "Simple Printout Demo"
PRN.Print
' Use smaller size for text
fnt.Size = 12: fnt.Bold = False
Set PRN.Font = fnt
PRN.Print "Print and preview formatted text. " & _
"If the text printed with a single call to " & _
"the Print method exceeds " & _
"the width of the form, the text isn't " & _
"wrapped automatically."
PRN.Print
PRN.Print "However, every time you call the " & _
"Print method, the text starts at the far " & _
"left edge of the form on the following line."
Notice that long strings are only partially printed. The following statements produced the text in italics, which fits better across the page. The same text was printed with several calls to the Print method, each time with a short string as argument (I've used ellipses in the place of the strings to make the listing shorter; you can always examine the project's code to see the full text of the strings).

 ' Use another font for formatted text
fnt.Size = 10: fnt.Italic = True
Set PRN.Font = fnt
' Set the left margin.
PRN.CurrentX = 20
PRN.Print "..."
PRN.CurrentX = 20
PRN.Print ""...""
PRN.CurrentX = 20
PRN.Print ""...""
' Call Print method to advance vertically
PRN.Print
PRN.CurrentX = 20
PRN.Print "..."
PRN.CurrentX = 20
PRN.Print "..."
PRN.CurrentX = 20
PRN.Print "..."
Next, here are some techniques for printing aligned text. After you know the width of the page and the width of the string, you can align the string in any way you wish within a given width. To left align a string, you simply set the current location to the page's left edge, or the left margin. To right align a string, you subtract the string's width from the page's width and move that many units to the right before you print. Likewise, to center a string, you move to the right by the half of that difference. The following statement prints a horizontal line across the page at the CurrentY location, leaving a space of 10 millimeters on each sides of the page:

 PRN.Line (10, PRN.CurrentY)-(PRN.ScaleWidth - 10,
PRN.CurrentY)
The three aligned strings were printed with the following statements:

 ' Printing Aligned Text
' Print left aligned text
' (no special action required)
Dim str As String
str = "Left aligned Text"
PRN.CurrentX = 0
YPos = PRN.CurrentY
PRN.CurrentY = PRN.CurrentY + 5
PRN.Print str
' Print centered text
str = "Centered Text"
PRN.CurrentX = (PRN.ScaleWidth --
PRN.TextWidth(str)) / 2
PRN.CurrentY = YPos
PRN.Print str
' Print right aligned text
str = "Right Aligned Text"
PRN.CurrentX = PRN.ScaleWidth - PRN.TextWidth(str)
PRN.CurrentY = YPos
PRN.Print str
As you can see, it's fairly straightforward to align strings on the page. Notice how all three strings were printed on the same line, because the preceding code resets the current location's vertical coordinate to the same value, using the YPos variable. This approach is based on the assumption that the string fits across the page (or in the width of a given rectangle). If the string's width exceeds the available width, you must break the original string into multiple lines of text, each one as long as possible, but not exceeding the available width. Usually, you'll want to break text at word boundaries; so it's essential to use a reliable algorithm for breaking long strings into multiple text lines.

What's Next?
In the remainder of this solution series, you'll see two different techniques for printing text in a rectangle with a specific width (the height of the rectangle can't be fixed; it depends on the number of text lines in which the original string is broken). In the next part of this series I'll present two practical utilities for printing the text and tabular data shown in Figure 1 and Figure 2.

Previous Page: Printout Preview  


Evangelos Petroutsos is a long time VB developer. When he's not writing code, he writes programming books and articles. His most recent title is Mastering Visual Basic .NET, published by Sybex. Reach him by e-mail here.
Page 1: What's in this Solution Series?Page 4: Printout Preview
Page 2: Basic Printing ConceptsPage 5: Simple Printing with VB6
Page 3: Coordinate Systems 
DevX is a division of Internet.com.
© Copyright 2010 Internet.com. All Rights Reserved. Legal Notices
advertisement