OpenPrinter

From Xojo Documentation

Revision as of 19:08, 3 September 2020 by THill (talk | contribs) (Text replacement - "{{Deprecated|" to "{{DeprecatedItem|")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Method

Returns a Graphics object in order to print to the currently selected printer.

Usage

result = OpenPrinter([ps As PageSetup])

Part Type Description
result Graphics A graphics object that represents the page to be printed.
PageSetup PrinterSetup Optional. A PrinterSetup object whose properties will be used (like page orientation, scale, etc.) when printing. Although this is optional, you will mostly likely always want to provide a PrinterSetup so that you have access to drawing resolutions.

Notes

The OpenPrinter function returns a Graphics object. The various drawing routines can then be used to draw into the Graphics object and will be sent to the printer for printing.

Although the pageSetup parameter is optional, in order to get the correct horizontal and vertical resolutions for drawing and positioning you will need to provide a PrinterSetup. You can do this without prompting the user with a dialog like this:

Dim p As New PageSetup
g = OpenPrinter(p)
Dim horizontalDPI = p.HorizontalResolution
Dim verticalDPI = p.VerticalResolution

Sample Code

This code prints "Hello World" to the currently selected printer:

Dim p As New PageSetup
Dim g As Graphics
g = OpenPrinter(p)
If g <> Nil Then
// Draw text 1 inch across and 1 inch down
g.DrawString("Hello World", p.HorizontalResolution, p.VerticalResolution)
End If

This code uses the Page Setup properties for the printer chosen by the user by the PageSetup dialog:

Dim ps As New PrinterSetup
If ps.PageSetupDialog Then
Dim g As Graphics
g = OpenPrinter(ps)
If g <> Nil Then
// Draw text 1 inch across and 1 inch down
g.DrawString("Hello World", ps.HorizontalResolution, ps.VerticalResolution)
End If
End If

See Also

Graphics, PrinterSetup classes; OpenPrinterDialog function.