16,554
社区成员
发帖
与我相关
我的任务
分享
Dim PreView As New PrintPreviewDialog
PreView.Document = pd
Try
PreView.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Try
End Sub
Private Sub MenuPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPageSetup.Click
Dim Ps As New PageSetupDialog
Ps.Document = pd
Try
Ps.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Try
End Sub
Dim pageno As Int16
Dim NewPage As Boolean
Dim RowPos As Int16
Private Sub pd_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.EndPrint
RowPos = 0
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Dim TmpWidth As Int16, TmpLeft As Int16, Tmptop As Int16, Tolwidth As Int16
Dim ColumnLefts As New ArrayList, ColumnWidths As New ArrayList
Dim i As Int16
Dim CellHeight As Int16
Dim StrFormat As New StringFormat
Dim StringFont As Font = DataGrid.DefaultCellStyle.Font
With StrFormat
.LineAlignment = StringAlignment.Center
.Alignment = StringAlignment.Center
.Trimming = StringTrimming.EllipsisCharacter
End With
TmpLeft = e.MarginBounds.X
Tmptop = e.MarginBounds.Y
'获取打印单元格的高度和宽度
For Each Gridcol As DataGridViewColumn In DataGrid.Columns
If Not Gridcol.Visible Then Continue For
TmpWidth = Gridcol.Width
ColumnLefts.Add(TmpLeft)
ColumnWidths.Add(TmpWidth)
TmpLeft += TmpWidth
Tolwidth += TmpWidth
Next
'调整打印位置使其居中
TmpLeft = (e.MarginBounds.Width - Tolwidth) / 2
'打印表头
For Each GridCol As DataGridViewColumn In DataGrid.Columns
If Not GridCol.Visible Then Continue For
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i) + TmpLeft, _
Tmptop, ColumnWidths(i), DataGrid.ColumnHeadersHeight))
e.Graphics.DrawString(GridCol.HeaderText, DataGrid.ColumnHeadersDefaultCellStyle.Font, _
New SolidBrush(GridCol.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i) + TmpLeft, Tmptop, ColumnWidths(i), _
DataGrid.ColumnHeadersHeight), StrFormat)
i += 1
Next
i = 0
StrFormat.Alignment = StringAlignment.Near '设置单元格文本的对齐方式
'打印表格和内部文本——————————
Try
'一行一行的打印
Do While RowPos <= DataGrid.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGrid.Rows(RowPos)
If GridRow.IsNewRow Then
RowPos += 1
Continue Do
End If
CellHeight = GridRow.Height
If Tmptop < e.MarginBounds.Height Then '判断是否分页
'打印表主体
For Each Cel As DataGridViewCell In GridRow.Cells
If Not Cel.OwningColumn.Visible Then Continue For
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i) + TmpLeft, Tmptop + DataGrid.ColumnHeadersHeight, ColumnWidths(i), _
CellHeight), StrFormat) '画单元格
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i) + TmpLeft, Tmptop + DataGrid.ColumnHeadersHeight, ColumnWidths(i), CellHeight)) '画单元格内的文本
i += 1
Next
Tmptop += CellHeight
i = 0
RowPos += 1
e.HasMorePages = False
Else
e.HasMorePages = True
Exit Do
End If
Loop
Catch ex As Printing.InvalidPrinterException
MessageBox.Show(ex.Message, "测试", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End Sub