467,877 Members | 1,021 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

Another Printing Question

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

Expand|Select|Wrap|Line Numbers
  1. Dim CurrPage As Integer
  2. Dim LastPage As Integer
  3. Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
  4. System.EventArgs) Handles btnPrint.Click
  5. With PrintDialog1
  6. .AllowPrintToFile = False
  7. .AllowSelection = True
  8. .AllowSomePages = False
  9. .PrinterSettings = New PrinterSettings
  10. .PrinterSettings.PrintRange = PrintRange.Selection
  11.  
  12. If .ShowDialog <> DialogResult.OK Then Exit Sub
  13.  
  14. Dim LastPage As Integer = .PrinterSettings.Copies
  15. Dim FirstPage As Integer = 1
  16.  
  17. PrintPages(FirstPage, LastPage)
  18.  
  19. End With
  20. End Sub
  21.  
  22. Sub PrintPages(ByVal FirstPage As Integer, ByVal LastPage As Integer)
  23. Me.CurrPage = FirstPage
  24. Me.LastPage = LastPage
  25. Me.PrintDocument2 = New PrintDocument
  26.  
  27. Try
  28. PrintDocument2.Print()
  29. Catch ex As Exception
  30. MessageBox.Show(ex.Message, "Print error")
  31. End Try
  32. End Sub
  33.  
  34. Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e
  35. As System.Drawing.Printing.PrintPageEventArgs) Handles
  36. PrintDocument2.PrintPage
  37. Me.LastPage = LastPage
  38. 'What to print needs to be entered here.
  39.  
  40.  
  41. CurrPage += 1
  42. e.HasMorePages = (CurrPage <= LastPage)
  43. End Sub
  44.  
  45.  
Basically, I don't know what to put in the "PrintPage" sub. Any ideas?

DJ Pomeroy
Nov 20 '05 #1
  • viewed: 1606
Share:
3 Replies
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" <Br*****@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...

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" wrote:
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...

Expand|Select|Wrap|Line Numbers
  1.  > Dim CurrPage As Integer
  2.  > Dim LastPage As Integer
  3.  > Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
  4.  > System.EventArgs) Handles btnPrint.Click
  5.  >     With PrintDialog1
  6.  >         .AllowPrintToFile = False
  7.  >         .AllowSelection = True
  8.  >         .AllowSomePages = False
  9.  >         .PrinterSettings = New PrinterSettings
  10.  >         .PrinterSettings.PrintRange = PrintRange.Selection
  11.  >
  12.  >         If .ShowDialog <> DialogResult.OK Then Exit Sub
  13.  >
  14.  >         Dim LastPage As Integer = .PrinterSettings.Copies
  15.  >         Dim FirstPage As Integer = 1
  16.  >
  17.  >         PrintPages(FirstPage, LastPage)
  18.  >
  19.  >     End With
  20.  > End Sub
  21.  >
  22.  > Sub PrintPages(ByVal FirstPage As Integer, ByVal LastPage As Integer)
  23.  >     Me.CurrPage = FirstPage
  24.  >     Me.LastPage = LastPage
  25.  >     Me.PrintDocument2 = New PrintDocument
  26.  >
  27.  >     Try
  28.  >         PrintDocument2.Print()
  29.  >     Catch ex As Exception
  30.  >         MessageBox.Show(ex.Message, "Print error")
  31.  >     End Try
  32.  > End Sub
  33.  >
  34.  > 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

    Nov 20 '05 #2
    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" <Br*****@discussions.microsoft.com> wrote in message
    news:43**********************************@microsof t.com...
    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" wrote:
    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...

    Expand|Select|Wrap|Line Numbers
    1.  > Dim CurrPage As Integer
    2.  > Dim LastPage As Integer
    3.  > Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
    4.  > System.EventArgs) Handles btnPrint.Click
    5.  >     With PrintDialog1
    6.  >         .AllowPrintToFile = False
    7.  >         .AllowSelection = True
    8.  >         .AllowSomePages = False
    9.  >         .PrinterSettings = New PrinterSettings
    10.  >         .PrinterSettings.PrintRange = PrintRange.Selection
    11.  >
    12.  >         If .ShowDialog <> DialogResult.OK Then Exit Sub
    13.  >
    14.  >         Dim LastPage As Integer = .PrinterSettings.Copies
    15.  >         Dim FirstPage As Integer = 1
    16.  >
    17.  >         PrintPages(FirstPage, LastPage)
    18.  >
    19.  >     End With
    20.  > End Sub
    21.  >
    22.  > Sub PrintPages(ByVal FirstPage As Integer, ByVal LastPage As Integer)
    23.  >     Me.CurrPage = FirstPage
    24.  >     Me.LastPage = LastPage
    25.  >     Me.PrintDocument2 = New PrintDocument
    26.  >
    27.  >     Try
    28.  >         PrintDocument2.Print()
    29.  >     Catch ex As Exception
    30.  >         MessageBox.Show(ex.Message, "Print error")
    31.  >     End Try
    32.  > End Sub
    33.  >
    34.  > 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

    Nov 20 '05 #3
    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...

    Expand|Select|Wrap|Line Numbers
    1.  Dim CurrPage As Integer
    2.  Dim LastPage As Integer
    3.  Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
    4.  System.EventArgs) Handles btnPrint.Click
    5.      With PrintDialog1
    6.          .AllowPrintToFile = False
    7.          .AllowSelection = True
    8.          .AllowSomePages = False
    9.          .PrinterSettings = New PrinterSettings
    10.          .PrinterSettings.PrintRange = PrintRange.Selection
    11.          If .ShowDialog <> DialogResult.OK Then Exit Sub
    12.          Dim LastPage As Integer = .PrinterSettings.Copies
    13.          Dim FirstPage As Integer = 1
    14.          PrintPages(FirstPage, LastPage)
    15.      End With
    16.  End Sub
    17.  Sub PrintPages(ByVal FirstPage As Integer, ByVal LastPage As Integer)
    18.      Me.CurrPage = FirstPage
    19.      Me.LastPage = LastPage
    20.      Me.PrintDocument2 = New PrintDocument
    21.      Try
    22.          PrintDocument2.Print()
    23.      Catch ex As Exception
    24.          MessageBox.Show(ex.Message, "Print error")
    25.      End Try
    26.  End Sub
    27.  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

    Nov 20 '05 #4

    This discussion thread is closed

    Replies have been disabled for this discussion.

    Similar topics

    2 posts views Thread by David Isaac | last post: by
    8 posts views Thread by David Isaac | last post: by
    9 posts views Thread by Jody Gelowitz | last post: by
    6 posts views Thread by Chris Dunaway | last post: by
    10 posts views Thread by Mtek | last post: by
    18 posts views Thread by Brett | last post: by
    reply views Thread by jack112 | last post: by
    By using this site, you agree to our Privacy Policy and Terms of Use.