views:

567

answers:

4

In my C# application, I'm attempting to generate a print preview without the progress dialog appearing on screen.

I believe you can use PrintDocument.PrintController to prevent this when printing for real (i.e. not a print preview), however it doesn't seem to work when performing a print preview.

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;
}

The print preview works exactly as I want, apart from the fact that the print preview progress dialog is displayed.

Suggestions please?

+1  A: 

You may have some luck with PreviewPrintController instead of StandardPrintController.

Matt Jacobsen
Yeah, however I've already tried that, and it still produces the progress dialog.
Bryan
I've updated the code sample to include the entire class as it currently stands.
Bryan
A: 

A workaround would be to use the EnumChildWindows API to find the handle to the window, and If found, use the ShowWindow API with the SW_HIDE flag to hide the window.

Here are an example for using FindWindow if you know the title 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); 

}
Stefan
A: 

Hi,

This works for me:

Set the printcontroller of your document to a 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));
        }
    }
Matt Jacobsen
sorry, just remembered that is has to work for a preview.
Matt Jacobsen
+1  A: 

I hate to answer my own question, but the solution was staring me in the face.

As I've already coded the ability to print a delivery note, my next step was to provide an on screen copy (i.e. no intention of printing a hard copy). The print preview dialog seemed like an easy way out.

In the end, I just created a custom form and painted directly on to it with no print preview control in sight.

Unfortunately, I got too focused on trying to get the print preview dialogue to behave as I wanted, rather than looking at the bigger problem.

Bryan