Public Class Form13
    Private Sub Form13_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '构建测试数据

        Me.客户TableAdapter.Fill(Me.NorthwindDataSet.客户)
        dgv1.DataSource = Me.NorthwindDataSet.客户
        dgv1.AutoResizeColumns()

        myp.InvalidatePreview()

    End Sub

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


    Private Sub myp_PrintDocument() Handles myp.PrintDocument
 
        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 - 1
            If isneednewpage = True Then
                '换页打印表头
                PrintHeader()
                isneednewpage = False
            End If
            myp.DrawDGVRow(dgv1, i, CheckBox1.Checked, True, "1111", 6) '打印DGV第i行的内容
            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


        '最后打印最后一页的表格尾部
        PrintFooter(footertxt, txtfont, footerheight)





    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 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