
Public Class Form2
    Dim printtype As Integer
    Private Sub UserControl11_Printdocument() Handles myp.PrintDocument

        '注意,打印DataGridView表格时,一般情况下您应当使用控件的以下两个方法先调整好列宽与行高,本控件的自动调整功能是有条件限制的(只有指定的某些参数才会自动调整)
        'D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        'D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
        Dim mymargin As New System.Drawing.Printing.Margins(100, 200, 200, 300)
        myp.NewPage() '注意,在调用本控件打印输出方法之前,必须调用NewPage方法(也可以用带参数的)来创建第1个页面
        Dim myformat As StringFormat
        'myformat = StringFormat.GenericTypographic '
        myformat = New StringFormat(StringFormat.GenericTypographic)
        '注意,如果用户自己创建StringFormat必须使用此格式(也绝不能使用前边那个myformat = StringFormat.GenericTypographic格式),否则输出文本时会有些差异,原因请参见VB2008帮助文件中关于此的解释
        Select Case printtype
            Case 100
                '普通方式输了表格1，适合纸张宽度 

                myp.PrintDGV(D1, "打印DataGridView1的内容,缩小到纸张宽度,每页都会打印标题行的,打印背景色(注意标题文字很特殊哦,是空心文字,当然您也可以用普通文字输出)", New Font("黑体", 20), "子标题(用阴影特效的文字)", New Font("华文行楷", 17), StringAlignment.Center, "1111", True, True, New Font("华文行楷", 12), "这是左边", "这是中间", "这是右边", New Font("楷体_GB2312", 10), "下左", "下中", "下右", 5, True, True, 1, 2)
            Case 1

                '打印文本图片及单元格测试
                myformat.Alignment = StringAlignment.Center  '居左
                myp.DrawText("以下为以文本形式输出文字，会自动换页", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.DrawText(TextBox1.Text, myp.PaperPrintWidth) '输出文本
                '以单元格形式输出
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.DrawText("以下为以单元格形式输出文字，会自动换页", myp.PaperPrintWidth, New Font("宋体", 14), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight * 2) '换新行
                myp.DrawCell(TextBox1.Text, myp.PaperPrintWidth, "1111", Color.Black)
                '以下输出图片
                myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, True) '新页
                myformat.Alignment = StringAlignment.Center
                myp.DrawText("以下不管页面设置如何,总是用A4纸张横向打印输出", myp.PaperPrintWidth, New Font("宋体", 44), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myformat.Alignment = StringAlignment.Near  '居左
                myp.DrawText(TextBox1.Text, myp.PaperPrintWidth) '输出文本
                myp.NewPage(False) '总是用纵向纸张
                myp.DrawText("本页不管页面设置如何,总是用纵向打印输出(但纸张会根据页面设置而变化", myp.PaperPrintWidth, New Font("宋体", 44), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight)
                myp.DrawImage(PictureBox1.Image) ', 0, myp.LastDrawTextHeight, myp.PaperPrintWidth, myp.PaperPrintHeight - myp.LastDrawTextHeight)
                myp.NewPage() '这是用默认的纸张了，而不是上面的纵向，也即，如果默认是横向，则也是横向输出，如果默认是纵向，则纵向输出
                myp.DrawImage(PictureBox2.Image, New Rectangle(0, 0, myp.PaperWidth / 2, myp.PaperHeight / 2))
                myp.NewPage() '新页
                myp.DrawText("以下输出填充矩形", myp.PaperPrintWidth)
                myp.NewRow(myp.LastDrawTextHeight)
                myp.DrawFillRect(New RectangleF(myp.Currentx, myp.Currenty, 200, 300), Color.Blue)
                myp.NewRow(300)
                myp.DrawFillRect(New RectangleF(myp.Currentx, myp.Currenty, 500, 400), Color.Red)
                myp.NewRow(400)
                Dim myconv As New ColorConverter()
                myp.DrawFillRect(New RectangleF(myp.Currentx, myp.Currenty, 500, 400), Color.Blue)
                myp.NewRow(400)
                '输出弧线
                myp.DrawArc(0, myp.Currenty, 400, 200, Color.Red, 2)
            Case 2
                '单元格测
                myp.DrawLine(0, 60, 100, 250, System.Drawing.Color.Red, 3)
                myp.DrawLine(50, 200, 400, 300, Color.Blue, 10)
                myp.Currenty = 450
                myformat.Alignment = StringAlignment.Near
                myformat.LineAlignment = StringAlignment.Center
                myp.DrawCell("第一行的第一个单元格", 250, 480, "1111", Color.Black)
                myp.DrawCell("第一行的第二个单元格", 400, 480, "1111", Color.Black)
                myp.DrawCell("单元格居中对齐", 160, 480, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center)
                myp.DrawCell("单元格居中但垂直靠上对齐", 230, 480, "1111", Color.Black, StringAlignment.Center, StringAlignment.Near)
                myp.NewRow(myp.LastDrawTextHeight)
                     myp.DrawCell("第二行的第一个单元格", 400, 480, "1111", Color.Black)
                myp.DrawCell("第二行的第二个单元格", 400, 480, "1111", Color.Black)
                  

                myp.NewRow(myp.LastDrawTextHeight)
                myp.DrawTitle("以上DrawCell函数使用时将控件的CellMargin全设置为0，所以文字离单元格的边距近，如果要设置文字与边框线之间的距离，请改变CellMargin属性", New Font("黑体", 18), Color.Blue, 0)


                myformat.Alignment = StringAlignment.Center '水平居中
                myformat.LineAlignment = StringAlignment.Center '垂直居中
                myp.DrawMultiCell("多个单元格测试", 8, True, True, 800, 180, Me.Font, Color.Blue, myformat, "1111", Color.Black, 6)
                myp.NewRow(myp.LastDrawTextHeight)
                myp.DrawMultiCell("多个单元格测试", 8, True, False, 800, 180, Me.Font, Color.Blue, myformat, "1111", Color.Black, 6)
                myp.NewPage()
                myp.DrawMultiCell("多个单元格测试", 8, False, True, 250, 800, Me.Font, Color.Blue, myformat, "1111", Color.Black, 6)
                myp.DrawMultiCell("多个单元格测试", 8, False, False, 250, 800, Me.Font, Color.Blue, myformat, "1111", Color.Black, 6)
                myp.DrawFillRect(New RectangleF(100, 900, 300, 500), Color.Blue)
                myp.Currenty = 800
            Case 3
                '完全按d2内容输出
                'D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                'D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGV(D2, "完全按DataGridView2的内容输出,不自动适合纸张宽度,每页都会打印标题行的", New Font("黑体", 20), "", New Font("黑体", 10), StringAlignment.Center, "1111", False, False, New Font("华文行楷", 12), "这是左边", "这是中间", "这是右边", New Font("楷体_GB2312", 10), "下左", "下中", "下右", 5, True, True, 0, 0, False)
            Case 4
                '完全按d2内容输出,且自动适应纸张宽度
                'D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                'D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGV(D2, "完全按DataGridView2的内容输出,且自动适应纸张宽度,只有首页打印标题行", New Font("黑体", 16), "这是子标题", New Font("华文行楷", 12), StringAlignment.Center, "1111", True, True, New Font("宋体", 10), "上左", "上中", "上右", New Font("隶书", 10), "下左", "", "下右", 5, False, True)
            Case 5
                '完全按d2内容输出,另一种表格线,且自动适应纸张宽度
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGV(D2, "完全按DataGridView2的内容输出,另一种表格线,且自动适应纸张宽度", New Font("黑体", 16), "这是子标题", New Font("华文行楷", 12), StringAlignment.Center, "0101", True, True, New Font("宋体", 10), "上左", "上中", "上右", New Font("隶书", 10), "下左", "", "下右", 5, False)
            Case 6
                '完全按d2内容输出,多栏输出,指定栏间距
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                'myp.PrintDGVColS(D2, 3, "2多栏输出DataGridView控件,指定栏宽", New Font("黑体", 20), "子标题", New Font("宋体", 13), 0, 50, "1111", 7, True, False, True)
                myp.PrintDGVColS(D2, 2, "2多栏输出DataGridView控件,指定栏宽", New Font("黑体", 20), "子标题(很好看的字哦)", New Font("宋体", 18, FontStyle.Bold), New Font("华文行楷", 10), "上左", "上中", "上右", New Font("华文新魏", 10), "下左", "下中", "下右", 0, 50, "1111", 7, True, False, True, True, 1, 6)
            Case 7
                '完全按d2内容输出,多栏输出,指定栏宽
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVColS(D2, 2, "2多栏输出DataGridView控件,指定栏间距,且每页都打印标题,最后一页先打印满左边的内容", New Font("黑体", 20), myp.PaperPrintWidth / 2 - 20, 0, "1111", 5, True)
            Case 8
                '完全按d1内容输出,横向多页输出
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVPages(D1, 1, "完全按DataGridView2的内容输出,横向多页输出,左边第一列做为固定列每页都重复,并且每页会自动缩放适合页宽", New Font("黑体", 16), StringAlignment.Center, "1111", True, 5, False, True)
            Case 9
                '完全按d1内容输出,横向多页输出
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVPages(D1, 0, "完全按DataGridView2的内容输出,横向多页输出,没有固定列,并且每页不会自动缩放适合页宽,且每页都打印标题列", New Font("黑体", 16), "子标题", New Font("宋体", 12), StringAlignment.Center, "1111", False, New Font("宋体", 10), "上左", "上中", "上右", Me.Font, "下左", "下中", "下右", 5, True)
            Case 10
                '工资条形式输出
                D1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVgzt(D2, "以工资条形式打印DataGridView1表格的内容(不打印单元格背景颜色)", New Font("黑体", 15), "小标题", New Font("华文行楷", 13), StringAlignment.Near, "1111", False, False, 0, False, 50, False, False)
            Case 11
                '工资条形式输出
                D1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVgzt(D2, "以工资条形式打印DataGridView1表格的内容(要打印单元格背景颜色)", New Font("黑体", 15), "小标题", New Font("华文行楷", 13), StringAlignment.Near, "1111", False, False, 0, False, 50, False, True)
            Case 12
                '多栏输出,最后一页先打印满上边的内容
                '完全按d2内容输出,多栏输出,指定栏宽
                D2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGVColS(D2, 2, "2多栏输出DataGridView控件,指定栏宽,且每页都打印标题,最后一页先打印满上边的内容", New Font("黑体", 20), myp.PaperPrintWidth / 2 - 20, 0, "1111", 5, True, True, True)
                'myp.PrintDGVColS(D2, 2, "主标题", New Font("黑体", 18), "", Nothing, New Font("宋体", 12), "左", "右", "", New Font("黑体", 18), "下左", "下中", "", 0, 10, "1111", 6, True, True)
            Case 13
                '文本,表格混合输出
                'myformat.Alignment = StringAlignment.Near  '居左
                'myp.DrawText("由于本页的NewPage中未带参数,故输出页面版式随页设置的改变而会改变", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
                'myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.TextFont = New Font("黑体", 12, FontStyle.Bold)

                myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本

                myp.NewRow(myp.LastDrawTextHeight) '换新行
                D1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                D1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
                myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5)
                myp.DrawText("这是表格下面可以输出的内容")

                myp.NewPage(True)
                myp.DrawText("由于本页的NewPage中带有参数,故输出页面版式随不管如何进行页面设置,总是按横向打印,纸张会随页面设置而改变", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5)
                myp.DrawText("这是表格下面可以输出的内容")

                myp.NewPage(System.Drawing.Printing.PaperKind.A3, False) '新页
                myp.DrawText("由于本页的NewPage中带有参数,故输出页面版式随不管如何进行页面设置,总是按A3纸(如果您的打印机不支持A3纸,则还是A4纸)纵向打印,只有页边距会随页面设置而改变", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5, True, True)


                myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, False) '新页
                myp.DrawText("由于本页的NewPage中带有参数,故输出页面版式随不管如何进行页面设置,总是按A4纸纵向打印,页边距也是固定了的", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
                myp.NewRow(myp.LastDrawTextHeight) '换新行
                myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "0101", True, True, 4)
            Case 14
                '文本输出测试
                Dim mystring As String
                mystring = "这是第一次输出的文本(单行)。"
                myp.DrawText(mystring, myp.MeasureText(mystring).Width)
                mystring = "这是第2次输出的文本,紧接着第一次的后面。"
                myp.DrawText(mystring) '注意，不要用后面这个，后面这个指定了宽度，在这个宽度范围内就不会自动换行了的，即便超过右边距了，除非您能确定从当前位置开始再加上指定宽度没有超过页面右边距 myp.DrawText(mystring, myp.MeasureText(mystring).Width)
                myp.NewRow(myp.MeasureText(mystring).Height)
                mystring = "这是第一次输出的文本，这次输出的文本是超过一行才能输出的，需要多行才能输出（多行）。"
                myp.DrawText(mystring, myp.PaperPrintWidth)
                myp.NewRow(myp.LastDrawTextHeight) '用LastDrawTextHeight得到上一行输出的高度
                mystring = "这是第2次输出的文本,紧接着第一次的后面另起一行"
                myp.DrawText(mystring, myp.MeasureText(mystring).Width)
                myp.NewRow(myp.LastDrawTextHeight) '用LastDrawTextHeight得到上一行输出的高度
                myp.DrawText("这是测试的用于测试", New Font("黑体", 30, FontStyle.Bold), Color.Red, 0)
                'myp.CellMargin = New System.Drawing.Printing.Margins(30, 40, 50, 60)
                'myp.DrawCellImage(PictureBox2.Image, 0, 0, myp.PaperPrintWidth, myp.PaperPrintHeight, "9999")
            Case 200
                '特殊文本样式测试
                myp.DrawText("普通文字输出", New Font("楷体_GB2312", 30), 0)
                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0
                myp.DrawText("空心文字输出", New Font("楷体_GB2312", 30), 1)
                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0

                myp.DrawText("阴影文字输出", New Font("楷体_GB2312", 60), 2)

                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0

                myp.DrawText("空心阴影文字输出", New Font("楷体_GB2312", 60), 3)
                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0
                myp.DrawText("浮雕文字输出", New Font("楷体_GB2312", 60), 4)

                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0
                myp.DrawText("另一种浮雕文字输出", New Font("楷体_GB2312", 60), 5)

                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0
                myp.DrawText("块文字输出", New Font("楷体_GB2312", 60), Color.Red, 6)

                myp.Currenty = myp.LastDrawTextHeight + myp.Currenty
                myp.Currentx = 0
            Case 300
                '使用方案文件打印dataGridView表格，要确保方案文件存在且与该表格能搭配，否则会有问题的
                '方案文件是文本文件，您可以随便修改，
                '比较好的办法就是，你可以先用本控件的DGVPrint组件生成该datagridView的打印方案文件，然后再进行修改
                Dim myfile As String
                myp.Currentx = 0
                myfile = My.Computer.FileSystem.GetParentPath(Application.ExecutablePath)
                If Microsoft.VisualBasic.Right(myfile, 1) <> "\" Then
                    myfile = myfile + "\"
                End If
                myfile = myfile + "第二个打印方案.fa"
                myp.PrintDGVFa(D1, myfile)
            Case 400
                '使用方案文件打印dataGridView表格，要确保方案文件存在且与该表格能搭配，否则会有问题的
                '方案文件是文本文件，您可以随便修改，
                '比较好的办法就是，你可以先用本控件的DGVPrint组件生成该datagridView的打印方案文件，然后再进行修改
                Dim myfile As String
                myp.DrawText(TextBox1.Text)
                myp.Currentx = 0
                myp.Currenty = myp.Currenty + myp.LastDrawTextHeight '定位到文字输出最再方，以便输出表格
                myfile = My.Computer.FileSystem.GetParentPath(Application.ExecutablePath)
                If Microsoft.VisualBasic.Right(myfile, 1) <> "\" Then
                    myfile = myfile + "\"
                End If
                myfile = myfile + "第二个打印方案.fa"
                myp.PrintDGVFa(D1, myfile, False)
            Case 500
                '使用方案文件打印dataGridView表格，要确保方案文件存在且与该表格能搭配，否则会有问题的
                '方案文件是文本文件，您可以随便修改，
                '比较好的办法就是，你可以先用本控件的DGVPrint组件生成该datagridView的打印方案文件，然后再进行修改
                '与上一个的区别在于，文字后面输出的表格是打印在另一页上的，而且表格所使用的面页设置是从方案文件中取数的
                '请参考帮助文件中的说明
                Dim myfile As String
                myp.DrawText(TextBox1.Text)
                myp.Currentx = 0
                myp.Currenty = myp.Currenty + myp.LastDrawTextHeight '定位到文字输出最再方，以便输出表格
                myfile = My.Computer.FileSystem.GetParentPath(Application.ExecutablePath)
                If Microsoft.VisualBasic.Right(myfile, 1) <> "\" Then
                    myfile = myfile + "\"
                End If
                myfile = myfile + "第三个打印方案.fa"
                myp.PrintDGVFa(D1, myfile, False)
            Case 2000
                '分组汇总加行号综合测试
                '本3.5版VB2008Print控件提供了直接达到此目的的功能
                '请注意，使用分组汇总时，所传递的列均为列的名称name而不是显示名称headertext
                myp.PrintDGV(D1, "分组汇总加行号打印D1测试(按联系人姓名列分组），每组换新页打印，加行号，且每组行号从1开始重新编号，并且汇总邮政编码列", New Font("黑体", 18), "子标题", New Font("宋体", 12), StringAlignment.Center, "1111", True, True, Nothing, "", "", "", Nothing, New Font("黑体", 14, FontStyle.Bold), Color.Red, Color.White, "邮政编码", True, True, "联系人姓名", System.ComponentModel.ListSortDirection.Ascending, "联系人姓名", True, "", "", "", 5, True, True, True, 0, 0)

            Case 2001
                '分组汇总加行号不换新页打印
                myp.PrintDGV(D1, "分组汇总加行号打印D1测试（按联系人姓名列分组），每组连续打印，加行号，要求每组行号连续编号，并且汇总邮政编码列", New Font("黑体", 18), "子标题", New Font("宋体", 12), StringAlignment.Center, "1111", True, True, Nothing, "", "", "", Nothing, New Font("黑体", 14, FontStyle.Bold), Color.Red, Color.White, "邮政编码", True, False, "联系人姓名", System.ComponentModel.ListSortDirection.Ascending, "联系人姓名", False, "", "", "", 5, True, True, True, 0, 0)
            Case 2002
                '分组加行号不换新页不汇总打印
                myp.PrintDGV(D1, "分组加行号打印D1测试（不汇总，按联系人姓名列分组），每组连续打印，加行号，要求每组行号从1开始重新编号", New Font("黑体", 18), "子标题", New Font("宋体", 12), StringAlignment.Center, "1111", True, True, Nothing, "", "", "", Nothing, New Font("黑体", 14, FontStyle.Bold), Color.Red, Color.White, "", True, True, "联系人姓名", System.ComponentModel.ListSortDirection.Ascending, "联系人姓名", False, "", "", "", 5, True, True, True, 0, 0)
            Case 2003
                '普通的加行号打印
                myp.PrintDGV(D1, "普通的加行号打印", New Font("黑体", 18), "子标题", New Font("宋体", 12), StringAlignment.Center, "1111", True, True, Nothing, "", "", "", Nothing, New Font("黑体", 14, FontStyle.Bold), Color.Red, Color.White, "", True, True, "", System.ComponentModel.ListSortDirection.Ascending, "", False, "", "", "", 5, True, True, True, 0, 0)
            Case 3000
                '工资条形式的分组打印
                myp.PrintDGVgzt(D1, "工资条形式的分组打印(按联系人姓名分组),加行号,每组行号从1开始重新编号", New Font("黑体", 14), "子标题", New Font("宋体", 12), Nothing, Nothing, Nothing, "", True, True, "联系人姓名", System.ComponentModel.ListSortDirection.Ascending, "联系人姓名", True, StringAlignment.Center, "1111", False, True, 5, True, 50, False, True, True)
        End Select
        'myp.NewRow(myp.LastDrawTextHeight)
        myp.Currentx = 1
        Dim mys As String
        mys = "这是表格紧接着下面可以输出的内容"
        myp.DrawText(mys, New Font("宋体", 12), Color.Red, myformat)
        myp.DrawText("新的内容2", New Font("宋体", 12), Color.Red, myformat)
        myp.CellMargin = New Printing.Margins(0, 0, 0, 0)
        myp.NewRow(myp.LastDrawTextHeight)
        myp.DrawCell(mys, myp.MeasureCell(mys).Width, "1111", Color.Black)
        myp.NewRow(myp.LastDrawTextHeight)

        myp.DrawCell(mys + "结果", myp.MeasureCell(mys).Width / 2, myp.MeasureCell(mys).Height * 1.5, "1111", Color.Black, StringAlignment.Near, StringAlignment.Center, True, True, False, False, 5)
        myp.NewRow(myp.LastDrawTextHeight)
        'myp.DrawCell(mys, myp.TextFont, Color.Red, myformat, "1111")
        Dim myfont As New Font("方正楷体", 10)
        Dim myss As SizeF
        myss = myp.MeasureCell(mys, myfont, myformat)
        myformat.Alignment = StringAlignment.Center
        'myp.DrawCell("这是表格紧接着下面可以输出的内容", New Rectangle(0, myp.Currenty, mys.Width - 10, mys.Height), myfont, Color.Blue, myformat, "1111")
        myp.DrawCell(mys + "真的吗?是真正的", myss.Width, myfont, "1111", Color.Beige)
        'myp.IsImmediatePrintNotPreview = True '要求不要显示预览,处理完后马上打印出来
    End Sub
    Private Sub UserControl11_HeaderOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles myp.HeaderFooterOut
        '可在此事件中调用PrintHeader、PrintFooter、PrintLeft、PrintRight分别在页面的上、下、左、右边距处输出内容
        '以上这些函数都有输出文本与图形的重函数，您可以同时使用，即既输出文本、又输出图像（注意必须先输出图像再输出文本，否则文本会被图像覆盖的）
        'myp.PrintHeader("页眉左边的内容", "页眉中间的内容", "湖南省株洲市", New Font("华文行楷", 12), Color.Green)

        myp.PrintHeader(PictureBox3.Image, Nothing, PictureBox1.Image)
        myp.PrintHeader("联系邮箱：myyouping@139.com", "", "中国航空动力机械研究所", New Font("隶书", 10), Color.Blue)


        myp.PrintFooter(PictureBox3.Image, PictureBox2.Image, Nothing)
        myp.PrintFooter("", "第" + CStr(curpage) + "页共" + CStr(pages) + "页", "打印时间：" + Now.ToString, New Font("华文行楷", 12), Color.Red)


        'myp.PrintLeft(PictureBox3.Image, Nothing, Nothing)
        'myp.PrintLeft("", "游平", "打印时间：" + Now.ToString, New Font("隶书", 14), Color.Blue, 1)


        myp.PrintRight("", "第" + CStr(curpage) + "页", "", New Font("隶书", 18), Color.Red, 6)

    End Sub


  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TreeView1.ExpandAll()
        TreeView1.SelectedNode = TreeView1.Nodes(0)
        Me.客户TableAdapter.Fill(Me.NorthwindDataSet.客户)
        D1.DataSource = Me.NorthwindDataSet.客户

        '构建表格二的内容，表格一为上边的自动取数
        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 = 105
        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 安全按DataGridView控件内容输出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 安全按DataGridView控件内容输出ToolStripMenuItem.Click
        printtype = 3
        myp.InvalidatePreview()
    End Sub
    Private Sub 完全按DataGridView控件内容输出二ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 完全按DataGridView控件内容输出二ToolStripMenuItem.Click
        printtype = 4
        myp.InvalidatePreview()

    End Sub
    Private Sub DataGridView控件内容输出方式三ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView控件内容输出方式三ToolStripMenuItem.Click
        printtype = 5
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridView控件内容输出方式四ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView控件内容输出方式四ToolStripMenuItem.Click
        printtype = 6
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridView控件内容输出方式四多栏输出二ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        printtype = 7
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridView控件内容输出方式五ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView控件内容输出方式五ToolStripMenuItem.Click
        printtype = 8
        myp.InvalidatePreview()

    End Sub
    Private Sub DataGridView控件内容输出方式五横向多页输出二ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView控件内容输出方式五横向多页输出二ToolStripMenuItem.Click
        printtype = 9
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridVew控件内容工资条形式打印ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridVew控件内容工资条形式打印ToolStripMenuItem.Click
        printtype = 10
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridVew控件内容工资条形式打印要打印单元格背景颜色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridVew控件内容工资条形式打印要打印单元格背景颜色ToolStripMenuItem.Click
        printtype = 11
        myp.InvalidatePreview()
    End Sub
    Private Sub DataGridView控件内容输出方式四多栏输出二ToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView控件内容输出方式四多栏输出二ToolStripMenuItem.Click
        printtype = 12
        myp.InvalidatePreview()
    End Sub
    Private Sub myp_QueryDefaultPageSettings(ByRef mypapersize As System.Drawing.Printing.PaperSize, ByRef thePaperMargin As System.Drawing.Printing.Margins, ByRef isPaperLandscape As System.Boolean) Handles myp.QueryDefaultPageSettings
        isPaperLandscape = False
        ' mypapersize = New System.Drawing.Printing.PaperSize("XXE", CInt(myp.ConvertCmToInch(9) * 100), CInt(myp.ConvertCmToInch(20) * 100))

    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        '表格文字混合输出
        printtype = 13
        myp.InvalidatePreview()
    End Sub




    Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem3.Click
        'D1.Rows(1).Visible = False
        'D1.Columns(1).Visible = False
        printtype = 100
        myp.InvalidatePreview()
    End Sub


    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        printtype = 200
        myp.InvalidatePreview()
    End Sub


    Private Sub 使用方案文件打印表格ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 使用方案文件打印表格ToolStripMenuItem.Click
        printtype = 300
        myp.InvalidatePreview()
    End Sub

    Private Sub 使用方案打印表格先输出文字再输出表格ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 使用方案打印表格先输出文字再输出表格ToolStripMenuItem.Click
        printtype = 400
        myp.InvalidatePreview()
    End Sub

    Private Sub 使用方案打印表格先输出文字再输出表格表格是换新一页打印的ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 使用方案打印表格先输出文字再输出表格表格是换新一页打印的ToolStripMenuItem.Click
        printtype = 500
        myp.InvalidatePreview()
    End Sub

    Private Sub myp_EndPreView() Handles myp.EndPreView
        'MsgBox("打印预览结束")
        ' myp.Print()
    End Sub

    Private Sub myp_EndPrintToPrinter_1() Handles myp.EndPrintToPrinter
        'MsgBox("打印结束")
    End Sub



    Private Sub 分组汇总加行号综合测试ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 分组汇总加行号综合测试ToolStripMenuItem.Click
        '分组汇总加行号综合测试
        printtype = 2000
        myp.InvalidatePreview()

    End Sub

    Private Sub 加行号测试ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 加行号测试ToolStripMenuItem.Click
        '分组汇总加行号不换新页打印
        printtype = 2001
        myp.InvalidatePreview()
    End Sub

    Private Sub 分组加行号不换新页打印ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 分组加行号不换新页打印ToolStripMenuItem.Click
        '分组加行号不换新页打印
        printtype = 2002
        myp.InvalidatePreview()
    End Sub

    Private Sub 普通的加行号打印ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 普通的加行号打印ToolStripMenuItem.Click
        '普通的加行号打印
        printtype = 2003
        myp.InvalidatePreview()
    End Sub


    Private Sub 工资条分组打印加行号ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 工资条分组打印加行号ToolStripMenuItem.Click
        '工资条形式的分组打印
        printtype = 3000
        myp.InvalidatePreview()
    End Sub



    'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    '    myp.Visible = Not myp.Visible
    'End Sub




 
    Private Sub 纯文本输出测试ToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 纯文本输出测试ToolStripMenuItem.Click
        printtype = 1
        myp.InvalidatePreview()
    End Sub

    Private Sub 单元格测试ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 单元格测试ToolStripMenuItem.Click
        printtype = 2
        myp.InvalidatePreview()
    End Sub

    Private Sub 文本测试ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 文本测试ToolStripMenuItem.Click
        '表格文字混合输出
        printtype = 14
        myp.InvalidatePreview()
    End Sub

   
    Private Sub 设置背景图片ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 设置背景图片ToolStripMenuItem.Click
        myp.SetPageBackGroundImage(My.Resources._55)
        myp.InvalidatePreview()
    End Sub

    Private Sub 清除背景图片ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 清除背景图片ToolStripMenuItem.Click
        myp.SetPageBackGroundImage("")
        myp.InvalidatePreview()
    End Sub


    

 

    Private Sub 用NewDoc与EndDoc创建打印作业ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 用NewDoc与EndDoc创建打印作业ToolStripMenuItem.Click
        myp.NewDoc()
        myp.NewPage()
        'myp.IsImmediatePrintNotPreview = True '立即打印
        Dim mymargin As New System.Drawing.Printing.Margins(100, 200, 200, 300)
        myp.TextFont = New Font("黑体", 12, FontStyle.Bold)
        Dim myformat As StringFormat
        myformat = New StringFormat(StringFormat.GenericTypographic)
        myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本

        myp.NewRow(myp.LastDrawTextHeight) '换新行
        D1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        D1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
        myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5)
        myp.DrawText("这是表格下面可以输出的内容")

        myp.NewPage(True)
        myp.DrawText("总是按横向打印表格", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5)
        myp.DrawText("这是表格下面可以输出的内容")

        myp.NewPage(System.Drawing.Printing.PaperKind.A3, False) '新页
        myp.DrawText("由于本页的NewPage中带有参数,故输出页面版式随不管如何进行页面设置,总是按A3纸(如果您的打印机不支持A3纸,则还是A4纸)纵向打印,只有页边距会随页面设置而改变", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "1111", True, True, 5, True, True)

        myp.NewPage(System.Drawing.Printing.PaperKind.A4, mymargin, False) '新页
        myp.DrawText("由于本页的NewPage中带有参数,故输出页面版式随不管如何进行页面设置,总是按A4纸纵向打印,页边距也是固定了的", myp.PaperPrintWidth, New Font("宋体", 34), Color.Red, myformat)
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.DrawText(TextBox2.Text, myp.PaperPrintWidth) '输出文本
        myp.NewRow(myp.LastDrawTextHeight) '换新行
        myp.PrintDGV(D1, "以下输出表格1的内容", New Font("黑体", 18), StringAlignment.Center, "0101", True, True, 4)

        myp.EndDoc()
        ' myp.Print(True) '立即打印
    End Sub

    Private Sub 打印TreeView控件的内容ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印TreeView控件的内容ToolStripMenuItem.Click
        myp.NewDoc()
        myp.NewPage()
        myp.PrintTreeView(TreeView1, "4.5版打印控件增加了TreeView控件的打印哦", New Font("黑体", 20), Color.Blue, 6)
        myp.DrawText("这是打印TreeView控件后接下去可打印的地方哦")
        myp.EndDoc()
    End Sub

  
    Private Sub 打印TreeView控件的第一与第二层的内容ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印TreeView控件的第一与第二层的内容ToolStripMenuItem.Click
        myp.NewDoc()
        myp.NewPage()
        myp.PrintTreeView(TreeView1, "本次演示要求只打印到第二层哦，结果是不是这样", 2)
        myp.DrawText("这是打印TreeView控件后接下去可打印的地方哦")
        myp.EndDoc()
    End Sub

   
   

    Private Sub 打印ListView的内容ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印ListView的内容ToolStripMenuItem.Click
        '打印ListView的内容
        myp.NewDoc()
        myp.NewPage()
        Dim mydgv As DataGridView
        mydgv = myp.ConVertListViewToDGV(ListView1)
        myp.PrintDGV(mydgv, "打印ListView的内容(先用ConvertListViewToDGV将其转换成DataGridView，然后再用打印DGV的函数打印）", New Font("黑体", 18), StringAlignment.Center)
        myp.DrawText("这是打印ListView控件后接下去可打印的地方哦")
        myp.EndDoc()
    End Sub



 
 
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

  

End Class