使用vb.net

时间:2019-08-22 23:08:41

标签: vb.net printing panel picturebox printdocument

您好,人们试图打印一个包含标签,文本框和图片框以及列表视图的可滚动面板,该页面可能超过两页。我已经尝试过并在网上寻求帮助,我可以打印标签和文本框,如何制作要打印的列表视图。

Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0

If page = 1 Then
    For Each ctrl As Control In Me.Panel1.Controls
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
            ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
            If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
        End If
    Next
End If

For Each ctrl As Control In Me.Panel1.Controls
    If CInt(ctrl.Tag) = page Then
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
            Dim sf As New System.Drawing.StringFormat
            If TypeOf ctrl Is TextBox Then
                If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            ElseIf TypeOf ctrl Is Label Then
                If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                    sf.Alignment = StringAlignment.Near
                ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                    sf.Alignment = StringAlignment.Far
                End If
            End If
            sf.FormatFlags = StringFormatFlags.NoClip
            e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
        ElseIf TypeOf ctrl Is PictureBox Then
            If Not DirectCast(ctrl, PictureBox).image  is nothing Then
                    e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
                End If
        End If
    End If
Next

page += 1
If page > maxPages Then
    e.HasMorePages = False
    page = 1
    maxPages = 0
Else
    e.HasMorePages = True
End If

1 个答案:

答案 0 :(得分:0)

由于面板上的列表视图不可滚动,我建议通过向现有代码中添加一些代码来将其捕获为图像。

 Static page As Integer = 1
    Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    Static maxPages As Integer = 0

    If page = 1 Then
        For Each ctrl As Control In Me.Panel1.Controls
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl is ListView Then
                ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
                If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
            End If
        Next
    End If

    For Each ctrl As Control In Me.Panel1.Controls
        If CInt(ctrl.Tag) = page Then
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
                Dim sf As New System.Drawing.StringFormat
                If TypeOf ctrl Is TextBox Then
                    If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                        sf.Alignment = StringAlignment.Far
                    Else
                        sf.Alignment = StringAlignment.Near
                    End If
                ElseIf TypeOf ctrl Is Label Then
                    If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                        sf.Alignment = StringAlignment.Near
                    ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                        sf.Alignment = StringAlignment.Far
                    End If
                End If
                sf.FormatFlags = StringFormatFlags.NoClip
                e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)

            ElseIf TypeOf ctrl Is ListView Then
                Dim lv As ListView = DirectCast(ctrl, ListView)
                Dim img As New Bitmap(lv.Width, lv.Height)
                lv.DrawToBitmap(img, New Rectangle(Point.Empty, img.Size))
                e.Graphics.DrawImage(img, New PointF(ctrl.Left, ctrl.Top - startPosition))
            End If
        End If
    Next

    page += 1
    If page > maxPages Then
        e.HasMorePages = False
        page = 1
        maxPages = 0
    Else
        e.HasMorePages = True
    End If