Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing windows forms

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
Hi :)
I'd like my users to be able to print a windows form as it appears on screen - but so far i've only managed to find out how to print a text file! Could some1 pls help a newbie??
Thanx
 
This example prints a copy of the current form from VB.Net Help:

Example
[tt]
Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (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 Long

Dim memoryImage As Bitmap

Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, _
Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
PrintDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub

Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles PrintButton.Click
CaptureScreen()
PrintDocument1.Print()
End Sub
[/tt]

Compiling the Code

This example requires:

A PrintDocument component on your form named PrintDocument1.
A Button control named PrintButton. The form is printed when PrintButton is clicked.
Robust Programming

The following conditions may cause an exception:

You do not have permission to access the printer.
You do not have permission to use unmanaged code.
There is no printer installed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top