![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Next, the program should present the dialog to the user by invoking the dialogs ShowPrinter method. When the dialog is finished, its properties indicate the choices the user made. Some of these properties are straightforward; others interact strangely with the Printer object. For example, if the printer and its device driver support hardware printing of multiple copies, the dialog will set Printer.Copies to indicate the number of copies the user entered. It will then set the dialogs Copies property to one. This allows the printer to generate the copies rather than making your application send the printout to the printer multiple times. Printing copies in hardware is generally much faster than printing them in software.
If the printer does not support multiple copies, the dialogs Copies property will indicate the number entered by the user. The application must then generate the printout several times. When the dialog is finished, some of the Printer objects properties may have been modified. For example, if the printer produced multiple copies in hardware, Printer.Copies will no longer be one. If the application does not reset this value before the next time it produces a printout, the program will again generate multiple copies. The ExpenseReporter application prints only a single page, and it does not provide a print dialog. A Print Dialog command is included in the File menu, but its Visible property is set to false so it is hidden. It can be enabled by checking its Visible property using the Visual Basic Menu Editor. ExpenseReporters Print Dialog command uses the following code to display the print dialog. Notice how the routine is prepared to produce multiple copies of the expense report in case the printer cannot produce multiple copies itself. It does this by creating several document pages all in the same print document rather than by creating separate documents.
Private Sub mnuFilePrintDialog_Click()
Dim num_copies As Integer
Dim i As Integer
Prepare the common dialog.
FileDialog.Flags = _
cdlPDHidePrintToFile + _
cdlPDNoPageNums + _
cdlPDNoSelection + _
cdlPDAllPages
FileDialog.CancelError = True
Present the dialog.
On Error Resume Next
FileDialog.ShowPrinter
If the user canceled, were done.
If Err.Number = cdlCancel Then Exit Sub
If Err.Number <> 0 Then
Beep
MsgBox Error printing file. & vbCrLf & _
Err.Description, _
vbOKOnly + vbExclamation, _
Printing Error
Exit Sub
End If
Start waiting.
WaitStart
Print to the printer.
num_copies = FileDialog.Copies
For i = 1 To num_copies
PrintDocument Printer
End this page.
Printer.NewPage
Next i
Close the document.
Printer.EndDoc
Reset the printers copy count.
Printer.Copies = 1
End waiting.
WaitEnd
End Sub
Providing Printer SetupAny application that uses the printer should allow the user to modify the printer setup. A standard printer dialog allows the user to select the printer, paper size, and orientation. It also provides a command button that presents another dialog that gives access to other printer properties. These properties are different for different kinds of printers and include such things as halftoning options, negative or mirror-image printing, scaling, and PostScript optimization. Figure 2.6 shows ExpenseReporters print setup dialog. Presenting the printer setup dialog is simple using the common dialog control. First, the program should set the dialogs Flags property to cdlPDPrintSetup. This tells Visual Basic that the dialog should be a printer setup dialog rather than a file-printing dialog similar to the one described in the previous section. Next, the program should invoke the dialogs ShowPrinter method. The following code shows how ExpenseReporter presents the print setup dialog
Private Sub mnuFilePrintSetup_Click()
Prepare the common dialog.
FileDialog.Flags = cdlPDPrintSetup
FileDialog.CancelError = False
Present the dialog.
FileDialog.ShowPrinter
End Sub
After the user has finished with the print setup dialog, any new settings are incorporated into the Printer object. (Note: This may not work with networked printers.) For example, if the user selects normal 8.5 by 11 paper printed in landscape (sideways) orientation, the system will redimension the Printer object so its width is 11 and its height is 8.5. The application can immediately take advantage of the new settings when it sends output to the printer or when it presents a print preview. ExpenseReporter uses the Printer objects Width and Height properties when it displays a print preview. If a user selects landscape orientation using the Print Setup command and then invokes the Print Preview command from the File menu, ExpenseReporter uses the Printer objects dimensions. If the user selects the previews small scale, it will be apparent that the preview shows the paper turned sideways. The expense report will still be drawn with its top at the top of the display, but the paper will be wider than it is tall. If the user then selects the Print command from the File menu, the expense report will be printed sideways. If the user uses the print setup dialog to change the default printer, printouts will automatically be sent to the new printer. If the user selects a special device as the printer, such as a fax modem, printed output will automatically be sent to that device. By using a common dialog, an application gets all of these features with almost no extra programming. The only way in which the application needs to be aware of the printers setup is through the Printer objects properties. For example, ExpenseReporter sets the width of the notes field in the printout based on the Printer objects Width property. It makes the width of this field equal to Printer.Width minus space for the margins and the reconciliation fields at the right edge of the page. This very simple use of the Printer objects properties barely hints at the things an application can do to take advantage of the full printing area. Providing HelpLike life insurance, a help system is something everyone hopes is never needed. If an application is well designed and the user interface is intuitive, users should be able to use it without accessing the help system. Unfortunately, help systems are also like life insurance in another way: By the time the user realizes it is needed, it is much too late. Writing a high-quality help system is a huge chore. In some cases, writing a good help system is more difficult than writing the application itself. There is no room in this chapter to explain every facet of building the perfect help system, but there is room to cover the basics. Before you read this section, however, be aware that you will need a copy of the help compiler to create help files. The help compiler is included with the Visual Basic Professional Edition. If you do not have access to the help compiler, you will not be able to build new help files (you may still find the information presented here interesting). Chapter 3 explains how you can create a Web-based help system that does not require the help compiler.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|