Пример #1
0
        ShowDialog()
        {
            UInt32 result = NativeMethods.PD_RESULT_CANCEL;

            //
            // Get the process main window handle
            //
            IntPtr owner = IntPtr.Zero;

            if ((System.Windows.Application.Current != null) &&
                (System.Windows.Application.Current.MainWindow != null))
            {
                System.Windows.Interop.WindowInteropHelper helper =
                    new System.Windows.Interop.WindowInteropHelper(System.Windows.Application.Current.MainWindow);
                owner = helper.CriticalHandle;
            }

            try
            {
                if (this._printQueue == null || this._printTicket == null)
                {
                    // Normally printDlgEx.SyncToStruct() probes the printer if both the print queue and print
                    // ticket are not null.
                    // If either is null we probe the printer ourselves
                    // If we dont end users will get notified that printing is disabled *after*
                    // the print dialog has been displayed.

                    ProbeForPrintingSupport();
                }

                //
                // Create a PrintDlgEx instance to invoke the Win32 Print Dialog
                //
                using (PrintDlgExMarshaler printDlgEx = new PrintDlgExMarshaler(owner, this))
                {
                    printDlgEx.SyncToStruct();

                    //
                    // Display the Win32 print dialog
                    //
                    Int32 hr = UnsafeNativeMethods.PrintDlgEx(printDlgEx.UnmanagedPrintDlgEx);
                    if (hr == MS.Win32.NativeMethods.S_OK)
                    {
                        result = printDlgEx.SyncFromStruct();
                    }
                }
            }
            //
            // NOTE:
            // This code was previously catch(PrintingNotSupportedException), but that created a circular dependency
            // between ReachFramework.dll and PresentationFramework.dll. Instead, we now catch Exception, check its full type name
            // and rethrow if it doesn't match. Not perfect, but better than having a circular dependency.
            //
            catch(Exception e)
            {
                if (String.Equals(e.GetType().FullName, "System.Printing.PrintingNotSupportedException", StringComparison.Ordinal))
                {
                    string message = System.Windows.SR.Get(System.Windows.SRID.PrintDialogInstallPrintSupportMessageBox);
                    string caption = System.Windows.SR.Get(System.Windows.SRID.PrintDialogInstallPrintSupportCaption);

                    bool isRtlCaption = caption != null && caption.Length > 0 && caption[0] == RightToLeftMark;
                    System.Windows.MessageBoxOptions mbOptions = isRtlCaption ? System.Windows.MessageBoxOptions.RtlReading : System.Windows.MessageBoxOptions.None;

                    int type =
                          (int) System.Windows.MessageBoxButton.OK
                        | (int) System.Windows.MessageBoxImage.Information
                        | (int) mbOptions;

                    if (owner == IntPtr.Zero)
                    {
                        owner = MS.Win32.UnsafeNativeMethods.GetActiveWindow();
                    }

                    if(0 != MS.Win32.UnsafeNativeMethods.MessageBox(new HandleRef(null, owner), message, caption, type))
                    {
                         result = NativeMethods.PD_RESULT_CANCEL;
                    }
                }
                else
                {
                    // Not a PrintingNotSupportedException, rethrow
                    throw;
                }
            }

            return result;
        }
        ShowDialog()
        {
            UInt32 result = NativeMethods.PD_RESULT_CANCEL;

            //
            // Get the process main window handle
            //
            IntPtr owner = IntPtr.Zero;

            if ((System.Windows.Application.Current != null) &&
                (System.Windows.Application.Current.MainWindow != null))
            {
                System.Windows.Interop.WindowInteropHelper helper =
                    new System.Windows.Interop.WindowInteropHelper(System.Windows.Application.Current.MainWindow);
                owner = helper.CriticalHandle;
            }

            try
            {
                if (this._printQueue == null || this._printTicket == null)
                {
                    // Normally printDlgEx.SyncToStruct() probes the printer if both the print queue and print
                    // ticket are not null.
                    // If either is null we probe the printer ourselves
                    // If we dont end users will get notified that printing is disabled *after*
                    // the print dialog has been displayed.

                    ProbeForPrintingSupport();
                }

                //
                // Create a PrintDlgEx instance to invoke the Win32 Print Dialog
                //
                using (PrintDlgExMarshaler printDlgEx = new PrintDlgExMarshaler(owner, this))
                {
                    printDlgEx.SyncToStruct();

                    //
                    // Display the Win32 print dialog
                    //
                    Int32 hr = UnsafeNativeMethods.PrintDlgEx(printDlgEx.UnmanagedPrintDlgEx);
                    if (hr == MS.Win32.NativeMethods.S_OK)
                    {
                        result = printDlgEx.SyncFromStruct();
                    }
                }
            }
            //
            // NOTE:
            // This code was previously catch(PrintingNotSupportedException), but that created a circular dependency
            // between ReachFramework.dll and PresentationFramework.dll. Instead, we now catch Exception, check its full type name
            // and rethrow if it doesn't match. Not perfect, but better than having a circular dependency.
            //
            catch (Exception e)
            {
                if (String.Equals(e.GetType().FullName, "System.Printing.PrintingNotSupportedException", StringComparison.Ordinal))
                {
                    string message = System.Windows.SR.Get(System.Windows.SRID.PrintDialogInstallPrintSupportMessageBox);
                    string caption = System.Windows.SR.Get(System.Windows.SRID.PrintDialogInstallPrintSupportCaption);

                    bool isRtlCaption = caption != null && caption.Length > 0 && caption[0] == RightToLeftMark;
                    System.Windows.MessageBoxOptions mbOptions = isRtlCaption ? System.Windows.MessageBoxOptions.RtlReading : System.Windows.MessageBoxOptions.None;

                    int type =
                        (int)System.Windows.MessageBoxButton.OK
                        | (int)System.Windows.MessageBoxImage.Information
                        | (int)mbOptions;

                    if (owner == IntPtr.Zero)
                    {
                        owner = MS.Win32.UnsafeNativeMethods.GetActiveWindow();
                    }

                    if (0 != MS.Win32.UnsafeNativeMethods.MessageBox(new HandleRef(null, owner), message, caption, type))
                    {
                        result = NativeMethods.PD_RESULT_CANCEL;
                    }
                }
                else
                {
                    // Not a PrintingNotSupportedException, rethrow
                    throw;
                }
            }

            return(result);
        }