Print to Printer in Xojo on Windows

As we can enumerate printers, we can show them in a popupmenu. User can select one and we modify the printer setup string for a Xojo printerSetup object and than use normal Xojo printing functions to print:
// now we have nice setupstring
dim ss as string = ps.SetupString
'MsgBox str(len(ss))
// parse it in device mode, to retain settings there
dim d as WindowsDeviceModeMBS = WindowsDeviceModeMBS.FromSetupString(ss)
if d = nil then
// or start with a blank one
d = new WindowsDeviceModeMBS
end if
// change printer
if PopupPrinter.ListIndex >= 0 then
dim w as WindowsPrinterInfoMBS = PopupPrinter.RowTag(PopupPrinter.ListIndex)
// set device name
d.DeviceName = w.PrinterName
else
MsgBox "No printer selected."
Return
end if
// enable duplex
'd.Fields = BitwiseOr(d.Fields, d.DM_DUPLEX)
'd.Duplex = d.DMDUP_HORIZONTAL
// get back as setup string
dim da as string = d.SetupString
if da = "" then
MsgBox "failed to create setup string"
Return
end if
// assign back
ps.SetupString = da
// and print something
dim g as Graphics = OpenPrinter(ps)
g.DrawString "Page on "+d.DeviceName, 50, 50
PS: Works in Real Studio, too.
