PrinterUtils.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Drawing.Printing;
  6. namespace FastReport.Utils
  7. {
  8. internal static class PrinterUtils
  9. {
  10. [DllImport("winspool.drv", EntryPoint = "DocumentPropertiesW")]
  11. private static extern int DocumentProperties(IntPtr hWnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevMode, IntPtr devModeIn, int fMode);
  12. [DllImport("winspool.drv")]
  13. private static extern int OpenPrinter(string pPrinterName, out IntPtr hPrinter, IntPtr pDefault);
  14. [DllImport("winspool.drv")]
  15. private static extern int ClosePrinter(IntPtr phPrinter);
  16. [DllImport("kernel32.dll")]
  17. private static extern IntPtr GlobalLock(IntPtr hMem);
  18. [DllImport("kernel32.dll")]
  19. private static extern int GlobalUnlock(IntPtr hMem);
  20. [DllImport("kernel32.dll")]
  21. private static extern int GlobalFree(IntPtr hMem);
  22. private const int DM_PROMPT = 4;
  23. private const int DM_OUT_BUFFER = 2;
  24. private const int DM_IN_BUFFER = 8;
  25. public static void ShowPropertiesDialog(PrinterSettings printerSettings)
  26. {
  27. #if !AVALONIA
  28. try
  29. {
  30. IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
  31. IntPtr handle;
  32. OpenPrinter(printerSettings.PrinterName, out handle, IntPtr.Zero);
  33. IntPtr pDevMode = GlobalLock(hDevMode);
  34. int result = DocumentProperties(IntPtr.Zero, handle, printerSettings.PrinterName, pDevMode, pDevMode, DM_IN_BUFFER | DM_PROMPT | DM_OUT_BUFFER);
  35. GlobalUnlock(hDevMode);
  36. if (result == 1)
  37. {
  38. printerSettings.SetHdevmode(hDevMode);
  39. printerSettings.DefaultPageSettings.SetHdevmode(hDevMode);
  40. }
  41. ClosePrinter(handle);
  42. }
  43. catch
  44. {
  45. // wrong printer
  46. }
  47. #endif
  48. }
  49. }
  50. }