3

I just created a Database Application for Requesting Parts.

It has several forms, one for the requester, one for the Supervisor Approval, one for the Purchase approval and one for the Clerk to use to know what to order.

Now I am a big fan of paperless, but my employer Really like their paper. Is there an easy way to WYSIWYG duplicate my Windows forms to paper?

I should also add that I am restricted to using the 2.0 .Net framework

Thanks

2 Answers 2

4

Here's a quick way of doing it. You can clean up the code to make it suit your needs:

    public static class FormExtensions
    {
        public static void PrintForm(this Form f)
        {
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += (o, e) =>
            {
                Bitmap image = new Bitmap(f.ClientRectangle.Width, f.ClientRectangle.Height);
                f.DrawToBitmap(image, f.ClientRectangle);

                e.Graphics.DrawImage(image, e.PageBounds);
            };

            doc.Print();
        }
    }

This will stretch out the form to the size of the page. You can tweak the second parameter of rhe DrawImage method call to draw it in other places if you want to.

2

Here's a code sample from MSDN that will do what you want:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
   Graphics mygraphics = this.CreateGraphics();
   Size s = this.Size;
   memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
   Graphics memoryGraphics = Graphics.FromImage(memoryImage);
   IntPtr dc1 = mygraphics.GetHdc();
   IntPtr dc2 = memoryGraphics.GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}

There are some caveats - there's no exception checking here, plus you'll need to be running in full trust in order to use the unmanaged BitBlt API - but this is probably the easiest way to print a Windows Forms form, as long as it is displayed on the screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.