![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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 forms Click event procedure, will load a List Box control with the names of all the Printer objects 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 statementsone 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, heres an output procedure that will display a message on either a form or printer:
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 programs 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 programs 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
The PrintForm MethodThe PrintForm methodapplicable only to Form objectssends a copy of the Form to the current printer. Everything on the Formcontrols, graphics, text, and bitmapswill 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 programmers aid, permitting you to create hard-copy records of your programs appearance. For end-user printing, you are almost always better off using the Printer object described previously. The Printers CollectionOne 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), youll 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 collections 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.
|
![]() |
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. |