http://www.developer.com/net/vb/article.php/3306131/The-Secret-Rebirth-of-Visual-Basics-PrintForm.htm
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: 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? 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. # # #
The Secret Rebirth of Visual Basic's .PrintForm
January 30, 2004
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
About the Author