home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / print / prtprev / readme.txt < prev   
Text File  |  1994-04-13  |  4KB  |  110 lines

  1. PrtPrev: How to Add Print Preview to Visual Basic Applications
  2.  
  3. This sample GENPRINT.MAK project contains the files GENPRINT.FRM and
  4. GENPRINT.BAS.
  5.  
  6. This sample describes how to create printing routines that can
  7. print to the printer or to a picture box. This enables you to add
  8. print preview capabilities to your Visual Basic applications.
  9.  
  10. Generic Printing
  11. ----------------
  12.  
  13. It would be ideal to have a generic print routine that could print
  14. to the printer or to the screen depending on what you pass it. The
  15. Visual Basic printer object and picture box control have many of the
  16. same methods and properties. For example, both of these are valid:
  17.  
  18.    Printer.Print AString
  19.    Picture1.Print AString
  20.  
  21. It would be nice if you could pass a generic object to a subroutine
  22. and the subroutine would use the Print method off of the generic object
  23. as in this example:
  24.  
  25.    Call PrintJob(Printer)
  26.    Call PrintJob(Picture1)
  27.  
  28.    Sub PrintJob(GenericObject As Object)
  29.       GenericObject.Print AString
  30.    End Sub
  31.  
  32. Unfortunately, this is not possible. The Visual Basic Printer object
  33. is a system object, so it can't be passed as a parameter.
  34.  
  35. This leaves you with two choices in Visual Basic. You could create two
  36. routines -- one for printing to the printer and one for print preview.
  37. However, the code would not be reusable in your future projects. The
  38. second approach is to write your own set of routines that can print to
  39. the printer or a picture box based on the value of a flag. This is the
  40. method used in the example code given below. Once you create the
  41. routines, you can re-use them in future programs.
  42.  
  43. The example creates routines that closely mimic Visual Basic's built in
  44. methods and properties. However, you could use this approach to create
  45. high-level routines that greatly simplify your printing needs.
  46.  
  47. The routines work by checking the variable PrinterFlag. PrinterFlag is
  48. True when printing is going to the printer and False when printing to
  49. the picture box.
  50.  
  51. Here's the print routine from the example. Notice how it is just a
  52. shell function that determines what to print to and then does it.
  53.  
  54.    Sub PrintPrint (PrintVar)
  55.       If PrinterFlag Then
  56.          Printer.Print PrintVar
  57.       Else
  58.          objPrint.Print PrintVar
  59.       End If
  60.    End Sub
  61.  
  62. With just a few simple routines like this, you can start to do
  63. generic printing.
  64.  
  65. Scaling
  66. -------
  67.  
  68. To accomplish print preview, the program must scale the output to the
  69. picture box to match the output on the printer.
  70.  
  71. In the example, the PrintStartDoc routine initializes the printer or
  72. picture box and sets up the scaling. The width and height of the paper
  73. are passed to the PrintStartDoc routine. These dimensions are used to
  74. determine the non-printable area of the printer object, find the ratio
  75. of the picture box to the printer, re-size the picture box, and scale
  76. the picture box. The picture box is scaled with the Scale method. After
  77. setting the scale of the picture box, graphic methods use the new
  78. coordinates. For an 8.5 x 11 inch piece of paper the picture box is
  79. scaled with this command:
  80.  
  81.    Picture1.Scale (0, 0)-(8.5, 11)
  82.  
  83. The Scale method does not scale fonts. To scale the fonts, use the
  84. ratio of the picture box height divided by the printer's height in
  85. inches. Then multiply by this ratio to determine the correct font
  86. size within the picture box. Here is the PrintFontSize routine that
  87. sets the appropriate font sizes in the example:
  88.  
  89.    Sub PrintFontSize (pSize)
  90.       If PrinterFlag Then
  91.          Printer.FontSize = pSize
  92.       Else
  93.          'Sized by ratio since Scale method does not effect FontSize
  94.          ObjPrint.FontSize = pSize * Ratio
  95.       End If
  96.    End Sub
  97.  
  98. The ratio used to calculate the font size can be applied to anything
  99. you need to scale in the picture box that is not automatically scaled
  100. by the Scale method. The ratio is also used in the PrintPicture routine
  101. to scale pictures.
  102.  
  103. To Run the Sample:
  104. ------------------
  105.  
  106. Click the command button with the check box checked to preview the
  107. page. Click the command button with the check box cleared to print
  108. the page.
  109.  
  110.