【发布时间】:2009-06-02 13:21:19
【问题描述】:
有PrintTicket如何显示打印机特定的配置对话框?
注意:我不是指来自System.Windows.Controls 命名空间的PrintDialog。
【问题讨论】:
有PrintTicket如何显示打印机特定的配置对话框?
注意:我不是指来自System.Windows.Controls 命名空间的PrintDialog。
【问题讨论】:
由于我不够可信,无法编辑已接受的答案,因此我将发布第二个答案...
接受的答案适用于显示本机打印机对话框并从该对话框中获取更改。但是,它没有事先正确设置对话框上的属性。
为了将设置推送到本机对话框,您必须更改 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);
【讨论】:
要显示打印机设置对话框,请使用
[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);
【讨论】: