Public Class Form22

    Private Sub Form22_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateD1()
        CreateD2()
    End Sub
    Private Sub CreateD1()
        Dim i As Integer
        d1.RowCount = NumericUpDown1.Value
        For i = 0 To d1.RowCount - 1
            d1.Rows(i).Cells(0).Value = i
            d1.Rows(i).Cells(1).Value = "电缆"
            d1.Rows(i).Cells(3).Value = "米"
        Next
        Dim myrnd As New Random()
        For i = 0 To d1.RowCount - 1
            d1.Rows(i).Cells(2).Value = 50 + i
            d1.Rows(i).Cells(4).Value = myrnd.Next(1, 99)
            d1.Rows(i).Cells(5).Value = myrnd.Next(10, 999)
            d1.Rows(i).Cells(6).Value = d1.Rows(i).Cells(4).Value * d1.Rows(i).Cells(5).Value
        Next

        d1.Rows(0).Cells(4).Value = 10
        d1.Rows(0).Cells(5).Value = 50
        d1.Rows(0).Cells(6).Value = 500

        d1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        d1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        d1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        d1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)

    End Sub
    Private Sub CreateD2()
        '构建表格1的内容
        Dim i As Integer
        Dim j As Integer

        '构建表格二的内容，表格一为上边的自动取数

        Dim mycol As DataGridViewColumn
        Dim myrow As DataGridViewRow
        Dim rows As Integer
        Dim cols As Integer

        cols = 2 '2
        rows = NumericUpDown2.Value
        d2.ColumnCount = cols
        d2.RowCount = rows
        '加一列按钮列
        For i = 0 To cols - 1
            mycol = d2.Columns(i)
            mycol.HeaderText = "第" + CStr(i + 1) + "列"
        Next
        Dim myco2 As New DataGridViewButtonColumn()
        myco2.HeaderText = "按钮列"
        myco2.Visible = True
        myco2.DefaultCellStyle.BackColor = Color.DarkGray
        d2.Columns.Add(myco2)

        d2.Rows(2).Cells(2).Style.ForeColor = Color.Blue
        d2.Rows(2).Cells(2).Style.Font = New Font("华文行楷", 12, FontStyle.Bold Or FontStyle.Strikeout Or FontStyle.Underline)
        cols = 3

        For i = 0 To rows - 1
            myrow = d2.Rows(i)
            For j = 0 To cols - 1
                myrow.Cells(j).Value = "第" + CStr(i + 1) + "行 第" + CStr(j + 1) + "列"
            Next

        Next
        d2.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        d2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        d2.Rows(0).Cells(0).Value = "表格2"

        Dim mycolumn As New DataGridViewCheckBoxColumn()

        'D2.Columns(2).CellTemplate
        Dim myco As New DataGridViewImageColumn()
        myco.HeaderText = "图片框"
        myco.Visible = True
        myco.ImageLayout = DataGridViewImageCellLayout.Zoom
        d2.Columns.Add(myco)
        d2.Rows(0).Cells(3).Value = My.Resources.mybmp
        d2.Rows(3).Cells(3).Value = My.Resources.myico


        mycolumn.HeaderText = "复选框"
        mycolumn.Visible = True
        mycolumn.TrueValue = "YES"
        mycolumn.FalseValue = "NO"
        d2.Columns.Add(mycolumn)
        d2.Rows(0).Cells(4).Value = "YES"
        d2.Rows(1).Cells(4).Value = "NO"
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        CreateD2()
    End Sub

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

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myp As New VBprinter.VB2008Print
        myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter
        If RadioButton2.Checked = True Then
            myp.PaperLandscape = True '横向打印
        Else
            myp.PaperLandscape = False '纵向打印
        End If
        myp.NewDoc()
        myp.NewPage()
        Dim myfont As New Font("黑体", 18, FontStyle.Bold)
        Dim mytxt As String
        Dim mywidth As Single
        Dim myspace As Single
        Dim tmpy As Single
        Dim tempyy As Single
        myspace = 30 '表格之间的间距
        mytxt = "用VB2008Print组件并排打印两个DataGridView测试"
        myp.DrawTitle(mytxt, New Font("黑体", 18, FontStyle.Bold), Color.Blue)
        mywidth = myp.PaperPrintWidth / 2 - myspace '表格打印宽度
        Dim curpage As Integer
        curpage = myp.GetCurrentPage '记录表格1开始打印所在的页码
        mytxt = "打印左边表格内容"
        tempyy = myp.Currenty '记录表格1开始打印的位置
        myp.PrintDGVFixedWidth(d1, mywidth, mytxt, myfont, StringAlignment.Center, "1111", True, False, 0, CheckBox1.Checked, True)
        Dim tmppage As Integer
        tmppage = myp.GetCurrentPage '记录表格1打印完后所处的页码
        tmpy = myp.Currenty '表格1打印完后当前的y位置
        myp.SetCurrentPage(curpage) '定位到表格1开始打印的那一页上打印表格2
        mytxt = "打印右边表格内容"
        myp.Currentx = mywidth + myspace '定位到表格1的右边（开始并排打印表格2的位置）
        myp.Currenty = tempyy '定位到表格1开始打印的y位置
        myp.PrintDGVFixedWidth(d2, mywidth, mytxt, myfont, StringAlignment.Center, "1111", True, False, 0, CheckBox1.Checked, True)
        If myp.GetCurrentPage() < tmppage Then
            '说明第1个表格打印的页数要多些
            myp.SetCurrentPage(tmppage)
            myp.Currenty = tmpy
        ElseIf myp.GetCurrentPage = tmppage Then
            '两个表格的页数一样多，看两个的Y位置更大些
            If myp.Currenty < tmpy Then
                myp.Currenty = tmpy
            End If
        End If
        myp.DrawText("打印完2个表格后接下去可以打印的内容，通过计算可以得到最后一页打印的位置，然后用SetCurrentPage与Currentx、Currenty定位后即可继续打印输出", New Font("宋体", 18))


        myp.EndDoc("并排打印2个DataGridView测试")
        myp.Dispose()
        myp = Nothing
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim myp As New VBprinter.VB2008Print
        If CheckBox2.Checked = True Then
            myp.IsDrawTableFooterEveryPage = True
        Else
            myp.IsDrawTableFooterEveryPage = False
        End If
        myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter
        If RadioButton2.Checked = True Then
            myp.PaperLandscape = True '横向打印
        Else
            myp.PaperLandscape = False '纵向打印
        End If
        myp.NewDoc()
        myp.NewPage()
        Dim myfont As New Font("黑体", 18, FontStyle.Bold)
        Dim subtitlefont As New Font("华文行楷", 15, FontStyle.Italic)
        Dim topfont As New Font("宋体", 12)
        Dim mytxt As String
        Dim mywidth As Single
        Dim myspace As Single
        Dim tmpy As Single
        Dim tempyy As Single
        myspace = 30 '表格之间的间距
        mytxt = "用VB2008Print组件并排打印两个DataGridView测试"
        myp.DrawTitle(mytxt, New Font("黑体", 18, FontStyle.Bold), Color.Blue)
        mywidth = myp.PaperPrintWidth / 2 - myspace '表格打印宽度
        Dim curpage As Integer
        curpage = myp.GetCurrentPage '记录表格1开始打印所在的页码
        mytxt = "打印左边表格内容"
        tempyy = myp.Currenty '记录表格1开始打印的位置
        myp.PrintDGVFixedWidth(d1, mywidth, mytxt, myfont, "子标题的内容1", subtitlefont, StringAlignment.Center, "1111", True, False, topfont, "表格页眉左边1", "表格页眉中间1", "表格页眉右边1", topfont, "表格页脚左边11", "表格页脚中间11", "表格页脚右边11", 0, CheckBox1.Checked, True)
        Dim tmppage As Integer
        tmppage = myp.GetCurrentPage '记录表格1打印完后所处的页码
        tmpy = myp.Currenty '表格1打印完后当前的y位置
        myp.SetCurrentPage(curpage) '定位到表格1开始打印的那一页上打印表格2
        mytxt = "打印右边表格内容"
        myp.Currentx = mywidth + myspace '定位到表格1的右边（开始并排打印表格2的位置）
        myp.Currenty = tempyy '定位到表格1开始打印的y位置
        myp.PrintDGVFixedWidth(d2, mywidth, mytxt, myfont, "子标题的内容2", subtitlefont, StringAlignment.Center, "1111", True, False, topfont, "表格页眉左边2", "表格页眉中间2", "表格页眉右边2", topfont, "表格页脚左边22", "表格页脚中间22", "表格页脚右边22", 0, CheckBox1.Checked, True)

        If myp.GetCurrentPage() < tmppage Then
            '说明第1个表格打印的页数要多些
            myp.SetCurrentPage(tmppage)
            myp.Currenty = tmpy
        ElseIf myp.GetCurrentPage = tmppage Then
            '两个表格的页数一样多，看两个的Y位置更大些
            If myp.Currenty < tmpy Then
                myp.Currenty = tmpy
            End If
        End If
        myp.DrawText("打印完2个表格后接下去可以打印的内容，通过计算可以得到最后一页打印的位置，然后用SetCurrentPage与Currentx、Currenty定位后即可继续打印输出", New Font("宋体", 18))


        myp.EndDoc("并排打印2个DataGridView测试")
        myp.Dispose()
        myp = Nothing
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim dgvcs1 As New VBprinter.DGVprint
        Dim dgvcs2 As New VBprinter.DGVprint
        dgvcs1.PrintType = VBprinter.DGVprint.mytype.GeneralPrint
        dgvcs1.Alignment = StringAlignment.Center
        dgvcs1.SubTitle = "子标题的内容1"
        dgvcs1.MainTitle = "打印左边表格内容"
        dgvcs1.TableFooterLeft = "表格页脚左边11"
        dgvcs1.TableFooterMiddle = "表格页脚中间11"
        dgvcs1.TableFooterRight = "表格页脚右边11"
        dgvcs1.TableHeaderLeft = "表格页眉左边1"
        dgvcs1.TableHeaderMiddle = "表格页眉中间1"
        dgvcs1.TableHeaderRight = "表格页眉右边1"
        dgvcs1.ZoomToPaperWidth = True
        dgvcs1.Border = "1111"
        dgvcs1.PrintTitlePerPage = CheckBox1.Checked
        dgvcs1.IsDrawTableFooterEveryPage = CheckBox2.Checked

        dgvcs2.PrintType = VBprinter.DGVprint.mytype.GeneralPrint
        dgvcs2.Alignment = StringAlignment.Center
        dgvcs2.SubTitle = "子标题的内容2"
        dgvcs2.MainTitle = "打印右边表格内容"
        dgvcs2.TableFooterLeft = "表格页脚左边22"
        dgvcs2.TableFooterMiddle = "表格页脚中间22"
        dgvcs2.TableFooterRight = "表格页脚右边22"
        dgvcs2.TableHeaderLeft = "表格页眉左边2"
        dgvcs2.TableHeaderMiddle = "表格页眉中间2"
        dgvcs2.TableHeaderRight = "表格页眉右边2"
        dgvcs2.ZoomToPaperWidth = True
        dgvcs2.Border = "1111"
        dgvcs2.PrintTitlePerPage = CheckBox1.Checked
        dgvcs2.IsDrawTableFooterEveryPage = CheckBox2.Checked

        Dim myp As New VBprinter.VB2008Print
        If RadioButton1.Checked = True Then
            myp.PaperLandscape = False
        Else
            myp.PaperLandscape = True
        End If

        myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter
        myp.NewDoc()
        myp.NewPage()
        Dim myspace As Single
        myspace = 30
        myp.PrintDGVEX("用VB2008Print组件并排打印两个DataGridView测试", New Font("黑体", 18, FontStyle.Bold), myspace, d1, dgvcs1, myp.PaperPrintWidth / 2, d2, dgvcs2, myp.PaperPrintWidth / 2 - myspace)
        myp.DrawText("打印完2个表格后接下去可以打印的内容，用PrintDGVEx函数打印完后，会自动将当前打印位置定位到接下去可打印的位置，无需额外用代码计算定位，方便简单。", New Font("宋体", 18))

        myp.EndDoc("并排打印2个DataGridView测试")
        myp.Dispose()
        myp = Nothing
        dgvcs1.Dispose()
        dgvcs1 = Nothing
        dgvcs2.Dispose()
        dgvcs2 = Nothing
    End Sub
End Class