private static void UpdateSettings(NativeMethods.PAGESETUPDLG data, PageSettings pageSettings, PrinterSettings printerSettings) { // SetHDevMode demands AllPrintingAndUnmanagedCode Permission : Since we are calling that function we should Assert the permision, IntSecurity.AllPrintingAndUnmanagedCode.Assert(); try { pageSettings.SetHdevmode(data.hDevMode); if (printerSettings != null) { printerSettings.SetHdevmode(data.hDevMode); printerSettings.SetHdevnames(data.hDevNames); } } finally { CodeAccessPermission.RevertAssert(); } Margins newMargins = new Margins(); newMargins.Left = data.marginLeft; newMargins.Top = data.marginTop; newMargins.Right = data.marginRight; newMargins.Bottom = data.marginBottom; PrinterUnit fromUnit = ((data.Flags & NativeMethods.PSD_INHUNDREDTHSOFMILLIMETERS) != 0) ? PrinterUnit.HundredthsOfAMillimeter : PrinterUnit.ThousandthsOfAnInch; pageSettings.Margins = PrinterUnitConvert.Convert(newMargins, fromUnit, PrinterUnit.Display); }
private static void UpdateSettings(NativeMethods.PAGESETUPDLG data, PageSettings pageSettings, PrinterSettings printerSettings) { pageSettings.SetHdevmode(data.hDevMode); if (printerSettings != null) { printerSettings.SetHdevmode(data.hDevMode); printerSettings.SetHdevnames(data.hDevNames); } Margins newMargins = new Margins(); newMargins.Left = data.marginLeft; newMargins.Top = data.marginTop; newMargins.Right = data.marginRight; newMargins.Bottom = data.marginBottom; PrinterUnit fromUnit = ((data.Flags & NativeMethods.PSD_INHUNDREDTHSOFMILLIMETERS) != 0) ? PrinterUnit.HundredthsOfAMillimeter : PrinterUnit.ThousandthsOfAnInch; pageSettings.Margins = PrinterUnitConvert.Convert(newMargins, fromUnit, PrinterUnit.Display); }
protected override bool RunDialog(IntPtr hwndOwner) { NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(HookProc); if (pageSettings == null) { throw new ArgumentException(SR.PSDcantShowWithoutPage); } NativeMethods.PAGESETUPDLG data = new NativeMethods.PAGESETUPDLG(); data.lStructSize = Marshal.SizeOf(data); data.Flags = GetFlags(); data.hwndOwner = hwndOwner; data.lpfnPageSetupHook = hookProcPtr; PrinterUnit toUnit = PrinterUnit.ThousandthsOfAnInch; // Below was a breaking change from RTM and EVERETT even though this was a correct FIX. // EnableMetric is a new Whidbey property which we allow the users to choose between the AutoConversion or not. if (EnableMetric) { //take the Units of Measurement while determining the PrinterUnits... // StringBuilder sb = new StringBuilder(2); int result = UnsafeNativeMethods.GetLocaleInfo(NativeMethods.LOCALE_USER_DEFAULT, NativeMethods.LOCALE_IMEASURE, sb, sb.Capacity); if (result > 0 && int.Parse(sb.ToString(), CultureInfo.InvariantCulture) == 0) { toUnit = PrinterUnit.HundredthsOfAMillimeter; } } if (MinMargins != null) { Margins margins = PrinterUnitConvert.Convert(MinMargins, PrinterUnit.Display, toUnit); data.minMarginLeft = margins.Left; data.minMarginTop = margins.Top; data.minMarginRight = margins.Right; data.minMarginBottom = margins.Bottom; } if (pageSettings.Margins != null) { Margins margins = PrinterUnitConvert.Convert(pageSettings.Margins, PrinterUnit.Display, toUnit); data.marginLeft = margins.Left; data.marginTop = margins.Top; data.marginRight = margins.Right; data.marginBottom = margins.Bottom; } // Ensure that the margins are >= minMargins. // This is a requirement of the PAGESETUPDLG structure. // data.marginLeft = Math.Max(data.marginLeft, data.minMarginLeft); data.marginTop = Math.Max(data.marginTop, data.minMarginTop); data.marginRight = Math.Max(data.marginRight, data.minMarginRight); data.marginBottom = Math.Max(data.marginBottom, data.minMarginBottom); PrinterSettings printer = printerSettings ?? pageSettings.PrinterSettings; data.hDevMode = printer.GetHdevmode(pageSettings); data.hDevNames = printer.GetHdevnames(); try { bool status = UnsafeNativeMethods.PageSetupDlg(data); if (!status) { // Debug.WriteLine(Windows.CommonDialogErrorToString(Windows.CommDlgExtendedError())); return(false); } UpdateSettings(data, pageSettings, printerSettings); // yes, printerSettings, not printer return(true); } finally { UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode)); UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames)); } }
/// <include file='doc\PageSetupDialog.uex' path='docs/doc[@for="PageSetupDialog.RunDialog"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> protected override bool RunDialog(IntPtr hwndOwner) { IntSecurity.SafePrinting.Demand(); NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc); if (pageSettings == null) { throw new ArgumentException(SR.GetString(SR.PSDcantShowWithoutPage)); } NativeMethods.PAGESETUPDLG data = new NativeMethods.PAGESETUPDLG(); data.lStructSize = Marshal.SizeOf(data); data.Flags = GetFlags(); data.hwndOwner = hwndOwner; data.lpfnPageSetupHook = hookProcPtr; if (MinMargins != null) { PrinterUnit toUnit = PrinterUnit.ThousandthsOfAnInch; Margins margins = PrinterUnitConvert.Convert(MinMargins, PrinterUnit.Display, toUnit); data.minMarginLeft = margins.Left; data.minMarginTop = margins.Top; data.minMarginRight = margins.Right; data.minMarginBottom = margins.Bottom; } if (pageSettings.Margins != null) { PrinterUnit toUnit = PrinterUnit.ThousandthsOfAnInch; Margins margins = PrinterUnitConvert.Convert(pageSettings.Margins, PrinterUnit.Display, toUnit); data.marginLeft = margins.Left; data.marginTop = margins.Top; data.marginRight = margins.Right; data.marginBottom = margins.Bottom; } // Ensure that the margins are >= minMargins. // This is a requirement of the PAGESETUPDLG structure. // data.marginLeft = Math.Max(data.marginLeft, data.minMarginLeft); data.marginTop = Math.Max(data.marginTop, data.minMarginTop); data.marginRight = Math.Max(data.marginRight, data.minMarginRight); data.marginBottom = Math.Max(data.marginBottom, data.minMarginBottom); PrinterSettings printer = (printerSettings == null) ? pageSettings.PrinterSettings : printerSettings; IntSecurity.AllPrinting.Assert(); try { data.hDevMode = printer.GetHdevmode(pageSettings); data.hDevNames = printer.GetHdevnames(); } finally { CodeAccessPermission.RevertAssert(); } try { bool status = UnsafeNativeMethods.PageSetupDlg(data); if (!status) { // Debug.WriteLine(Windows.CommonDialogErrorToString(Windows.CommDlgExtendedError())); return(false); } UpdateSettings(data, pageSettings, printerSettings); // yes, printerSettings, not printer return(true); } finally { UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode)); UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames)); } }