Public Class Form19
    Dim printtype As Integer
   
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.客户TableAdapter.Fill(Me.NorthwindDataSet.客户)
        d1.RowCount = Me.NorthwindDataSet.客户.Rows.Count
        d1.ColumnCount = 9
        For i As Integer = 0 To d1.RowCount - 1
            For j As Integer = 0 To 8
                d1.Rows(i).Cells(j).Value = Me.NorthwindDataSet.客户.Rows(i).Item(j)
            Next
        Next
        For j As Integer = 0 To d1.ColumnCount - 1
            d1.Columns(j).HeaderText = Me.NorthwindDataSet.客户.Columns(j).Caption
        Next

        MyCheck()


    End Sub



    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To d1.SelectedCells.Count - 1
            d1.SelectedCells(i).Style.Font = New Font("黑体", 18, FontStyle.Bold)
            d1.SelectedCells(i).Style.WrapMode = DataGridViewTriState.True
        Next

    End Sub

   

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TabControl1.SelectTab(1)
        myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter '百分之一厘米
        myp.PaperLandscape = True '横向打印
        myp.InvalidatePreview()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As New VBprinter.VB2008Print()
        p.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter
        p.NewDoc()
        p.NewPage(True)
        Dim dgv As New DataGridView()
        p.CopyDataGridView(d1, dgv)
        dgv.Columns(GetDGVindexByDisplayindex(dgv, 0)).Width = p.ConvToDisplay(w.Value * 10, True)
        dgv.ColumnHeadersHeight = p.ConvToDisplay(h.Value * 10, False)



        p.DrawTitle("斜线表头打印测试", New Font("黑体", 18, FontStyle.Bold), Color.Blue, 0)
        p.NewRow(p.LastDrawTextHeight / 2)
        Dim myfont As New Font("宋体", 10)
        Dim mycolor As Color = Color.Black
        Dim mypen As Pen = Pens.Black
        Dim myw As Single
        Dim myh As Single
        myw = w.Value * 10
        myh = h.Value * 10
        Dim myindex As Integer
        '第一列以斜线表头打印
        Dim rowheight As Single
        Dim mybrush As Brush = New SolidBrush(dgv.ColumnHeadersDefaultCellStyle.BackColor) '设置为一样的颜色
        For j As Integer = 0 To dgv.RowCount - 1
            rowheight = p.ConvFromDisplay(dgv.Rows(j).Height, False) '当前行高
            If j = 0 Or p.IsNewPage(rowheight, True) Then
                '需要打印表头
                If RadioButton1.Checked = True Then
                    p.DrawBiasCell1(r1.Text, c1.Text, myw, myh, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton2.Checked = True Then
                    p.DrawBiasCell2(r1.Text, d.Text, c1.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton3.Checked = True Then
                    p.DrawBiasCell3(r1.Text, r2.Text, c1.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton4.Checked = True Then
                    p.DrawBiasCell4(r1.Text, c1.Text, c2.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton5.Checked = True Then
                    p.DrawBiasCell5(r1.Text, r2.Text, c1.Text, c2.Text, myw, myh, myfont, myfont, myfont, myfont, mycolor, mypen, mybrush)
                End If

                '继续打印其他列标题，故从1开始
                For i As Integer = 1 To dgv.ColumnCount - 1
                    myindex = GetDGVindexByDisplayindex(dgv, i)
                    p.DrawDGVHeaderCell(dgv, i, p.ConvFromDisplay(dgv.Columns(myindex).Width, True), myh, True, "1111", 0)
                Next
                p.NewRow(myh)
            End If
            p.DrawDGVRow(dgv, j, "1111", 6)

        Next

        p.EndDoc("打印预览结果")
        p.Dispose()
        p = Nothing
    End Sub

    Private Sub myp_PrintDocument() Handles myp.PrintDocument
        myp.NewPage()
        myp.DrawTitle("斜线表头打印测试", New Font("黑体", 18, FontStyle.Bold), Color.Blue, 0)
        myp.NewRow(myp.LastDrawTextHeight / 2)
        Dim myfont As New Font("宋体", 10)
        Dim mycolor As Color = Color.Black
        Dim mypen As Pen = Pens.Black
        Dim myw As Single
        Dim myh As Single
        myw = w.Value * 10
        myh = h.Value * 10
        Dim myindex As Integer
        '第一列以斜线表头打印
        Dim rowheight As Single
        Dim mywidth As Single
        Dim mybrush As Brush = New SolidBrush(d1.ColumnHeadersDefaultCellStyle.BackColor) '设置为一样的颜色
        For j As Integer = 0 To d1.RowCount - 1
            rowheight = myp.ConvFromDisplay(d1.Rows(j).Height, False) '当前行高
            If j = 0 Or myp.IsNewPage(rowheight, True) Then
                '需要打印表头
                If RadioButton1.Checked = True Then
                    myp.DrawBiasCell1(r1.Text, c1.Text, myw, myh, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton2.Checked = True Then
                    myp.DrawBiasCell2(r1.Text, d.Text, c1.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton3.Checked = True Then
                    myp.DrawBiasCell3(r1.Text, r2.Text, c1.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton4.Checked = True Then
                    myp.DrawBiasCell4(r1.Text, c1.Text, c2.Text, myw, myh, myfont, myfont, myfont, mycolor, mypen, mybrush)
                ElseIf RadioButton5.Checked = True Then
                    myp.DrawBiasCell5(r1.Text, r2.Text, c1.Text, c2.Text, myw, myh, myfont, myfont, myfont, myfont, mycolor, mypen, mybrush)
                End If

                '继续打印其他列标题，故从1开始
                For i As Integer = 1 To d1.ColumnCount - 1
                    myindex = GetDGVindexByDisplayindex(d1, i)
                    myp.DrawDGVHeaderCell(d1, i, myp.ConvFromDisplay(d1.Columns(myindex).Width, True), myh, True, "1111", 0)
                Next
                myp.NewRow(myh)
            End If

            For i As Integer = 0 To d1.ColumnCount - 1
                myindex = GetDGVindexByDisplayindex(d1, i)
                If i = 0 Then
                    mywidth = myw '第一列是斜线表头，故宽茺是斜线表头的宽度
                Else
                    mywidth = myp.ConvFromDisplay(d1.Columns(myindex).Width, True)
                End If
                myp.DrawDGVContentCell(d1, j, i, mywidth, rowheight, True, "1111", 6)
            Next
            myp.Currenty = myp.Currenty + rowheight
            myp.Currentx = 0
        Next
       


    End Sub
    ''' <summary>
    ''' 根据显示的displayindex得到其列的index
    ''' </summary>
    ''' <param name="dgv"></param>
    ''' <param name="displayindex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetDGVindexByDisplayindex(ByVal dgv As DataGridView, ByVal displayindex As Integer) As Integer
        For i As Integer = 0 To dgv.ColumnCount - 1
            If dgv.Columns(i).DisplayIndex = displayindex Then
                Return i
                Exit Function
            End If
        Next
        Return -1
    End Function

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton5.CheckedChanged, RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged
        MyCheck()
    End Sub
    Private Sub MyCheck()
        r1.Enabled = False
        r2.Enabled = False
        c1.Enabled = False
        c2.Enabled = False
        d.Enabled = False
        If RadioButton1.Checked = True Then
            r1.Enabled = True
            c1.Enabled = True
        ElseIf RadioButton2.Checked = True Then
            r1.Enabled = True
            c1.Enabled = True
            d.Enabled = True
        ElseIf RadioButton3.Checked = True Then
            r1.Enabled = True
            r2.Enabled = True
            c1.Enabled = True
        ElseIf RadioButton4.Checked = True Then
            r1.Enabled = True
            c1.Enabled = True
            c2.Enabled = True
        ElseIf RadioButton5.Checked = True Then
            r1.Enabled = True
            r2.Enabled = True
            c1.Enabled = True
            c2.Enabled = True
        End If
    End Sub
End Class