Another Printing Question

D

DJ Pomeroy

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...

Code:
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
 
D

DJ Pomeroy

Thank you for the quick reply. What is the BitBlt supposed to be?

Also, the panel I'm trying to print is pnlLabel. Where is this placed in all
this?

BrianDH said:
Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = PannelSize
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

B

DJ Pomeroy said:
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...

Code:
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
 
J

Jay B. Harlow [MVP - Outlook]

Brain,
Be certain to Dispose of your Graphics objects when you are done with them.
memoryGraphics.ReleaseHdc(dc2) memoryGraphics.Dispose()
mygraphics.Dispose()
End Sub

I would consider wrapping the CaptureScreen routine in a Try/Finally to
ensure that the Dispose methods are called in case of failure...

Hope this helps
Jay

BrianDH said:
Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = PannelSize
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

B

DJ Pomeroy said:
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...

Code:
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
 
D

DJ Pomeroy

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




[QUOTE="DJ Pomeroy"]
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...

[code]
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
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top