📄 list.vb
字号:
End Try
End Sub
Private Sub MenuPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuPreview.Click
Try
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
MessageBox.Show("错误信息:" & ex.Message.ToString, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub MenuPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuPrint.Click
Try
Dim answer As System.Windows.Forms.DialogResult
PrintDialog1.Document = PrintDocument1
PrintDialog1.AllowSomePages = True
PrintDialog1.AllowSelection = True
answer = PrintDialog1.ShowDialog
PrintDialog1.PrintToFile = False
If answer = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
MessageBox.Show("错误信息:" & ex.Message.ToString, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ThePen As System.Drawing.Pen = New Pen(Color.Black)
Dim TheRect As System.Drawing.Rectangle
Dim TheFont = New Font("宋体", 10, FontStyle.Regular) '打印字体
Dim lpp As Single = 0 '一页打印的行数
Dim yPos As Single = 0 '每一行列的起始位置
Dim xPos As Single = 0 '每一列的起始位置
Dim Row As Int32 = 0 '一页中打印的当前行
Dim i, j As Int16
Dim Column As Int16 = ListView1.Columns.Count
Dim Width(Column) As Int16
Dim LeftMargin As Single = e.MarginBounds.Left '左边距
Dim RightMargin As Single = e.MarginBounds.Right
Dim BottomMargin As Single = e.MarginBounds.Bottom
Dim TopMargin As Single = e.MarginBounds.Top '上边距
Try
lpp = e.MarginBounds.Height / TheFont.getheight(e.Graphics)
Call Check_Width(ListView1.Items.Count, Column, Width) '计算每一个字段的最大宽度
For i = 0 To Column
Width(i) = Width(i) + 2 '字段间隔为2
Next
'打印标题
e.Graphics.DrawString(CountItem, New Font("隶书", 18, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2 - LeftMargin - Len(CountItem) * 5, TopMargin, New StringFormat)
If PageNo = 0 Then '只有第一页打印表头
Row = Row + 2
'yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
TheRect = New Rectangle(LeftMargin, TopMargin + 3 * TheFont.GetHeight(e.Graphics), e.MarginBounds.Width, e.MarginBounds.Height - 2 * TheFont.GetHeight(e.Graphics))
e.Graphics.DrawRectangle(ThePen, TheRect) '画一个矩形
Row = Row + 1
xPos = LeftMargin '打印竖线
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
For i = 1 To Column - 1
xPos = xPos + Width(i - 1) * 10
e.Graphics.DrawLine(ThePen, xPos - 5, TopMargin + 3 * TheFont.GetHeight(e.Graphics), xPos - 5, e.MarginBounds.Bottom + TheFont.GetHeight(e.Graphics)) '打印一条竖线
Next
xPos = xPos + Width(i - 1) * 10
If xPos < RightMargin - 100 Then '最后一条竖线
e.Graphics.DrawLine(ThePen, xPos - 5, TopMargin + 3 * TheFont.GetHeight(e.Graphics), xPos - 5, e.MarginBounds.Bottom + TheFont.GetHeight(e.Graphics)) '打印一条竖线
End If
Else
TheRect = New Rectangle(LeftMargin, TopMargin + 3 * TheFont.GetHeight(e.Graphics), e.MarginBounds.Width, e.MarginBounds.Height - 2 * TheFont.GetHeight(e.Graphics))
e.Graphics.DrawRectangle(ThePen, TheRect) '画一个矩形
xPos = LeftMargin '打印竖线
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
For i = 1 To Column - 1
xPos = xPos + Width(i - 1) * 10
e.Graphics.DrawLine(ThePen, xPos - 5, TopMargin + 3 * TheFont.GetHeight(e.Graphics), xPos - 5, e.MarginBounds.Bottom + TheFont.GetHeight(e.Graphics)) '打印一条竖线
Next
xPos = xPos + Width(i - 1) * 10
If xPos < RightMargin - 100 Then '最后一条竖线
e.Graphics.DrawLine(ThePen, xPos - 5, TopMargin + 3 * TheFont.GetHeight(e.Graphics), xPos - 5, e.MarginBounds.Bottom + TheFont.GetHeight(e.Graphics)) '打印一条竖线
End If
Row = Row + 3 '后移两行
End If
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
xPos = LeftMargin
Row = Row + 1
'打印字段名
e.Graphics.DrawString(ListView1.Columns(0).Text, TheFont, Brushes.Black, xPos, yPos + 15, New StringFormat)
For i = 1 To Column - 1
xPos = xPos + Width(i - 1) * 10
e.Graphics.DrawString(ListView1.Columns(i).Text, TheFont, Brushes.Black, xPos, yPos + 15, New StringFormat)
Next
Row = Row + 1
xPos = xPos + (Width(Column - 1) - 4) * 10
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
'e.Graphics.DrawLine(ThePen, LeftMargin, yPos, RightMargin, yPos) '打印一条横线
Row = Row + 1
While (PageCount < ListView1.Items.Count And Row < lpp)
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
xPos = LeftMargin
e.Graphics.DrawString(ListView1.Items(PageCount).SubItems(0).Text, TheFont, Brushes.Black, xPos, yPos + 5, New StringFormat)
For j = 1 To Column - 1
xPos = xPos + Width(j - 1) * 10
e.Graphics.DrawString(ListView1.Items(PageCount).SubItems(j).Text, TheFont, Brushes.Black, xPos, yPos + 5, New StringFormat)
Next
Row = Row + 1
e.Graphics.DrawLine(ThePen, LeftMargin, yPos - 5, RightMargin, yPos - 5) '打印一条横线
Row = Row + 1
PageCount = PageCount + 1
End While
yPos = TopMargin + (Row * TheFont.GetHeight(e.Graphics))
While yPos < BottomMargin - TheFont.GetHeight(e.Graphics)
e.Graphics.DrawLine(ThePen, LeftMargin, yPos - 5, RightMargin, yPos - 5) '打印最后横线
yPos = yPos + 40
End While
PageNo = PageNo + 1
Row = Row + 1
e.Graphics.DrawString("第" & CStr(PageNo) & "页", TheFont, Brushes.Black, e.MarginBounds.Right / 2, e.MarginBounds.Bottom + 40, New StringFormat)
If Row > lpp Then
e.HasMorePages = True
Row = 0
Else
e.HasMorePages = False
End If
Catch ex As Exception
MessageBox.Show("错误信息:" & ex.Message.ToString, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PageCount = 0
PageNo = 0
End Sub
Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
PageCount = 0
PageNo = 0
End Sub
Public Sub Check_Width(ByVal m As Int64, ByVal n As Int16, ByVal Wid() As Int16) 'm表示列表中的行,n表示列
Dim i, j As Int64
Try
For j = 0 To n - 1 '设置字段宽度初值为字段头部的宽度,由于字头都是汉字,所以宽度乘2
Wid(j) = Len(Trim(ListView1.Columns(j).Text)) * 2
Next
Dim temp As String
'找出每列数据中字符宽度最大的数据的宽度作为将来保存或打印的字段宽度
For j = 0 To n - 1
For i = 0 To m - 1
If Trim(ListView1.Items(i).SubItems(j).Text) = Nothing Then
temp = ""
Else
temp = Trim(ListView1.Items(i).SubItems(j).Text)
End If
If Len(temp) > 0 Then '如果内容为空不用判断
If Asc(temp.Substring(0, 1)) >= 0 And Asc(temp.Substring(0, 1)) <= 128 Then
If Len(temp) > Wid(j) Then Wid(j) = Len(temp)
Else
If Len(temp) * 2 > Wid(j) Then Wid(j) = Len(temp) * 2
End If
End If
Next
Next
Catch ex As Exception
MessageBox.Show(CStr(i) & CStr(j), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MessageBox.Show("错误信息:" & ex.Message.ToString, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub ListView1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
'ToolTip1.SetToolTip(ListView1, "右击在快捷菜单中可以打印")
End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -