The Secret Rebirth of Visual Basic's .PrintForm

If you've managed to print anything in .NET, you'll know it's no mean task. What, you may ask, happened to the old .PrintForm method of VB6 fame? Unfortunately, like a number of older features, it got sold out in the name of "standardization."

But don't fret: With just a few lines of extra code, we can bring it back from the dead. How? Simply follow these four easy steps:

  1. Design your form as usual, adding all the required controls you want to be printed.
  2. From your Toolbox, add one PictureBox, PrintDocument, and PrintDialog controls to your form. These will all be used by our code to support the printing of your form. For this code sample, I've named my controls MyPictureBox, MyPrintDocument, and MyPrintDialog respectively. The PictureBox is the only "visible" control, which you can make invisible if you want.
  3. Add the following code behind your form. These functions perform the basic function of taking a screenshot and sending the results to the printer. The main method is PrintForm:
  4. Public Sub PrintForm()
        ' Takes a screenshot, then initiates the print
        GrabScreen()
        MyPrintDialog.Document = MyPrintDocument
        If MyPrintDialog.ShowDialog = DialogResult.OK Then
            MyPrintDocument.Print()
        End If
    End Sub
    
    ' API call to help generate final screenshot
    Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal _
        hdcDest As IntPtr, ByVal _
        nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth _
        As Integer, ByVal nHeight _
        As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc _
        As Integer, _
        ByVal nYSrc As Integer, ByVal dwRop As System.Int32) _
        As Boolean
    ' Variable to store screenshot
    Private bmpScreenshot As Bitmap
    
    Private Sub GrabScreen()
      ' Performs a screenshot, saving results to bmpScreenshot
      Dim objGraphics As Graphics = Me.CreateGraphics
      Dim objSize As Size = Me.Size
      Const SRCCOPY As Integer = &HCC0020
    
      bmpScreenshot = New Bitmap(objSize.Width, objSize.Height, _
                                 objGraphics)
      Dim objGraphics2 As Graphics = _
          objGraphics.FromImage(bmpScreenshot)
      Dim deviceContext1 As IntPtr = objGraphics.GetHdc
      Dim deviceContext2 As IntPtr = objGraphics2.GetHdc
    
      BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, _
        Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
      objGraphics.ReleaseHdc(deviceContext1)
      objGraphics2.ReleaseHdc(deviceContext2)
    End Sub
    
    Private Sub MyPrintDocument_PrintPage(ByVal sender _
      As System.Object, _
      ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
      Handles MyPrintDocument.PrintPage
      ' Method that handles the printing
      Dim objImageToPrint As Graphics = e.Graphics
      objImageToPrint.DrawImage(bmpScreenshot, 0, 0)
      bmpScreenshot.Dispose()
      objImageToPrint.Dispose()
      e.HasMorePages = False
    End Sub
    
  5. Add a print button to your control, add code to run the PrintForm method; then simply wait and let our code run its magic! Don't forget, you may want to make certain controls (such as the print button itself) invisible before running the PrintForm method, then making it visible again afterward.

And that's quite simply all you need to print your form. Admittedly not as easy as .PrintForm, but when the code is already written and ready-to-roll, who's complaining?

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book, plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.



About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • By now you've likely heard of Agile development and building products in small incremental pieces, so you can get real feedback along the way. In fact, you may even be considering using Agile on your next project. But where do you start? Agile can take a lot of forms, such as Scrum or Kanban. Each form has advantages and disadvantages, but both will help your team get the right feedback they need to build great products. Read this white paper to find out which one is right for you.

  • Gain buy-in, choose the right tools, and encourage employees to participate. Whether you need help introducing social media or making your efforts more successful, this is the guide for you. It contains 140 tips in tweet form (140 characters or less) with ideas for promoting social media, deciding which venues to use, and encouraging participation. Plus, the guide contains links to social media statistics, informative websites, and other resources.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date