Printing a popup form

wcsloco

Member
Joined
Feb 7, 2005
Messages
14
Programming Experience
5-10
I had posted this in the general discussion, but I later found a forum specifically for forms, so I wanted to get people's input here.

Through a bit of research, I've been able to figure out how to print an entire active form through the click of a button. Now what I'm trying to do is the following scenario:

1) Click a button on form A
2) Have that button click call form B, passing in some parameters
3) Have form B display key information about the parameters
4) Print form B
5) Close form B

I tried to do the following from form B:

-------------------------begin snipet----------------------------

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 formB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CaptureScreen()
PrintDocument1.Print()
MyBase.Close()
End Sub

--------------------------end snipet-----------------------------

I thought this would capture the screen and print, and then close. It seems to be pretty close. I do get form B to open, something prints, and then have form B close, but what prints is actually part of form A. What prints is some small part of form A, exactly the size of what form B is. Can anyone offer suggestions how to get form B to load, print, and close? At this point, I'm stuck with having a "Print" button on form B, and when the user click the print button, it first hides the button and then prints the form. But, I don't want to have that additional step. Any suggestions would be greatly appreciated. Thanks in advance!
 
Back
Top