Printing scrollable form

前端 未结 5 1867
面向向阳花
面向向阳花 2020-12-21 16:37

I have a scroll-able form which I would like to print entirely.

I have already tried using this code to print it:

    Private Sub btnPrint_Click(ByVa         


        
相关标签:
5条回答
  • 2020-12-21 16:53

    I had a similar problem I was able to solve without any additional libraries or extensions. It is simple with the DrawToBitmap method available on most Forms controls.

        Dim ctrlColl As ControlCollection
        Dim i As Integer = 0
    
        ' Get collection of controls
        ctrlColl = Me.Controls
    
        ' create bitmap array
        Dim Bitmaps(ctrlColl.Count - 1) As Bitmap
    
        ' remove controls you have hidden before printing
        For Each ctrl As Control In ctrlColl
            If Not ctrl.Visible Then
                ctrlColl.Remove(ctrl)
            End If
        Next
    
        ' Loop through controls
        For Each ctrl As Control In ctrlColl
            ' create bitmap from control.DrawToBitmap
            Bitmaps(i) = New Bitmap(ctrl.Width, ctrl.Height)
            ctrl.DrawToBitmap(Bitmaps(i), ctrl.ClientRectangle)
            i = i + 1
        Next
    
        ' Print each bitmap in array
        i = 0
        For Each bmp As Bitmap In Bitmaps
            e.Graphics.DrawImage(bmp, New Point(ctrlColl(i).Location.X, ctrlColl(i).Location.Y))
            i = i + 1
        Next
    
    End Sub
    
    0 讨论(0)
  • 2020-12-21 17:00

    In this line:

    PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    

    It looks like PrintOption.Scrollable is only going to work, if you have a scrollable form. You have a scrollable control here (probably a Panel), inside a form. In this case its area will not be expanded onto the printer. Compare:

    Scrollable control:

    enter image description here

    prints as:

    enter image description here

    Scrollable form:

    enter image description here

    prints as:

    enter image description here

    According to this official answer from Microsoft, capturing a scrollable control is not possible with PrintForm. If you use a PrintDocument and some custom coding, you can do it for simple cases, such as a scrollable TextBox. In your case you may need even more custom coding to be done. Handling PrintDocument1.PrintPage looks like the best place to start, if you are up to it.

    0 讨论(0)
  • 2020-12-21 17:01

    Check for the MSDN Forum of the code here the code like this

    1. In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.

    2. The PrintForm component will be added to the component tray.

    3. In the Properties window, set the PrintAction property to PrintToPrinter.

    Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).

    PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    

    and here got the same question as your and have been answer.

    0 讨论(0)
  • Based on what you're showing... it looks like the scrollable area is a container like a Panel. If that's the case, printing the form isn't the problem, it's printing the scrollable control.

    Have a look at this project to create a bitmap from a control: http://www.codeproject.com/Articles/35734/Print-a-WinForms-User-Control

    EDIT: On second thought, I don't think the code at that link addresses the actual scrolling problem either.

    I think you're going to need to do one of two things: 1) Temporarily resize the panel large enough that the scrollbars disappear then resize it back 2) Build a control (perhaps a "Printable Version" form) that doesn't have nested scrollable elements and gracefully deals with stuff like pagination.

    Option #2 might sound like a lot of work, but I think you could pretty quickly do something like: create a new panel, clone each control you want printed and add it to the panel (resizing as necessary to avoid scrolling), print the panel, then dispose of the panel.

    0 讨论(0)
  • 2020-12-21 17:16

    I Think your answer is in the article
    All you need is to reference to FormPrinting library (or import the source to your solution).

    Private Sub btnPrint_Click(object sender, EventArgs e)
            {
                var fp = new FormPrinting.FormPrinting(this);                
                fp.Print();
            }
    

    will do the printing job.
    I have tested the library, having no problems with scrollable contents like images and ....

    0 讨论(0)
提交回复
热议问题