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:
- Design your form as usual, adding all the required controls you want to be printed.
- 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.
- 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:
- 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.
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
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.
# # #