1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| Public Class Form1
Private strFormat As StringFormat
Private arrColumnLefts As New ArrayList()
Private arrColumnWidths As New ArrayList()
Private iCellHeight As Integer = 0
Private iTotalWidth As Integer = 0
Private iRow As Integer = 0
Private bFirstPage As Boolean = False
Private bNewPage As Boolean = False
Private iHeaderHeight As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintDocument1.DocumentName = "Test Page Print"
Me.PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
Try
strFormat = New StringFormat()
strFormat.Alignment = StringAlignment.Near
strFormat.LineAlignment = StringAlignment.Center
strFormat.Trimming = StringTrimming.EllipsisCharacter
arrColumnLefts.Clear()
arrColumnWidths.Clear()
iCellHeight = 0
iRow = 0
bFirstPage = True
bNewPage = True
iTotalWidth = 0
For Each dgvGridCol As DataGridViewColumn In DataGridView1.Columns
iTotalWidth += dgvGridCol.Width
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
If bFirstPage Then
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
iTmpWidth = Math.Floor(GridCol.Width / iTotalWidth * iTotalWidth * e.MarginBounds.Width / iTotalWidth)
iHeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height + 11
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin += iTmpWidth
Next
End If
While iRow <= DataGridView1.Rows.Count - 1
Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)
iCellHeight = GridRow.Height + 5
Dim iCount As Integer = 0
If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
Exit While
Else
If bNewPage Then
e.Graphics.DrawString("JE VEUX PLUS DE L'ESPACE POUR ECRIRE PLUS DE TEXTE", New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
Dim strDate As String = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()
e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In DataGridView1.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), iTopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), iTopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(DirectCast(arrColumnLefts(iCount), Integer), iTopMargin, DirectCast(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
System.Math.Max(System.Threading.Interlocked.Increment(iCount), iCount - 1)
Next
bNewPage = False
iTopMargin += iHeaderHeight
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If Not Cel.Value Is Nothing Then
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(DirectCast(arrColumnLefts(iCount), Integer), DirectCast(iTopMargin, Integer), DirectCast(arrColumnWidths(iCount), Integer), DirectCast(iCellHeight, Integer)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(DirectCast(arrColumnLefts(iCount), Integer), iTopMargin, DirectCast(arrColumnWidths(iCount), Integer), iCellHeight))
System.Math.Max(System.Threading.Interlocked.Increment(iCount), iCount - 1)
Next
End If
System.Math.Max(System.Threading.Interlocked.Increment(iRow), iRow - 1)
iTopMargin += iCellHeight
End While
If bMorePagesToPrint Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
End Class |
Partager