日期:2014-05-18  浏览次数:20943 次

如何打开打印机属性对话框
采用下面的代码可以打开打印机属性对话框,但是用户在该对话框中进行了设定后,新的设定值并没有保存到系统中。如何才能保存用户的设定阿?
  string printerName = _document.PrinterSettings.PrinterName;

  if (printerName != null && printerName.Length > 0)
  {
  IntPtr pPrinter = IntPtr.Zero;
  IntPtr pDevModeOutput = IntPtr.Zero;
  IntPtr pDevModeInput = IntPtr.Zero;
  IntPtr nullPointer = IntPtr.Zero;

  OpenPrinter(printerName, ref pPrinter, ref nullPointer);

  int iNeeded = DocumentProperties(this.Handle, pPrinter, printerName, ref pDevModeOutput, ref pDevModeInput, 0);
  pDevModeOutput = System.Runtime.InteropServices.Marshal.AllocHGlobal(iNeeded);
  DocumentProperties(this.Handle, pPrinter, printerName, ref pDevModeOutput, ref pDevModeInput, 4);
   
  ClosePrinter(pPrinter);
  }

------解决方案--------------------
C# code

var query = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
var printers = query.Get();
foreach (var each in printers) { Console.WriteLine(each["name"]); }

if (xpsprinter != null) {
    xpsprinter.InvokeMethod("SetDefaultPrinter", new object[] { xpsprinter["name"].ToString() });
}