private PaperSize GetPaperSize(IntPtr modeHandle) { if (_paperSize == null) { bool ownHandle = false; if (modeHandle == IntPtr.Zero) { modeHandle = printerSettings.GetHdevmode(); ownHandle = true; } IntPtr modePointer = Interop.Kernel32.GlobalLock(modeHandle); SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)Marshal.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE)); PaperSize result = PaperSizeFromMode(mode); Interop.Kernel32.GlobalUnlock(modeHandle); if (ownHandle) { Interop.Kernel32.GlobalFree(modeHandle); } return(result); } else { return(_paperSize); } }
private PaperSize GetPaperSize(IntPtr modeHandle) { if (paperSize == null) { bool ownHandle = false; if (modeHandle == IntPtr.Zero) { modeHandle = printerSettings.GetHdevmode(); ownHandle = true; } IntPtr modePointer = SafeNativeMethods.GlobalLock(new HandleRef(null, modeHandle)); SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)UnsafeNativeMethods.PtrToStructure(modePointer, typeof(SafeNativeMethods.DEVMODE)); PaperSize result = PaperSizeFromMode(mode); SafeNativeMethods.GlobalUnlock(new HandleRef(null, modeHandle)); if (ownHandle) { SafeNativeMethods.GlobalFree(new HandleRef(null, modeHandle)); } return(result); } else { return(paperSize); } }
protected override bool RunDialog(IntPtr hwndOwner) { System.Windows.Forms.NativeMethods.PAGESETUPDLG pagesetupdlg; bool flag2; System.Windows.Forms.IntSecurity.SafePrinting.Demand(); System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc); if (this.pageSettings == null) { throw new ArgumentException(System.Windows.Forms.SR.GetString("PSDcantShowWithoutPage")); } pagesetupdlg = new System.Windows.Forms.NativeMethods.PAGESETUPDLG { lStructSize = Marshal.SizeOf(pagesetupdlg), Flags = this.GetFlags(), hwndOwner = hwndOwner, lpfnPageSetupHook = proc }; PrinterUnit thousandthsOfAnInch = PrinterUnit.ThousandthsOfAnInch; if (this.EnableMetric) { StringBuilder lpLCData = new StringBuilder(2); if ((UnsafeNativeMethods.GetLocaleInfo(System.Windows.Forms.NativeMethods.LOCALE_USER_DEFAULT, 13, lpLCData, lpLCData.Capacity) > 0) && (int.Parse(lpLCData.ToString(), CultureInfo.InvariantCulture) == 0)) { thousandthsOfAnInch = PrinterUnit.HundredthsOfAMillimeter; } } if (this.MinMargins != null) { Margins margins = PrinterUnitConvert.Convert(this.MinMargins, PrinterUnit.Display, thousandthsOfAnInch); pagesetupdlg.minMarginLeft = margins.Left; pagesetupdlg.minMarginTop = margins.Top; pagesetupdlg.minMarginRight = margins.Right; pagesetupdlg.minMarginBottom = margins.Bottom; } if (this.pageSettings.Margins != null) { Margins margins2 = PrinterUnitConvert.Convert(this.pageSettings.Margins, PrinterUnit.Display, thousandthsOfAnInch); pagesetupdlg.marginLeft = margins2.Left; pagesetupdlg.marginTop = margins2.Top; pagesetupdlg.marginRight = margins2.Right; pagesetupdlg.marginBottom = margins2.Bottom; } pagesetupdlg.marginLeft = Math.Max(pagesetupdlg.marginLeft, pagesetupdlg.minMarginLeft); pagesetupdlg.marginTop = Math.Max(pagesetupdlg.marginTop, pagesetupdlg.minMarginTop); pagesetupdlg.marginRight = Math.Max(pagesetupdlg.marginRight, pagesetupdlg.minMarginRight); pagesetupdlg.marginBottom = Math.Max(pagesetupdlg.marginBottom, pagesetupdlg.minMarginBottom); System.Drawing.Printing.PrinterSettings settings = (this.printerSettings == null) ? this.pageSettings.PrinterSettings : this.printerSettings; System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert(); try { pagesetupdlg.hDevMode = settings.GetHdevmode(this.pageSettings); pagesetupdlg.hDevNames = settings.GetHdevnames(); } finally { CodeAccessPermission.RevertAssert(); } try { if (!UnsafeNativeMethods.PageSetupDlg(pagesetupdlg)) { return(false); } UpdateSettings(pagesetupdlg, this.pageSettings, this.printerSettings); flag2 = true; } finally { UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevMode)); UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevNames)); } return(flag2); }
public static void OpenPrinterPropertiesDialog(PrinterSettings printerSettings, IntPtr handle) { IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings); IntPtr pDevMode = GlobalLock(hDevMode); int sizeNeeded = DocumentProperties(handle, IntPtr.Zero, printerSettings.PrinterName, IntPtr.Zero, pDevMode, 0); if (sizeNeeded < 0) { GlobalUnlock(hDevMode); return; } IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded); DocumentProperties(handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER | DM_PROMPT); GlobalUnlock(hDevMode); printerSettings.SetHdevmode(devModeData); printerSettings.DefaultPageSettings.SetHdevmode(devModeData); GlobalFree(hDevMode); Marshal.FreeHGlobal(devModeData); }
//Grabs the devmode data in memory and stores in arraylist /// <summary> /// Сохранение в файл или в массив байт (mode = 1 - в файл/ mode=2 - в массив) /// </summary> /// <param name="printerSettings"></param> /// <param name="mode"></param> /// <param name="Filename"></param> public byte[] SaveDevmode(PrinterSettings printerSettings, int mode, String Filename) { ///int mode ///1 = Save devmode structure to file ///2 = Save devmode structure to Byte array and arraylist IntPtr hDevMode = IntPtr.Zero; // handle to the DEVMODE IntPtr pDevMode = IntPtr.Zero; // pointer to the DEVMODE IntPtr hwnd = ((Form)_controller).Handle; byte[] DevModeArray = null; try { // Get a handle to a DEVMODE for the default printer settings hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings); // Obtain a lock on the handle and get an actual pointer so Windows won't // move it around while we're futzing with it pDevMode = GlobalLock(hDevMode); int sizeNeeded = DocumentProperties(hwnd, IntPtr.Zero, printerSettings.PrinterName, IntPtr.Zero, pDevMode, 0); if (sizeNeeded <= 0) { MessageBox.Show("Devmode Bummer, Cant get size of devmode structure"); GlobalUnlock(hDevMode); GlobalFree(hDevMode); return null; } DevModeArray = new byte[sizeNeeded]; //Copies the buffer into a byte array if (mode == 1) //Save devmode structure to file { FileStream fs = new FileStream(Filename, FileMode.Create); for (int i = 0; i < sizeNeeded; ++i) { fs.WriteByte(Marshal.ReadByte(pDevMode, i)); } fs.Close(); fs.Dispose(); } if (mode == 2) //Save devmode structure to Byte array and arraylist { for (int i = 0; i < sizeNeeded; ++i) { DevModeArray[i] = (byte)(Marshal.ReadByte(pDevMode, i)); //Copies the array to an arraylist where it can be recalled } } // Unlock the handle, we're done futzing around with memory GlobalUnlock(hDevMode); // And to boot, we don't need that DEVMODE anymore, either GlobalFree(hDevMode); hDevMode = IntPtr.Zero; } catch (Exception ex) { if (hDevMode != IntPtr.Zero) { MessageBox.Show("BUGGER"); GlobalUnlock(hDevMode); // And to boot, we don't need that DEVMODE anymore, either GlobalFree(hDevMode); hDevMode = IntPtr.Zero; } } return DevModeArray; }
//Grabs the data in arraylist and chucks it back into memory "Crank the suckers out" /// <summary> /// Применение настроек принтера полученных из файла или из массива байт (mode=1 - загрузка из файла/ mode=2 - загрузка из переданного массива) /// </summary> /// <param name="printerSettings"></param> /// <param name="Filename"></param> /// /// <param name="Filename"></param> public void SetDevmode(PrinterSettings printerSettings, int mode, String Filename, byte[] devmodearray) { ///int mode ///1 = Load devmode structure from file ///2 = Load devmode structure from arraylist IntPtr hDevMode = IntPtr.Zero; // a handle to our current DEVMODE IntPtr pDevMode = IntPtr.Zero; // a pointer to our current DEVMODE Byte[] Temparray; try { var DevModeArray = devmodearray; // Obtain the current DEVMODE position in memory hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings); // Obtain a lock on the handle and get an actual pointer so Windows won't move // it around while we're futzing with it pDevMode = GlobalLock(hDevMode); // Overwrite our current DEVMODE in memory with the one we saved. // They should be the same size since we haven't like upgraded the OS // or anything. if (mode == 1) //Load devmode structure from file { FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read); Temparray = new byte[fs.Length]; fs.Read(Temparray, 0, Temparray.Length); fs.Close(); fs.Dispose(); for (int i = 0; i < Temparray.Length; ++i) { Marshal.WriteByte(pDevMode, i, Temparray[i]); } } if (mode == 2) //Load devmode structure from arraylist { for (int i = 0; i < DevModeArray.Length; ++i) { Marshal.WriteByte(pDevMode, i, DevModeArray[i]); } } // We're done futzing GlobalUnlock(hDevMode); // Tell our printer settings to use the one we just overwrote printerSettings.SetHdevmode(hDevMode); printerSettings.DefaultPageSettings.SetHdevmode(hDevMode); // It's copied to our printer settings, so we can free the OS-level one GlobalFree(hDevMode); } catch (Exception ex) { if (hDevMode != IntPtr.Zero) { MessageBox.Show("BUGGER"); GlobalUnlock(hDevMode); // And to boot, we don't need that DEVMODE anymore, either GlobalFree(hDevMode); hDevMode = IntPtr.Zero; } } }