I found this answer that works perfectly for me...
[code]
Private Const SRCCOPY As Integer = &HCC0020
Private memImage As Bitmap
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
Dim currPage As Integer
Dim LastPage As Integer
Private WithEvents PrintDocument1 As New PrintDocument
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
BuildFormImage()
With PrintDialog1
.AllowPrintToFile = False
.AllowSelection = False
.AllowSomePages = False
.PrinterSettings = New PrinterSettings
With .PrinterSettings
.PrintRange = PrintRange.AllPages
LastPage = .Copies
End With
If .ShowDialog <> DialogResult.OK Then Exit Sub
If .PrinterSettings.PrintRange = PrintRange.AllPages Then
Dim FirstPage As Integer = 1
Dim LastPage As Integer = .PrinterSettings.Copies
PrintLabel(FirstPage, LastPage)
End If
End With
End Sub
Private Sub BuildFormImage()
'this procedure builds an image (snapshot) of the form
Dim graphicID As Graphics = pnlLabel.CreateGraphics
Dim sizeID As Size = pnlLabel.Size
memImage = New Bitmap(sizeID.Width, sizeID.Height, graphicID)
Dim memGraphic As Graphics = graphicID.FromImage(memImage)
Dim deviceContext1 As IntPtr = graphicID.GetHdc
Dim deviceContext2 As IntPtr = memGraphic.GetHdc
BitBlt(deviceContext2, 0, 0, pnlLabel.ClientRectangle.Width,
pnlLabel.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
graphicID.ReleaseHdc(deviceContext1)
memGraphic.ReleaseHdc(deviceContext2)
End Sub
Sub PrintLabel(ByVal FirstPage As Integer, ByVal LastPage As Integer)
Me.currPage = FirstPage
Me.LastPage = LastPage
Me.PrintDocument1 = New PrintDocument
Try
PrintDocument1.Print()
Catch ex As Exception
MessageBox.Show(ex.Message, "Print Error")
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'this procedure draws the image (snapshot) to the print document
Dim formGraphics As Graphics = e.Graphics
formGraphics.DrawImage(memImage, 0, 0)
currPage += 1
e.HasMorePages = (currPage <= LastPage)
End Sub
Thanks for your input.
DJ
"DJ Pomeroy" <dj*******@almegatf.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
I just can't seem to get it...
I have a form. On this form is a panel. In this panel is a bunch of
objects - graphics, text, and so on. I want to be able to print what's in
the panel to a printer. I'm not using Crystal Reports. Here's the code I
have so far...
- Dim CurrPage As Integer
- Dim LastPage As Integer
- Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
- System.EventArgs) Handles btnPrint.Click
- With PrintDialog1
- .AllowPrintToFile = False
- .AllowSelection = True
- .AllowSomePages = False
- .PrinterSettings = New PrinterSettings
- .PrinterSettings.PrintRange = PrintRange.Selection
- If .ShowDialog <> DialogResult.OK Then Exit Sub
- Dim LastPage As Integer = .PrinterSettings.Copies
- Dim FirstPage As Integer = 1
- PrintPages(FirstPage, LastPage)
- End With
- End Sub
- Sub PrintPages(ByVal FirstPage As Integer, ByVal LastPage As Integer)
- Me.CurrPage = FirstPage
- Me.LastPage = LastPage
- Me.PrintDocument2 = New PrintDocument
- Try
- PrintDocument2.Print()
- Catch ex As Exception
- MessageBox.Show(ex.Message, "Print error")
- End Try
- End Sub
- Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal
-
e
- As System.Drawing.Printing.PrintPageEventArgs) Handles
- PrintDocument2.PrintPage
- Me.LastPage = LastPage
- 'What to print needs to be entered here.
- CurrPage += 1
- e.HasMorePages = (CurrPage <= LastPage)
- End Sub
-
Basically, I don't know what to put in the "PrintPage" sub. Any ideas?
DJ Pomeroy