Building a Chart > Printing Charts > Outputting Charts to Device Contexts |
Use the DrawToDC for more complicated chart output, such as printing multiple charts on a single page, or for printing directly to a printer without using the Windows Print dialog. For example, to print several charts on one page, call DrawToDC for each chart, and specify the top, left, width and height arguments to specify each chart's different size and position. The following example shows how to use DrawToDC for a printing function that prints a chart that fills the page to the default printer without using the Windows Print dialog box:
' Prints a title
Printer.Print "Simple Chart Example"
' Prints the chart directly to the printer as an enhanced
' metafile with top and left margins of 0 pixels, and a
' height and width to fit the page
Chart2D1.DrawToDC Printer.hDC, oc2dFormatEnhMetafile, oc2dScaleToMax, 0, 0,_
Printer.ScaleWidth/Printer.TwipsPerPixelX,_ Printer.ScaleHeight/Printer.TwipsPerPixelY
' Ends the print job
Printer.EndDoc
The last four parameters specified in the PrintChart call indicate the location and size (in pixels) of the printer page into which the chart image is printed. The pixels for a printer are usually much smaller than those of the screen, as printing devices have a much higher resolution.
If the second parameter is oc2dScaleNone, the printer will use the same number of pixels that are used by the chart on the screen. A typical resolution for the screen is 96 pixels per inch while a laser printer is 600 pixels per inch, resulting in a small image. If the image is larger that the rectangle specified, the image will be clipped.
If the second parameter is oc2dScaleToMax, the image is scaled to fill the entire rectangle specified. To print a chart 1 cm from the left top margin, 15 cm wide, and 10 cm high, the following code would be used:
Private Sub cmdPrintChart_Click()
Dim left As Integer, top As Integer, width As Integer, height As Integer
Dim xfactor As Double, yfactor As Double
xfactor = 1440 / (2.54 * Printer.TwipsPerPixelX)
yfactor = 1440 / (2.54 * Printer.TwipsPerPixelY)
left = 1 * xfactor
top = 1 * yfactor
width = 15 * xfactor
height = 10 * xfactor
Me.lblPrintChart.Caption = Me.Chart2D.PrintChart (oc2dFormatBitmap,_
oc2dScaleToMax, left, top, width, height)
End Sub