OpenPrinterDialog

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

Displays the standard Print dialog box and returns a Graphics object in order to print to the currently selected printer.

Syntax

result=OpenPrinterDialog([pageSetup][,window])

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.
window Window Optional. If you want to present the Print dialog as a Sheet window (macOS only), window is the window to which you want to attach the Print dialog box.

Notes

The OpenPrinterDialog 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. You can get the values in the Copies, From and To fields of the Print dialog box using the Copies, FirstPage and LastPage properties of the Graphics object returned by the OpenPrinterDialog function.

If the user clicks the Cancel button, the OpenPrinterDialog function returns Nil.

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 = OpenPrinterDialog(p, MyWindow)
Dim horizontalDPI = p.HorizontalResolution
Dim verticalDPI = p.VerticalResolution

Examples

This example prints "Hello World" to the currently selected printer.

Dim p As New PrinterSetup
Dim g As Graphics
g = OpenPrinterDialog(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 example 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 = OpenPrinterDialog(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

This example checks if the Windows printer supports alpha blending:

Declare Function GetDeviceCaps Lib "gdi32" ( hdc As Integer, index As Int32 ) As Int32
Const SHADEBLENDCAPS = 120
Const SB_NONE = 0
Const SB_CONST_ALPHA = 1
Const SB_PIXEL_ALPHA = 2
Const SB_PREMULT_ALPHA = 4
Const SB_GRAD_RECT = &h10
Const SB_GRAD_TRI = &h20

Dim g As Graphics = OpenPrinterDialog(Nil, Nil)
Dim hdc As Integer = g.Handle(Graphics.HandleTypeHDC)

If GetDeviceCaps(hdc, SHADEBLENDCAPS) = SB_NONE Then
MsgBox("This Printer does not support alpha blending")
Else
MsgBox("Yay! This Printer supports alpha blending")
End If

See Also

Graphics, PrinterSetup, StyledTextPrinter classes; OpenPrinter function.