tags:

views:

134

answers:

1

Having a PrintTicket how to display the printer-specific configuration dialog?

Note: I don't mean the PrintDialog from the System.Windows.Controls namespace.

A: 

To show the PrinterSettings dialog use

[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true,
  CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(
  IntPtr hwnd,
  IntPtr hPrinter,
  [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName,
  IntPtr pDevModeOutput,
  ref IntPtr pDevModeInput,
  int fMode);

[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern bool GlobalFree(IntPtr hMem);

private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings) {
  var handle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
  var hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
  var pDevMode = GlobalLock(hDevMode);
  var sizeNeeded = DocumentProperties(handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
  var devModeData = Marshal.AllocHGlobal(sizeNeeded);
  DocumentProperties(handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, ref pDevMode, 14);
  GlobalUnlock(hDevMode);
  printerSettings.SetHdevmode(devModeData);
  printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
  GlobalFree(hDevMode);
  Marshal.FreeHGlobal(devModeData);
}

// Show this dialog.
var printQueue = LocalPrintServer.GetDefaultPrintQueue();
var settings = new PrinterSettings { PrinterName = printQueue.FullName };
OpenPrinterPropertiesDialog(settings);
Shurup