Public Class Form10
    Private printtype As Integer
    Private Sub Form10_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '构建测试数据
        Button3_Click(Nothing, Nothing)

        dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        dgv1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
    End Sub

   

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        myp.InvalidatePreview()
    End Sub

    Private Sub myp_PrintDocument() Handles myp.PrintDocument
        Dim d1 As DateTime
        Dim d2 As DateTime
        d1 = Now
     
            '自定义方式补白打印
            myp.NewPage()
            Dim txtfont As New Font("宋体", 10) '表尾字体
            Dim isneednewpage As Boolean = True
            Dim mysize As SizeF
            Dim footertxt As String
            footertxt = Trim(dgvfooter.Text)
            mysize = myp.MeasureText(footertxt, txtfont, myp.PaperPrintWidth) '表尾要打印的大小
            Dim footerheight As Single
            footerheight = mysize.Height '表尾的高度
            Dim rowheight As Single = 0
        For i As Integer = 0 To dgv1.RowCount - 2 '合计行先不打，所以要减2
            If isneednewpage = True Then
                '换页打印表头
                PrintHeader()
                isneednewpage = False
            End If
            myp.DrawDGVRow(dgv1, i, CheckBox1.Checked, True, "1111", 6)
            If i <> dgv1.RowCount - 1 Then
                '非最后一行
                rowheight = myp.ConvFromDisplay(dgv1.Rows(i + 1).Height, False) '下一行的高度
                If myp.IsNewPage(rowheight + footerheight, False, False) = True Then
                    '需要换新页
                    '先打印表尾
                    PrintFooter(footertxt, txtfont, footerheight)
                    myp.NewPage()
                    isneednewpage = True
                End If
            End If
        Next

            '
        '添加空行并打印合计行
       
        Dim lastrowheight As Single '最后一行的高度，补白行均用此高度打印行,其实也是合计行的高度
            lastrowheight = myp.ConvFromDisplay(dgv1.Rows(dgv1.RowCount - 1).Height, False) '

            Dim tempheight As Single
            tempheight = myp.PaperPrintHeight - myp.Currenty - footerheight '剩余这么多可以打印补白
            Dim num As Integer
            num = Math.Floor(tempheight / lastrowheight) '可以补白这么多行
        For i As Integer = 1 To num - 1 '为什么减1呢，因为还有一行合计行要打印
            myp.DrawDGVRowForEmpty(dgv1, dgv1.RowCount - 1, CheckBox1.Checked, False, "1111", 0)
        Next
        '打印合计行
        myp.DrawDGVRow(dgv1, dgv1.RowCount - 1, CheckBox1.Checked, True, "1111", 6)
            '最后打印最后一页的表格尾部
            PrintFooter(footertxt, txtfont, footerheight)




        d2 = Now()


        '    MsgBox("预处理耗时" & DateDiff(DateInterval.Second, d1, d2) & "秒")
    End Sub
    ''' <summary>
    ''' 打印表头
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub PrintHeader()
        myp.Currentx = 0
        Dim txtfont As Font
        Dim mysize As SizeF
        If Trim(maintitle.Text) <> "" Then
            '主标题
            txtfont = New Font("黑体", 18, FontStyle.Bold)
            mysize = myp.MeasureText(maintitle.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(maintitle.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Center, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.6)
        End If
        If Trim(subtitle.Text) <> "" Then
            '小标题
            txtfont = New Font("华文行楷", 13)
            mysize = myp.MeasureText(subtitle.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(subtitle.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Center, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.3)
        End If
        If Trim(dgvheader.Text) <> "" Then
            '表头内容
            txtfont = New Font("宋体", 11)
            mysize = myp.MeasureText(dgvheader.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(dgvheader.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Near, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.2)
        End If
        '再打印表格的列标题
        myp.DrawDGVHeader(dgv1, CheckBox1.Checked, True, "1111", 6)

    End Sub
    ''' <summary>
    ''' 打印表格尾部
    ''' 
    ''' </summary>
    ''' <param name="txt" >表尾打印内容</param>
    ''' <param name="txtfont" >字体</param>
    ''' <param name="hh" >打印高度</param>
    ''' <remarks></remarks>
    Private Sub PrintFooter(ByVal txt As String, ByVal txtfont As Font, ByVal hh As Single)
        myp.DrawText(dgvfooter.Text, myp.PaperPrintWidth, hh, txtfont, Color.Blue, StringAlignment.Near, StringAlignment.Center, True, False, False, False, 0, 0)

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        myp.InvalidatePreview()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IsNumeric(tablerows.Text) = False Then
            Exit Sub
        End If
        Dim myrnd As New Random()

        Dim num As Integer
        num = CInt(tablerows.Text)
        If num <= 0 Then
            Exit Sub
        End If
        dgv1.RowCount = num
        For i As Integer = 0 To dgv1.RowCount - 1
            dgv1.Rows(i).Cells(0).Value = CStr(i + 1)
        Next
        For i = 0 To dgv1.RowCount - 1
            For j As Integer = 1 To dgv1.ColumnCount - 4
                dgv1.Rows(i).Cells(j).Value = "第" & CStr(i + 1) & "行第" & CStr(j) & "列"
            Next

            dgv1.Rows(i).Cells(4).Value = myrnd.Next(50)
            dgv1.Rows(i).Cells(4 + 1).Value = myrnd.Next(80)
            dgv1.Rows(i).Cells(4 + 2).Value = dgv1.Rows(i).Cells(4).Value * dgv1.Rows(i).Cells(4 + 1).Value

        Next
        '最后加一行合计数
        Dim je1 As Single = 0
        Dim je2 As Single = 0
        Dim je3 As Single = 0
        For i As Integer = 0 To dgv1.RowCount - 1
            je1 = je1 + dgv1.Rows(i).Cells(4).Value
            je2 = je2 + dgv1.Rows(i).Cells(5).Value
            je3 = je3 + dgv1.Rows(i).Cells(6).Value

        Next
        dgv1.RowCount = dgv1.RowCount + 1

        dgv1.Rows(dgv1.RowCount - 1).Cells(0).Value = "合计"
        dgv1.Rows(dgv1.RowCount - 1).Cells(0).Tag = "右合并"
        dgv1.Rows(dgv1.RowCount - 1).Cells(1).Value = "合计"
        dgv1.Rows(dgv1.RowCount - 1).Cells(1).Tag = "合并"
        dgv1.Rows(dgv1.RowCount - 1).Cells(2).Value = "合计"
        dgv1.Rows(dgv1.RowCount - 1).Cells(2).Tag = "合并"
        dgv1.Rows(dgv1.RowCount - 1).Cells(3).Value = "合计"
        dgv1.Rows(dgv1.RowCount - 1).Cells(3).Tag = "左合并"
        dgv1.Rows(dgv1.RowCount - 1).Cells(0).Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        dgv1.Rows(dgv1.RowCount - 1).Cells(4).Value = je1
        dgv1.Rows(dgv1.RowCount - 1).Cells(5).Value = je2
        dgv1.Rows(dgv1.RowCount - 1).Cells(6).Value = je3
    End Sub

    Private Sub myp_HeaderFooterOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles myp.HeaderFooterOut
        myp.PrintFooter("", "第" & CStr(curpage) & "页 共" & CStr(pages) & "页", CStr(Now()))
    End Sub



   

End Class