Example #1
0
 internal static NativeMethods.PRINTDLGEX CreatePRINTDLGEX()
 {
     NativeMethods.PRINTDLGEX data = new NativeMethods.PRINTDLGEX();
     data.lStructSize    = Marshal.SizeOf(data);
     data.hwndOwner      = IntPtr.Zero;
     data.hDevMode       = IntPtr.Zero;
     data.hDevNames      = IntPtr.Zero;
     data.hDC            = IntPtr.Zero;
     data.Flags          = 0;
     data.Flags2         = 0;
     data.ExclusionFlags = 0;
     data.nPageRanges    = 0;
     data.nMaxPageRanges = 1;
     data.pageRanges     = UnsafeNativeMethods.GlobalAlloc(NativeMethods.GPTR,
                                                           data.nMaxPageRanges * Marshal.SizeOf <NativeMethods.PRINTPAGERANGE>());
     data.nMinPage            = 0;
     data.nMaxPage            = 9999;
     data.nCopies             = 1;
     data.hInstance           = IntPtr.Zero;
     data.lpPrintTemplateName = null;
     data.nPropertyPages      = 0;
     data.lphPropertyPages    = IntPtr.Zero;
     data.nStartPage          = NativeMethods.START_PAGE_GENERAL;
     data.dwResultAction      = 0;
     return(data);
 }
Example #2
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (!UseEXDialog)
            {
                return(ShowPrintDialog(hwndOwner));
            }

            NativeMethods.PRINTDLGEX data = CreatePRINTDLGEX();
            return(ShowPrintDialog(hwndOwner, data));
        }
Example #3
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            var hookProcPtr = new NativeMethods.WndProc(HookProc);

            if (!UseEXDialog)
            {
                NativeMethods.PRINTDLG data = CreatePRINTDLG();
                return(ShowPrintDialog(hwndOwner, hookProcPtr, data));
            }
            else
            {
                NativeMethods.PRINTDLGEX data = CreatePRINTDLGEX();
                return(ShowPrintDialog(hwndOwner, data));
            }
        }
Example #4
0
        /// <include file='doc\PrintDialog.uex' path='docs/doc[@for="PrintDialog.RunDialog"]/*' />
        /// <devdoc>
        /// </devdoc>
        /// <internalonly/>
        // Use PrintDlgEx and PRINTDLGEX on Win2k and newer OS'.
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool returnValue = false;

            NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc);

            if (!UseEXDialog || (Environment.OSVersion.Platform != System.PlatformID.Win32NT ||
                                 Environment.OSVersion.Version.Major < 5))
            {
                NativeMethods.PRINTDLG data = CreatePRINTDLG();
                returnValue = ShowPrintDialog(hwndOwner, hookProcPtr, data);
            }
            else
            {
                NativeMethods.PRINTDLGEX data = CreatePRINTDLGEX();
                returnValue = ShowPrintDialog(hwndOwner, data);
            }

            return(returnValue);
        }
Example #5
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));
                }
            }
        }
Example #6
0
 public static extern HRESULT PrintDlgEx([In, Out] NativeMethods.PRINTDLGEX lppdex);
 // VSWhidbey 93449: Use PRINTDLGEX on Win2k and newer OS'. Note that at the time of this
 // fix, PrinterSettings did not support multiple page ranges. (See VSWhidbey 193829.)
 // Create a PRINTDLGEX with a few useful defaults.
 internal static NativeMethods.PRINTDLGEX CreatePRINTDLGEX() {
     NativeMethods.PRINTDLGEX data = new NativeMethods.PRINTDLGEX();
     data.lStructSize = Marshal.SizeOf(data);
     data.hwndOwner = IntPtr.Zero;
     data.hDevMode = IntPtr.Zero;
     data.hDevNames = IntPtr.Zero;
     data.hDC = IntPtr.Zero;
     data.Flags = 0;
     data.Flags2 = 0;
     data.ExclusionFlags = 0;
     data.nPageRanges = 0;
     data.nMaxPageRanges = 1;
     data.pageRanges = UnsafeNativeMethods.GlobalAlloc(NativeMethods.GPTR,
                                                       data.nMaxPageRanges * Marshal.SizeOf(typeof(NativeMethods.PRINTPAGERANGE)));
     data.nMinPage = 0;
     data.nMaxPage = 9999;
     data.nCopies = 1;
     data.hInstance = IntPtr.Zero;
     data.lpPrintTemplateName = null;
     data.nPropertyPages = 0;
     data.lphPropertyPages = IntPtr.Zero;
     data.nStartPage = NativeMethods.START_PAGE_GENERAL;
     data.dwResultAction = 0;
     return data;
 }