How can I prevent the Print Progress dialog box from appearing during print preview

In my C # application, I am trying to create a print preview without the progress dialog appearing on the screen.

I believe that you can use PrintDocument.PrintController to prevent this from printing for real (i.e. not for preview), however this doesn’t mean t seems to work when doing print previews. A.

My code is as follows:

public FrmDeliveryNotePrintPreview(DeliveryNote deliveryNote) { InitializeComponent(); this.Text = "Delivery Note #" + deliveryNote.Id.ToString(); // The print preview window should occupy about 90% of the // total screen height int height = (int) (Screen.PrimaryScreen.Bounds.Height * 0.9); // Making an assumption that we are printing to A4 landscape, // then adjust the width to give the correct height:width ratio // for A4 landscape. int width = (int) (height / 1.415); // Set the bounds of this form. The PrintPreviewControl is // docked, so it should just do the right thing this.SetBounds(0, 0, width, height); PrinterSettings printerSettings = new PrinterSettings(); PrintDeliveryNotes pdn = new PrintDeliveryNotes( new DeliveryNote[] { deliveryNote }, printerSettings); PrintDocument printDocument = pdn.PrintDocument; printDocument.PrintController = new PreviewPrintController(); ppcDeliveryNote.Document = printDocument; } 

Print preview works exactly the way I want, except that it displays a print preview progress dialog. A.

Suggestions please?

+4
source share
7 answers

I hate answering my question, but the solution looked in my face.

Since I already encoded the ability to print a delivery note, my next step was to provide a copy on the screen (i.e. I do not intend to print a hard copy). The preview dialog seemed like an easy way out.

In the end, I just created a custom form and drew it directly, without visible print control. A.

Unfortunately, I focused too much on trying to make the print preview dialog behave the way I wanted, instead of looking at the big problem.

+2
source

This works for me:

Set the print control of your document to StandardPrintController .

 static class Program { static void Main() { PrintDocument doc = new PrintDocument(); doc.PrintController = new StandardPrintController(); doc.PrintPage += new PrintPageEventHandler(doc_PrintPage); doc.Print(); } static void doc_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("xxx", Control.DefaultFont, Brushes.Black, new PointF(e.PageBounds.Width / 2, e.PageBounds.Height / 2)); } } 
+6
source

You might be lucky with the PreviewPrintController instead of the standard PrintController .

+1
source

Just to confirm the answer from Pooven. I had the same problem and tried to solve, the solution from Stefan did not work for me either. Then I finally looked at the source code and found out that it is hard-coded, so it cannot be changed. If you need to hide the status dialog, try finding a solution other than PrintPreviewControl. Here is the source code of PrintPreviewControl.

  private void ComputePreview() { int oldStart = StartPage; if (document == null) pageInfo = new PreviewPageInfo[0]; else { IntSecurity.SafePrinting.Demand(); PrintController oldController = document.PrintController; // --> HERE they have hardcoded it! Why they do this! PreviewPrintController previewController = new PreviewPrintController(); previewController.UseAntiAlias = UseAntiAlias; document.PrintController = new PrintControllerWithStatusDialog(previewController, SR.GetString(SR.PrintControllerWithStatusDialog_DialogTitlePreview)); // Want to make sure we've reverted any security asserts before we call Print -- that calls into user code document.Print(); pageInfo = previewController.GetPreviewPageInfo(); Debug.Assert(pageInfo != null, "ReviewPrintController did not give us preview info"); // --> and then swap the old one document.PrintController = oldController; } if (oldStart != StartPage) { OnStartPageChanged(EventArgs.Empty); } } 

Source http://reflector.webtropy.com/default.aspx/ 4@0 / 4@0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / Printing / PrintPreviewControl@cs / 1305376 / PrintPreviewControl@cs

+1
source

A workaround would be to use the EnumChildWindows API to search for a window handle, and if found, use the ShowWindow API with the SW_HIDE flag to hide the window.

Here is an example of using FindWindow if you know the name of the window:

 #region Constants private const int SW_HIDE = 0; private const int SW_SHOWNORMAL = 1; private const int SW_SHOW = 5; #endregion Constants #region APIs [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool EnableWindow(IntPtr hwnd, bool enabled); #endregion APIs public static void ShowProgress() { IntPtr h = FindWindow(null, "titleofprogresswindow"); ShowWindow(h, SW_SHOW); EnableWindow(h, true); } public static void HideProgress() { IntPtr h = FindWindow(null, "titleofprogresswindow"); ShowWindow(h, SW_HIDE); EnableWindow(h, false); } 
0
source

It seems that the PrintPreviewControl used by PrintPreviewDialog will replace PrintController PrintDocument so that PrintController PrintDocument during the preview rendering process. After performing the Print operation, the PrintController restored to its previous value. It seems that it would be impossible to configure PrintPreviewControl to use any other PrintController .

0
source

I think I did it. Use this class instead of PrintPreviewControl:

 public class PrintPreviewControlSilent : PrintPreviewControl { public new PrintDocument Document { get { return base.Document; } set { base.Document = value; PreviewPrintController ppc = new PreviewPrintController(); Document.PrintController = ppc; Document.Print(); FieldInfo fi = typeof(PrintPreviewControl).GetField("pageInfo", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); fi.SetValue(this, ppc.GetPreviewPageInfo()); } } } 
0
source

All Articles