473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
3 1830
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 thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    2
    by: David Isaac | last post by:
    I'd like to try personal financial management using Python. I just found PyCheckbook, but it does not support check printing. Is there a Python check printing application kicking around? Thanks,...
    8
    by: David Isaac | last post by:
    "Alan Isaac" <aisaac0@verizon.net> wrote in message news:_A34e.2207$1r6.248@trnddc02... > I'd like to try personal financial management using Python. > I just found PyCheckbook, but it does not...
    9
    by: Jody Gelowitz | last post by:
    I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
    2
    by: gerb | last post by:
    Hello, I realise this is not a pure Javascript question, and that VBscript is probably involved at some point, though not as much as I fear. If you opened this item looking for a pute Javascript...
    0
    by: Martin K | last post by:
    Hello all..thanks for everyone's advice regarding my SHBrowseForFolder API problem, I was able to put together a public class that runs great! But I have another migration headache regarding the...
    3
    by: Mika M | last post by:
    Hi all! I have made an application for printing simple barcode labels using PrintDocument object, and it's working fine. Barcode printer that I use is attached to the computer, and this...
    6
    by: Chris Dunaway | last post by:
    The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
    10
    by: Mtek | last post by:
    Hi, I have a web page with some drop downs. Once the user makes his selections the bottom half of the page is populated. When that happens, I want to create a formatted report in a hidden div....
    18
    by: Brett | last post by:
    I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
    1
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    by: Faith0G | last post by:
    I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
    0
    by: ryjfgjl | last post by:
    In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.