显示打印机特定的配置对话框

时间:2009-06-02 13:21:19

标签: .net wpf printing

如何PrintTicket显示特定于打印机的配置对话框?

注意:我不是指PrintDialog命名空间中的System.Windows.Controls

2 个答案:

答案 0 :(得分:3)

由于我不值得信任以编辑接受的答案,我会发布第二个答案......

接受的答案适用于显示本机打印机对话框并从该对话框获取更改。但是,它不会事先正确设置对话框中的属性。

要将设置推送到本机对话框,您必须更改DocumentProperties的签名,如下所示。新签名不使用ref参数作为输入。

Here is the page指出了这个小而重要的差异。

[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,
  IntPtr pDevModeInput,    //removed ref
  int fMode);

答案 1 :(得分:2)

要显示PrinterSettings对话框,请使用

[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);