Title | Provide print preview |
Description | This example shows how to provide print preview in Visual Basic 6. It uses a single drawing routine to draw either on a PictureBox or on the Printer object. |
Keywords | print, print preview |
Categories | Graphics, Controls |
|
|
The program uses a single routine, DrawPrintout, to draw on either a PictureBox or on the Printer object. This routine takes as a parameter the object on which it should draw.
|
|
' Send the "printout" to the object. The object is
' either a PictureBox for preview, or the Printer.
Private Sub DrawPrintout(obj As Object)
Const INCH = 1440 ' 1440 twips per inch
Const TXT = "Here is some sample output."
' Make sure the object uses the right font.
obj.Font.Bold = False
obj.Font.Italic = False
obj.Font.Name = "Arial"
obj.Font.StrikeThrough = False
obj.Font.Underline = False
obj.Font.Size = 8
obj.CurrentX = INCH / 2
obj.CurrentY = INCH / 2
obj.Print TXT
obj.Line (INCH / 4, INCH / 4)-Step(2880, 1440), , B
obj.CurrentX = Printer.ScaleLeft + Printer.ScaleWidth / _
2
obj.CurrentY = Printer.ScaleTop
obj.Line -(Printer.ScaleLeft + Printer.ScaleWidth, _
Printer.ScaleTop + Printer.ScaleHeight / 2)
obj.Line -(Printer.ScaleLeft + Printer.ScaleWidth / 2, _
Printer.ScaleTop + Printer.ScaleHeight)
obj.Line -(Printer.ScaleLeft, Printer.ScaleTop + _
Printer.ScaleHeight / 2)
obj.Line -(Printer.ScaleLeft + Printer.ScaleWidth / 2, _
Printer.ScaleTop)
End Sub
|
|
To display a print preview, the program makes the print preview form's PictureBox half the size of the printer (the whole printer won't fit on the screen). It sizes the form to fit and makes the PictureBox use the same coordinates as the Printer. It then calls DrawPrintout, passing it the PictureBox, and displays the preview form.
|
|
' Display a preview.
Private Sub CmdPreview_Click()
' Make the preview PictureBox half the size
' of the Printer.
PreviewForm.PreviewPicture.Width = _
Printer.Width / 2 + _
PreviewForm.PreviewPicture.Width - _
PreviewForm.PreviewPicture.ScaleWidth
PreviewForm.PreviewPicture.Height = _
Printer.Height / 2 + _
PreviewForm.PreviewPicture.Height - _
PreviewForm.PreviewPicture.ScaleHeight
' Size the preview form to fit.
PreviewForm.Height = _
PreviewForm.PreviewPicture.Height + _
PreviewForm.Height - PreviewForm.ScaleHeight
PreviewForm.Width = _
PreviewForm.PreviewPicture.Width + _
PreviewForm.Width - PreviewForm.ScaleWidth
' Make the preview PictureBox use the same
' scaling as the Printer.
PreviewForm.PreviewPicture.ScaleLeft = Printer.ScaleLeft
PreviewForm.PreviewPicture.ScaleTop = Printer.ScaleTop
PreviewForm.PreviewPicture.ScaleWidth = _
Printer.ScaleWidth
PreviewForm.PreviewPicture.ScaleHeight = _
Printer.ScaleHeight
' Clear the preview PictureBox.
PreviewForm.PreviewPicture.Cls
' Draw the "printout" on a PictureBox.
DrawPrintout PreviewForm.PreviewPicture
' Display the PictureBox.
PreviewForm.Show vbModal
End Sub
|
|
To make a printout, the program simply calls DrawPrintout, passing it the Printer object.
|
|
' Create a printout.
Private Sub CmdPrint_Click()
' Draw the "printout" on the Printer object.
DrawPrintout Printer
' Finish the printout.
Printer.EndDoc
End Sub
|
|
To keep things simple, this example is not very fancy. My book Advanced Visual Basic Techniques shows how to make a much fancier preview including preview at multiple scales and scrolling through the preview image.
|
|
|
|