Académique Documents
Professionnel Documents
Culture Documents
Visual Basic provides three techniques for printing text and graphics.
z You can produce the output you want on a form and then print the form using the PrintForm method.
z You can send text and graphics to a printer by setting the default printer to a member of the Printers collection.
z You can send text and graphics to the Printer object and then print them using the NewPage and EndDoc methods.
This section examines the advantages and disadvantages of these three approaches.
[form.]PrintForm
If you omit the form name, Visual Basic prints the current form. PrintForm prints the entire form, even if part of the form is not visible on the screen. If a form contains graphics, however, the graphics print only if the
form’s AutoRedraw property is set to True. When printing is complete, PrintForm calls the EndDoc method to clear the printer.
For example, you could send text to a printer by printing it on a form and then calling PrintForm with the following statements:
The PrintForm method is by far the easiest way to print from your application. Because it may send information to the printer at the resolution of the user’s screen (typically 96 dots per inch), results can be
disappointing on printers with much higher resolutions (typically 300 dots per inch for laser printers). The results may vary depending on objects on your form.
Regardless of which printing method you use, all printed output from a Visual Basic application is directed to the Printer object, which initially represents the default printer specified in the Windows Control Panel.
However, you can set the default printer to any one member in the Printers collection.
To select the printer from the collection, use the following syntax:
The following statements print the device names of all the printers on the operating system to the Immediate window:
Note You cannot create new instances of the Printer object in code, and you cannot directly add or remove printers from the Printers collection. To add or remove printers on your system, use the Windows Control Panel.
The Printer object provides the best print quality across a variety of printers because Windows translates text and graphics from the device-independent drawing space of the Printer object to best match the resolution
and abilities of the printer. You can also print multiple-page documents by using the NewPage method on the Printer object.
The main drawback to using the Printer object is the amount of code required to get the best results. Printing bitmaps on the Printer object also takes time and can therefore slow the performance of the application.