Browse DevX
Sign up for e-mail newsletters from DevX


Expand Your VB6 Printing Repertoire—Part I-2 : Page 2

Basic Printing Concepts
To print with VB6 you must understand a few basic concepts. For typical business applications you need to focus on printing text, lines and frames. Printing text is a straightforward process: you first set up a scale (i.e., whether a millimeter on the paper corresponds to a meter or a mile of the original), determine the dimensions of the strings you want to print and then select a starting point for each string. The following sections explain concepts you'll use throughout this solution series.

The Printer Object
This object represents the printer, as well as the page on which you'll print. The Printer object exposes basic printer properties, such as the page's dimensions and orientation, the quality of the printout, and so on. It also exposes methods for printing basic shapes and text, just like the Form object and the PictureBox control. To draw something on the printer you can call any of the methods discussed in the next section. In addition to these printing methods, the Printer object exposes the NewPage method, which renders and emits the current page, and the EndDoc method, which emits the current page and terminates the printout. You can place graphics elements on the page in any sequence, because the Printer object generates the page's image in memory. In other words, you might just as easily print the footer and then the header rather than the more natural header-then-footer sequence. The Printer object renders the image to paper only when you call the NewPage method.

Graphics Methods
VB6 provides a few methods for drawing basic shapes like lines, rectangles, circles and ellipses (the Line and Circle methods), as well as a method for printing text (the Print method). The Form object and the PictureBox control also expose these methods. Each of these methods accepts the basic attributes of the shape they will print as arguments: the string to be printed, the starting and ending point of a line, or the location and radius of a circle. For Forms and PictureBoxes, the output goes to the screen; for the Printer object, it goes to the printer.

Figure 3. Visualizing a String's Width and Height: The rectangles shown were drawn using the return values of the TextWidth and TextHeight methods, which return the width and height that a string requires when drawn in an output device's current font.

To position text on the page, you need a mechanism to calculate the dimensions of strings. The methods TextWidth and TextHeight accept a string as argument and return the string's width and height when printed, respectively, taking into consideration the current font of the output device. If you draw a rectangle specified by these two numbers, it will enclose the entire area in which the string will print, leaving a sufficient margin above and below the characters to prevent consecutive lines of text from touching each other. Figure 3 shows a string printed in three different font sizes enclosed within rectangles determined using the results of calling TextWidth and TextHeight methods. You'll see how to use these methods to align text in columns.

Notice that the TextHeight method always returns a value that accommodates all characters, including upper and lower case characters and symbols. In other words, the method returns the same height regardless of the content of the string you pass to the TextHeight method. The TextWidth method, on the other hand, takes into consideration the widths of the individual characters. A string made up of w's is wider than a string made of i's.

Thanks for your registration, follow us on our social networks to keep up-to-date