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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds