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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds