Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleProvide print preview with different scales
DescriptionThis example shows how to provide print preview with different scales in Visual Basic 6. It uses a single drawing routine to draw either on a PictureBox or on the Printer object.
Keywordsprint, print preview, scale
CategoriesGraphics, 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. In this example, the routine draws some text, a rectangle, and a bitmap.
 
' Draw the printout on prn, either a PictureBox
' or the Printer.
Private Sub DrawPrintout(prn As Object)
    ' Make the object use 10 point Courier New.
    ' It is likely to be available on both the
    ' printer and in PictureBoxes.
    prn.Font.Name = "Courier New"
    prn.Font.Size = 10
    prn.DrawWidth = 4

    ' Draw a 1" by 3" square in the upper left.
    prn.Line (0, 0)-(3, 1), , B

    ' Center the text within the square.
    prn.CurrentX = (3 - prn.TextWidth(Text1.Text)) / 2
    prn.CurrentY = (1 - prn.TextHeight(Text1.Text)) / 2
    prn.Print Text1.Text

    ' Copy the smiley face bitmap onto prn.
    prn.PaintPicture SmileyPict.Picture, 0, 1.25
End Sub
 
To display a print preview, the program passes the DrawPrintout subroutine a hidden PictureBox. If then displays the preview form.
 
' Display a print preview.
Private Sub mnuFilePrintPreview_Click()
Dim wid As Single
Dim hgt As Single
    
    ' Make PreviewForm.HiddenPict behave as much
    ' like the Printer as possible.

    ' Make it 8.5" by 11" in real size.
    wid = PreviewForm.ScaleX(8.5, vbInches, _
        PreviewForm.ScaleMode)
    hgt = PreviewForm.ScaleY(11, vbInches, _
        PreviewForm.ScaleMode)
    PreviewForm.HiddenPict.Width = wid
    PreviewForm.HiddenPict.Height = hgt

    ' Make it use the same scale as the Printer.
    PreviewForm.HiddenPict.Scale (-1, -1.5)-(7.5, 9.5)

    ' Draw the preview onto the hidden PictureBox.
    DrawPrintout PreviewForm.HiddenPict

    ' Display the preview form.
    PreviewForm.ShowPreview
End Sub
 
The preview form displays the preview image at different scales by sizing the preview PictureBox appropriately and then using PaintPicture to copy the full-scale hidden picture onto this PictureBox.
 
' View at scale 1 / Index.
Private Sub mnuSetScale_Click(Index As Integer)
Dim i As Integer
Dim wid As Single
Dim hgt As Single

    ' Check the selected menu scale item.
    mnuSetScale(1).Checked = False
    mnuSetScale(2).Checked = False
    mnuSetScale(4).Checked = False
    mnuSetScale(Index).Checked = True
   
    ' Make PreviewPict the correct size.
    wid = (PreviewPict.Width - _
        PreviewPict.ScaleWidth) + _
        HiddenPict.Width / Index
    hgt = (PreviewPict.Height - _
        PreviewPict.ScaleHeight) + _
        HiddenPict.Height / Index
    PreviewPict.Move 0, 0, wid, hgt

    ' Copy the hidden picture into PreviewPict.
    PreviewPict.PaintPicture HiddenPict.Image, _
        0, 0, _
        PreviewPict.ScaleWidth, _
        PreviewPict.ScaleHeight
End Sub
 
To create a printout, the program calls subroutine DrawPrintout, passing it the Printer object.
 
' Print the document.
Private Sub mnuFilePrint_Click()
    ' Draw the printout onto the Printer object.
    DrawPrintout Printer

    ' Send the printout to the printer.
    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.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated