![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
ExpenseReporter displays print previews in a separate form that is quite simple. It contains its own menus appropriate for a print preview. It holds two picture boxes and two scroll bars that implement a scrolling area much like the one on the main ExpenseReporter form. Finally, the preview screen contains a hidden picture box to hold the initial preview image. This picture box will be passed into the PrintDocument routine so PrintDocument will build its image of the printout there.
When the print preview screen is created, its Load event handler calls subroutine MakePreview. MakePreview resizes and scales the hidden picture box so that its dimensions match those of the Printer object. It then uses the PrintDocument subroutine to draw an image of the printout in the hidden picture box. Once this picture has been created, it does not need to be created again while the preview screen is loaded. Commands in the Scale menu display the image at different scales on a visible picture box, but the hidden picture remains unchanged.
Sub MakePreview()
Start from scratch.
HiddenPicture.Cls
Give the hidden picture the right size for
the printer.
HiddenPicture.Width = Printer.Width
HiddenPicture.Height = Printer.Height
Set the hidden pictures scale factors.
HiddenPicture.ScaleLeft = Printer.ScaleLeft
HiddenPicture.ScaleTop = Printer.ScaleTop
HiddenPicture.ScaleWidth = Printer.ScaleWidth
HiddenPicture.ScaleHeight = Printer.ScaleHeight
Print to the hidden picture.
ExpenseForm.PrintDocument HiddenPicture
End Sub
The screen displays print previews at several different scales. At the largest scale the user can read the text that will be printed. The smallest scale gives an overview showing how the printed material fits on the page. To display the preview at different scales, the SetScale subroutine sets the ScrollArea picture box to an appropriate size. The ScaleValue array stores the scaling factors used by the program. ExpenseReporter displays previews at three scales with scaling factors 1.0, 0.75, and 0.375. For example, at medium scale the picture is displayed 0.75 times its logical printed size. Note that a logical inch on the printer is not necessarily the same size as a logical inch on the computers screen. That means the preview image may not be exactly 75 percent as large as the printed output. Resize the scroll area. wid = Printer.Width * ScaleValue(PreviewScale) hgt = Printer.Height * ScaleValue(PreviewScale) ScrollArea.Move 0, 0, wid, hgt When the application is ready to display the preview, the DrawPreview subroutine copies the hidden picture into the ScrollArea picture box using the PaintPicture method. DrawPreview uses PaintPicture to draw the entire hidden preview image so that it fills the ScrollArea picture box. Because ScrollArea has been resized for the appropriate scale, the image will be shrunk to fit. ExpenseReporter does not provide any magnified previews with scales greater than 1.0. If it did, PaintPicture would stretch the preview image to fit the enlarged ScrollArea.
Sub DrawPreview()
Copy the hidden picture to the scroll area.
ScrollArea.Cls
ScrollArea.PaintPicture HiddenPicture.Image, _
0, 0, _
ScrollArea.Width, _
ScrollArea.Height, _
0, 0, _
HiddenPicture.Width, _
HiddenPicture.Height, _
vbSrcCopy
End Sub
An alternative strategy for displaying preview images at different scales would be to draw the image directly on the ScrollArea picture box. You could use ScrollAreas scaling properties to make the image appear at the correct size. For two reasons this method does not work well for ExpenseReporter. First, this method takes longer. Whenever the user changed the preview scale, the program would need to redraw the picture at the new scale. The current implementation requires ExpenseReporter to draw the image only once. When the scale changes, the program can quickly copy the image using PaintPicture. If the image is very complicated, redrawing it every time will take much longer. Second, the computer is not able to produce every font at every scale. To display 8 point Arial with a scale factor of 0.375, the computer would need to produce Arial at 3 point. Most computers cannot produce this font. Faced with this dilemma the computer would select the font that it thought was closest to the required font. That might be 4 point Arial, 4 point Times Roman, or even 8 point Arial, depending on the fonts available. A drawing with lines and boxes scaled by a factor of 0.375 but text at 8 point instead of 3 point looks terrible. On the other hand, if the program does not produce text, redrawing the image at each scale will produce a better result. The PaintPicture method tends to lose detail when it shrinks or stretches an image. At small scales the image can be very hard to understand. If the program redraws the image at the smaller scale, it will be able to take better advantage of the computers full resolution. Managing Two Scroll BarsThe last new technique used by the print preview screen involves the two scroll bars. The way the two scroll bars work and the way the program sets their Min, Max, SmallChange, and LargeChange properties is very similar to the way these tasks are handled for ExpenseReporters main form. Because there are two scroll bars instead of one, however, there is a difference in the way the program decides when the scroll bars are necessary. Suppose the vertical scroll bar is needed because the preview image is too tall to fit on the form. In that case, the vertical scroll bar takes up some space on the right edge of the form; that, in turn, makes the area available for the preview image narrower. The preview area may not be as wide as the form, but it may be wider than the narrowed area now available. In that case, the horizontal scroll bar is also needed. That, in turn, reduces the amount of vertical space available. The following code fragment shows how the ArrangeControls subroutine determines which scroll bars are needed.
See if we need the horizontal scroll bar.
need_hscroll = (need_wid > got_wid)
If (need_hscroll) Then _
got_hgt = got_hgt - HScroll.Height
See if we need the vertical scroll bar.
need_vscroll = (need_hgt > got_hgt)
If (need_vscroll) Then
got_wid = got_wid - VScroll.Width
If we do not already need it, see if we
need the horizontal scroll bar now that
the available area is narrower.
If Not need_hscroll Then
need_hscroll = (need_wid > got_wid)
If (need_hscroll) Then _
got_hgt = got_hgt - HScroll.Height
End If
End If
Printing with a DialogWhen an ExpenseReporter user selects the Print command from the File menu or presses Ctrl-P, the program prints a single page on the currently selected printer. In some applications it is appropriate to give the user more printing options. For example, an application could allow the user to select the range of pages to print or indicate the number of copies to print. Figure 2.5 shows a print dialog that allows the user to make these choices. In Visual Basic, an application can use a common dialog control to allow the user to make these choices. The program should first set the dialogs Flags property to indicate any special features the dialog should have. For instance, if the Flags are set to cdlPDHidePrintToFile, the dialog will not display the Print to file check box.
|
|
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.
|