Esempio n. 1
0
        // Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods
        // are required for showing the print dialog on Win2k and newer OS'.
        private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
        {
            data.Flags     = GetFlags();
            data.nCopies   = PrinterSettings.Copies;
            data.hwndOwner = hwndOwner;

            try
            {
                if (PageSettings == null)
                {
                    data.hDevMode = PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = PrinterSettings.GetHdevmode(PageSettings);
                }

                data.hDevNames = PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException)
            {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
                // Leave those fields null; Windows will fill them in
            }

            try
            {
                // Windows doesn't like it if page numbers are invalid
                if (AllowSomePages)
                {
                    if (PrinterSettings.FromPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.FromPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "FromPage"));
                    }

                    if (PrinterSettings.ToPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.ToPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "ToPage"));
                    }

                    if (PrinterSettings.ToPage < PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "FromPage"));
                    }

                    unsafe
                    {
                        int *pageRangeField = (int *)data.pageRanges;
                        *    pageRangeField = PrinterSettings.FromPage;
                        pageRangeField += 1;
                        *pageRangeField = PrinterSettings.ToPage;
                    }
                    data.nPageRanges = 1;

                    data.nMinPage = PrinterSettings.MinimumPage;
                    data.nMaxPage = PrinterSettings.MaximumPage;
                }

                //
                // The flags NativeMethods.PD_SHOWHELP and NativeMethods.PD_NONETWORKBUTTON don't work with
                // PrintDlgEx. So we have to strip them out.
                data.Flags &= ~(NativeMethods.PD_SHOWHELP | NativeMethods.PD_NONETWORKBUTTON);

                int hr = UnsafeNativeMethods.PrintDlgEx(data);
                if (NativeMethods.Failed(hr) || data.dwResultAction == NativeMethods.PD_RESULT_CANCEL)
                {
                    return(false);
                }

                UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, PrinterSettings, PageSettings);

                PrintToFile = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
                PrinterSettings.PrintToFile = PrintToFile;
                if (AllowSomePages)
                {
                    unsafe
                    {
                        int *pageRangeField = (int *)data.pageRanges;
                        PrinterSettings.FromPage = *pageRangeField;
                        pageRangeField          += 1;
                        PrinterSettings.ToPage   = *pageRangeField;
                    }
                }

                // When the flag PD_USEDEVMODECOPIESANDCOLLATE is not set,
                // PRINTDLG.nCopies or PRINTDLG.nCopies indicates the number of copies the user wants
                // to print, and the PD_COLLATE flag in the Flags member indicates
                // whether the user wants to print them collated.
                if ((data.Flags & NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE) == 0)
                {
                    PrinterSettings.Copies  = (short)(data.nCopies);
                    PrinterSettings.Collate = ((data.Flags & NativeMethods.PD_COLLATE) == NativeMethods.PD_COLLATE);
                }

                // We should return true only if the user pressed the "Print" button while dismissing the dialog.
                return(data.dwResultAction == NativeMethods.PD_RESULT_PRINT);
            }
            finally
            {
                if (data.hDevMode != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                }

                if (data.hDevNames != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
                }

                if (data.pageRanges != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.pageRanges));
                }
            }
        }
Esempio n. 2
0
        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));
            }
        }
Esempio n. 4
0
        private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.WndProc hookProcPtr, NativeMethods.PRINTDLG data)
        {
            data.Flags         = GetFlags();
            data.nCopies       = (short)PrinterSettings.Copies;
            data.hwndOwner     = hwndOwner;
            data.lpfnPrintHook = hookProcPtr;

            try
            {
                if (PageSettings == null)
                {
                    data.hDevMode = PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = PrinterSettings.GetHdevmode(PageSettings);
                }

                data.hDevNames = PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException)
            {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
                // Leave those fields null; Windows will fill them in
            }

            try
            {
                // Windows doesn't like it if page numbers are invalid
                if (AllowSomePages)
                {
                    if (PrinterSettings.FromPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.FromPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "FromPage"));
                    }

                    if (PrinterSettings.ToPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.ToPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "ToPage"));
                    }

                    if (PrinterSettings.ToPage < PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(string.Format(SR.PDpageOutOfRange, "FromPage"));
                    }

                    data.nFromPage = (short)PrinterSettings.FromPage;
                    data.nToPage   = (short)PrinterSettings.ToPage;
                    data.nMinPage  = (short)PrinterSettings.MinimumPage;
                    data.nMaxPage  = (short)PrinterSettings.MaximumPage;
                }

                if (!UnsafeNativeMethods.PrintDlg(data))
                {
                    return(false);
                }

                UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, settings, PageSettings);

                PrintToFile = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
                PrinterSettings.PrintToFile = PrintToFile;

                if (AllowSomePages)
                {
                    PrinterSettings.FromPage = data.nFromPage;
                    PrinterSettings.ToPage   = data.nToPage;
                }

                // When the flag PD_USEDEVMODECOPIESANDCOLLATE is not set,
                // PRINTDLG.nCopies or PRINTDLG.nCopies indicates the number of copies the user wants
                // to print, and the PD_COLLATE flag in the Flags member indicates
                // whether the user wants to print them collated.
                if ((data.Flags & NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE) == 0)
                {
                    PrinterSettings.Copies  = data.nCopies;
                    PrinterSettings.Collate = ((data.Flags & NativeMethods.PD_COLLATE) == NativeMethods.PD_COLLATE);
                }

                return(true);
            }
            finally
            {
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
            }
        }
        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);
        }
Esempio n. 6
0
        // VSWhidbey 93449: Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods
        // are required for showing the print dialog on Win2k and newer OS'.
        private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.WndProc hookProcPtr, NativeMethods.PRINTDLG data)
        {
            data.Flags         = GetFlags();
            data.nCopies       = (short)PrinterSettings.Copies;
            data.hwndOwner     = hwndOwner;
            data.lpfnPrintHook = hookProcPtr;

            IntSecurity.AllPrintingAndUnmanagedCode.Assert();

            try {
                if (PageSettings == null)
                {
                    data.hDevMode = PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = PrinterSettings.GetHdevmode(PageSettings);
                }

                data.hDevNames = PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException) {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
                // Leave those fields null; Windows will fill them in
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }

            try {
                // Windows doesn't like it if page numbers are invalid
                if (AllowSomePages)
                {
                    if (PrinterSettings.FromPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.FromPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "FromPage"));
                    }
                    if (PrinterSettings.ToPage < PrinterSettings.MinimumPage ||
                        PrinterSettings.ToPage > PrinterSettings.MaximumPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "ToPage"));
                    }
                    if (PrinterSettings.ToPage < PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "FromPage"));
                    }

                    data.nFromPage = (short)PrinterSettings.FromPage;
                    data.nToPage   = (short)PrinterSettings.ToPage;
                    data.nMinPage  = (short)PrinterSettings.MinimumPage;
                    data.nMaxPage  = (short)PrinterSettings.MaximumPage;
                }

                if (!UnsafeNativeMethods.PrintDlg(data))
                {
                    return(false);
                }

                IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                try {
                    UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, settings, PageSettings);
                }
                finally {
                    CodeAccessPermission.RevertAssert();
                }
                PrintToFile = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
                PrinterSettings.PrintToFile = PrintToFile;

                if (AllowSomePages)
                {
                    PrinterSettings.FromPage = data.nFromPage;
                    PrinterSettings.ToPage   = data.nToPage;
                }

                // Fix Dev10 #575399, when the flag PD_USEDEVMODECOPIESANDCOLLATE is not set,
                // PRINTDLG.nCopies or PRINTDLG.nCopies indicates the number of copies the user wants
                // to print, and the PD_COLLATE flag in the Flags member indicates
                // whether the user wants to print them collated.
                // Due to a Windows OS Bug 558734, we don't need to consider Windows XP and before
                if ((data.Flags & NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE) == 0)
                {
                    if (Environment.OSVersion.Version.Major >= 6)
                    {
                        PrinterSettings.Copies  = data.nCopies;
                        PrinterSettings.Collate = ((data.Flags & NativeMethods.PD_COLLATE) == NativeMethods.PD_COLLATE);
                    }
                }

                return(true);
            }
            finally {
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
            }
        }
Esempio n. 7
0
        /// <include file='doc\PrintDialog.uex' path='docs/doc[@for="PrintDialog.RunDialog"]/*' />
        /// <devdoc>
        /// </devdoc>
        /// <internalonly/>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            IntSecurity.SafePrinting.Demand();

            NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc);
            if (settings == null)
            {
                throw new ArgumentException(SR.GetString(SR.PDcantShowWithoutPrinter));
            }

            NativeMethods.PRINTDLG data = CreatePRINTDLG();
            data.Flags         = GetFlags();
            data.nCopies       = (short)settings.Copies;
            data.hwndOwner     = hwndOwner;
            data.lpfnPrintHook = hookProcPtr;

            IntSecurity.AllPrinting.Assert();

            try {
                if (PageSettings == null)
                {
                    data.hDevMode = settings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = settings.GetHdevmode(PageSettings);
                }

                data.hDevNames = settings.GetHdevnames();
            }
            catch (InvalidPrinterException) {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
                // Leave those fields null; Windows will fill them in
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }

            try {
                // Windows doesn't like it if page numbers are invalid
                if (AllowSomePages)
                {
                    if (settings.FromPage < settings.MinimumPage ||
                        settings.FromPage > settings.MaximumPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "FromPage"));
                    }
                    if (settings.ToPage < settings.MinimumPage ||
                        settings.ToPage > settings.MaximumPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "ToPage"));
                    }
                    if (settings.ToPage < settings.FromPage)
                    {
                        throw new ArgumentException(SR.GetString(SR.PDpageOutOfRange, "FromPage"));
                    }

                    data.nFromPage = (short)settings.FromPage;
                    data.nToPage   = (short)settings.ToPage;
                    data.nMinPage  = (short)settings.MinimumPage;
                    data.nMaxPage  = (short)settings.MaximumPage;
                }

                if (!UnsafeNativeMethods.PrintDlg(data))
                {
                    return(false);
                }

                UpdatePrinterSettings(data, settings, PageSettings);
                PrintToFile          = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
                settings.PrintToFile = PrintToFile;

                if (AllowSomePages)
                {
                    settings.FromPage = data.nFromPage;
                    settings.ToPage   = data.nToPage;
                }

                return(true);
            }
            finally {
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
            }
        }
Esempio n. 8
0
        private bool ShowPrintDialog(IntPtr hwndOwner, System.Windows.Forms.NativeMethods.WndProc hookProcPtr, System.Windows.Forms.NativeMethods.PRINTDLG data)
        {
            bool flag;

            data.Flags         = this.GetFlags();
            data.nCopies       = this.PrinterSettings.Copies;
            data.hwndOwner     = hwndOwner;
            data.lpfnPrintHook = hookProcPtr;
            System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try
            {
                if (this.PageSettings == null)
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode(this.PageSettings);
                }
                data.hDevNames = this.PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException)
            {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            try
            {
                if (this.AllowSomePages)
                {
                    if ((this.PrinterSettings.FromPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.FromPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    if ((this.PrinterSettings.ToPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.ToPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "ToPage" }));
                    }
                    if (this.PrinterSettings.ToPage < this.PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    data.nFromPage = (short)this.PrinterSettings.FromPage;
                    data.nToPage   = (short)this.PrinterSettings.ToPage;
                    data.nMinPage  = (short)this.PrinterSettings.MinimumPage;
                    data.nMaxPage  = (short)this.PrinterSettings.MaximumPage;
                }
                if (!UnsafeNativeMethods.PrintDlg(data))
                {
                    return(false);
                }
                System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                try
                {
                    UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, this.settings, this.PageSettings);
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                this.PrintToFile = (data.Flags & 0x20) != 0;
                this.PrinterSettings.PrintToFile = this.PrintToFile;
                if (this.AllowSomePages)
                {
                    this.PrinterSettings.FromPage = data.nFromPage;
                    this.PrinterSettings.ToPage   = data.nToPage;
                }
                if (((data.Flags & 0x40000) == 0) && (Environment.OSVersion.Version.Major >= 6))
                {
                    this.PrinterSettings.Copies  = data.nCopies;
                    this.PrinterSettings.Collate = (data.Flags & 0x10) == 0x10;
                }
                flag = true;
            }
            finally
            {
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
            }
            return(flag);
        }
Esempio n. 9
0
        private unsafe bool ShowPrintDialog(IntPtr hwndOwner, System.Windows.Forms.NativeMethods.PRINTDLGEX data)
        {
            bool flag;

            data.Flags     = this.GetFlags();
            data.nCopies   = this.PrinterSettings.Copies;
            data.hwndOwner = hwndOwner;
            System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try
            {
                if (this.PageSettings == null)
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode(this.PageSettings);
                }
                data.hDevNames = this.PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException)
            {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            try
            {
                if (this.AllowSomePages)
                {
                    if ((this.PrinterSettings.FromPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.FromPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    if ((this.PrinterSettings.ToPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.ToPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "ToPage" }));
                    }
                    if (this.PrinterSettings.ToPage < this.PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    int *pageRanges = (int *)data.pageRanges;
                    pageRanges[0] = this.PrinterSettings.FromPage;
                    pageRanges++;
                    pageRanges[0]    = this.PrinterSettings.ToPage;
                    data.nPageRanges = 1;
                    data.nMinPage    = this.PrinterSettings.MinimumPage;
                    data.nMaxPage    = this.PrinterSettings.MaximumPage;
                }
                data.Flags &= -2099201;
                if (System.Windows.Forms.NativeMethods.Failed(UnsafeNativeMethods.PrintDlgEx(data)) || (data.dwResultAction == 0))
                {
                    return(false);
                }
                System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                try
                {
                    UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, this.PrinterSettings, this.PageSettings);
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                this.PrintToFile = (data.Flags & 0x20) != 0;
                this.PrinterSettings.PrintToFile = this.PrintToFile;
                if (this.AllowSomePages)
                {
                    int *numPtr2 = (int *)data.pageRanges;
                    this.PrinterSettings.FromPage = numPtr2[0];
                    numPtr2++;
                    this.PrinterSettings.ToPage = numPtr2[0];
                }
                if (((data.Flags & 0x40000) == 0) && (Environment.OSVersion.Version.Major >= 6))
                {
                    this.PrinterSettings.Copies  = (short)data.nCopies;
                    this.PrinterSettings.Collate = (data.Flags & 0x10) == 0x10;
                }
                flag = data.dwResultAction == 1;
            }
            finally
            {
                if (data.hDevMode != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                }
                if (data.hDevNames != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
                }
                if (data.pageRanges != IntPtr.Zero)
                {
                    UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.pageRanges));
                }
            }
            return(flag);
        }