473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1869
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*****@discus sions.microsoft .com> wrote in message
news:43******** *************** ***********@mic rosoft.com...

Dim memoryImage As Bitmap
Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphi cs()
Dim s As Size = PannelSize
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromIm age(memoryImage )
Dim dc1 As IntPtr = mygraphics.GetH dc
Dim dc2 As IntPtr = memoryGraphics. GetHdc
BitBlt(dc2, 0, 0, Me.ClientRectan gle.Width, Me.ClientRectan gle.Height, dc1, 0, 0, 13369376) mygraphics.Rele aseHdc(dc1)
memoryGraphics. ReleaseHdc(dc2)
End Sub
Private Sub PrintDocument1_ PrintPage(ByVal sender As System.Object,_ ByVal e As System.Drawing. Printing.PrintP ageEventArgs) Handles_
PrintDocument1. PrintPage e.Graphics.Draw Image(memoryIma ge, 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.Disp ose() 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*****@discus sions.microsoft .com> wrote in message
    news:43******** *************** ***********@mic rosoft.com...
    Dim memoryImage As Bitmap
    Private Sub CaptureScreen()
    Dim mygraphics As Graphics = Me.CreateGraphi cs()
    Dim s As Size = PannelSize
    memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
    Dim memoryGraphics As Graphics = Graphics.FromIm age(memoryImage )
    Dim dc1 As IntPtr = mygraphics.GetH dc
    Dim dc2 As IntPtr = memoryGraphics. GetHdc
    BitBlt(dc2, 0, 0, Me.ClientRectan gle.Width, Me.ClientRectan gle.Height, dc1, 0, 0, 13369376) mygraphics.Rele aseHdc(dc1)
    memoryGraphics. ReleaseHdc(dc2)
    End Sub
    Private Sub PrintDocument1_ PrintPage(ByVal sender As System.Object,_ ByVal e As System.Drawing. Printing.PrintP ageEventArgs) Handles_
    PrintDocument1. PrintPage e.Graphics.Draw Image(memoryIma ge, 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.EventArg s) Handles btnPrint.Click
    BuildFormImage( )
    With PrintDialog1
    .AllowPrintToFi le = False
    .AllowSelection = False
    .AllowSomePages = False
    .PrinterSetting s = New PrinterSettings
    With .PrinterSetting s
    .PrintRange = PrintRange.AllP ages
    LastPage = .Copies
    End With
    If .ShowDialog <> DialogResult.OK Then Exit Sub
    If .PrinterSetting s.PrintRange = PrintRange.AllP ages Then
    Dim FirstPage As Integer = 1
    Dim LastPage As Integer = .PrinterSetting s.Copies
    PrintLabel(Firs tPage, LastPage)
    End If
    End With
    End Sub

    Private Sub BuildFormImage( )
    'this procedure builds an image (snapshot) of the form
    Dim graphicID As Graphics = pnlLabel.Create Graphics
    Dim sizeID As Size = pnlLabel.Size
    memImage = New Bitmap(sizeID.W idth, sizeID.Height, graphicID)
    Dim memGraphic As Graphics = graphicID.FromI mage(memImage)
    Dim deviceContext1 As IntPtr = graphicID.GetHd c
    Dim deviceContext2 As IntPtr = memGraphic.GetH dc
    BitBlt(deviceCo ntext2, 0, 0, pnlLabel.Client Rectangle.Width ,
    pnlLabel.Client Rectangle.Heigh t, deviceContext1, 0, 0, SRCCOPY)
    graphicID.Relea seHdc(deviceCon text1)
    memGraphic.Rele aseHdc(deviceCo ntext2)
    End Sub

    Sub PrintLabel(ByVa l FirstPage As Integer, ByVal LastPage As Integer)
    Me.currPage = FirstPage
    Me.LastPage = LastPage
    Me.PrintDocumen t1 = 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.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
    'this procedure draws the image (snapshot) to the print document
    Dim formGraphics As Graphics = e.Graphics
    formGraphics.Dr awImage(memImag e, 0, 0)
    currPage += 1
    e.HasMorePages = (currPage <= LastPage)
    End Sub

    Thanks for your input.
    DJ


    "DJ Pomeroy" <dj*******@alme gatf.com> wrote in message
    news:Oa******** ******@TK2MSFTN GP12.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
    2060
    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, Alan Isaac
    8
    2428
    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 support check printing. > Is there a Python check printing application kicking around? OK, I'll assume silence means "no", so new question: What is the current best practice for cross platform printing of PostScript files from Python?
    9
    4118
    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 PrintDialog control under a security setting with only SafePrinting allowed. I have attached a sample project that I am using to try to accomplish this. The print dialog appears, but when I press the Print button, I get an exception (at the end...
    2
    12489
    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 question, I'm sorry. For an IE6 intranet application I'm running into some problems regarding printing screens from the browser. The specs dictate these requirements:
    0
    1421
    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 MSFlexGrid. I have been able to successfully add a COM object MSFlexGrid in my VB.Net project. I can successfully populate and manipulate the grid. But, I have two problems, that I used to be able to do with no issues in VB 6. 1) AutoSizing...
    3
    6320
    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 computer has drivers installed for this printer, and this printer is shared for the network. Question 1:
    6
    3245
    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 (much like the current model), but my PrintDocument would have a Pages collection such that each time you need to have an additional page, you would just add another page to the collection and then use the page object for the actual drawing etc. ...
    10
    1567
    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. When the user clicks the print button, I want to print only that hidden report...... Can anyone help me with this?
    18
    11316
    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 classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I move the page to the web server, I get the error "Settings to access printer...
    0
    9645
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
    0
    9480
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
    0
    9950
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
    1
    7500
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
    0
    6740
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
    0
    5381
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
    0
    5511
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    1
    4053
    by: 6302768590 | last post by:
    Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
    2
    3650
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.

    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.