Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


A few things are unique to the Printer object. Table 12.7 lists some Printer object-specific methods.

You should be aware that your printer will not support the same fonts as your screen display. Some fonts will be common to both, of course, including the Windows TrueType fonts; but you cannot be sure that a font available for the screen will also be available for the Printer object. To determine which fonts your Printer object has available, use the FontCount property to determine the number of fonts, then extract their names from the Fonts property. The following code, placed in a form’s Click event procedure, will load a List Box control with the names of all the Printer object’s fonts:

Private Sub Form_Click()

Dim i As Integer

For i = 0 To Printer.FontCount -1    ‘ Determine number of fonts.
    List1.AddItem Printer.Fonts(i)   ‘ Put each font into List Box.
Next I

End Sub

You can use this technique to provide a way for the user to select a font. Remember that the Screen object also has FontCount and Fonts properties, which you can use to get a list of the screen fonts. One technique I often use is to obtain lists of both the Screen and Printer object fonts, then compare them and generate a list containing only the fonts common to both. This way the user never runs into the problem of using a font on screen that cannot be printed.

Given that you can use the same methods and properties for displaying graphics and text on the screen and the printer, does this mean that you must write two complete sets of program statements—one directing output to the Printer object and the other to a Form? Not necessarily. Visual Basic procedures have the ability to take an argument of type Object. In other words, you pass an object to the procedure. This enables you to write a procedure that performs the graphics and text output, and then pass it the Form or the Printer object, depending on where you want the output to go. For example, here’s an output procedure that will display a message on either a form or printer:

Table 12.7 Printer object methods.

Method Description
Printer.NewPage Ejects the current page and starts a new one
Printer.EndDoc Aborts printing of a long document; however, the portion already sent to the Windows Print Manager will still be printed
Printer.KillDoc Terminates a print job immediately

Public Sub Output(Dest As Object, Msg as String)

Dest.Print Msg

End Sub

To display the message “Hello” on the form named Form1, use this statement:

Call Output(Form1, “Hello”)

To print the same message on the printer, use this statement:

Call Output(Printer, “Hello”)

This technique is demonstrated in the program PRINT1. The program’s form contains a control array of three Command Buttons. The button with Index 0 has the caption &Display, Index 1 has the caption &Print, and Index 2 has the caption E&xit. When you click on the Display button, the form displays the text and graphics shown in Figure 12.14. If you click on the Print button, the same output is produced on your printer. The program’s code is presented in Listing 12.12.

Listing 12.12 Code in PRINT1.FRM.

Option Explicit

Public Sub Output(Dest As Object)

‘ Set the font name and size.
Dest.FontName = “Times Roman”
Dest.FontSize = 36

‘ Display a message.
Dest.Print “Hello world!”

‘ Print a rectangle to fill the page.
Dest.Line (10, 10)-(Dest.ScaleWidth - 10, Dest.ScaleHeight - 10), , B

‘ Put an X in the rectangle.
Dest.Line (10, 10)-(Dest.ScaleWidth - 10, Dest.ScaleHeight - 10)
Dest.Line (10, Dest.ScaleHeight - 10)-(Dest.ScaleWidth - 10, 10)

End Sub

Private Sub Command1_Click(Index As Integer)

Select Case Index
    Case 0
        Call Output(Form1)
    Case 1
        Call Output(Printer)
    Case 2
        End
End Select

End Sub


Figure 12.14  The form displayed by the project PRINT1.

The PrintForm Method

The PrintForm method—applicable only to Form objects—sends a copy of the Form to the current printer. Everything on the Form—controls, graphics, text, and bitmaps—will be printed (although AutoRedraw must be On for graphics created with Line, Circle, and Pset to print). The border and title bar of the form are included in the printout. For the most part, this method is intended as a programmer’s aid, permitting you to create hard-copy records of your program’s appearance. For end-user printing, you are almost always better off using the Printer object described previously.

The Printers Collection

One of the collections maintained by Windows is the Printers collection, which includes all the printers installed on the system, with each printer represented by a Printer object. In this context, installed does not necessarily mean physically present, but only that the printer driver has been installed. If you display the Windows Printers box (select Settings from the Start menu, then select Printers), you’ll see the printers installed on your system.

Using For Each…Next, you can loop through all the installed printers. For example, this code snippet displays the device names of all installed printers:

Dim p As Printer

For Each p In Printers
    List1.AddItem p.DeviceName
Next

You can use the Printers collection’s Count property to verify that at least one printer is installed:

If Printers.Count < 1 Then
    MsgBox(“No installed printers - you cannot print”)
End If

While the Printers collection lets you access the installed printers, using the Printers part of the Common Dialog control is a lot easier. You display it as follows (assuming CD1 is the name of your Common Dialog control):

Cd1.ShowPrinter

With the dialog box, shown in Figure 12.15, users can select from the available printers, set printing options, and so on. This is a lot easier than trying to do it yourself in code.


Figure 12.15  The Printers Common Dialog box.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.