Hsmorepages

domenica 12 ottobre 2008 - 15.18

ans66 Profilo | Junior Member

ho preso spunto da una routine di amdbook che mi e` stata molto utile per capire meglio hamorepages. L'ho elaborata come segue:
================================================================================================================
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim dirDoc As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
e.Graphics.DrawImage(Image.FromFile(dirDoc & "\SF.bmp"), 550, 10, 190, 150)
e.Graphics.DrawString(Date.Today.ToShortDateString, New Font("Arial", 12, FontStyle.Bold), Brushes.LightBlue, 20, 20)
e.Graphics.DrawString("ORDINE " & DataGridView1.Item(0, 0).Value, New Font("Arial", 18, FontStyle.Bold), Brushes.Red, 250, 50)
DataGridView1.Columns(0).Name = "Nr. Ordine"
DataGridView1.Columns(1).Name = "Cod. Meccan."
DataGridView1.Columns(2).Name = "Descrizione"
DataGridView1.Columns(3).Name = "Prezzo lordo"
DataGridView1.Columns(4).Name = "Q.tà"
DataGridView1.Columns(5).Name = "Netto"
DataGridView1.Columns(6).Name = "Tot. Netto"



Try
Static RigheStampate As Integer
Static PagineStampate As Integer

Dim Table As DataTable = CType(DataGridView1.DataSource, DataTable)
Dim RigheTotali As Integer = DataGridView1.Rows.Count - 1
Dim ControllaPagine As Boolean = True

'Crea l'intestazione
Dim Rect As Rectangle
Dim ColCorrente As Integer = 0 'Indica la colonna corrente
Dim Top As Integer = e.MarginBounds.Top 'Top = Margine superiore
Dim Left As Integer = e.MarginBounds.Left 'Left = Margine sinistro
Dim Height As Integer = DataGridView1.RowTemplate.Height

'Quante righe posso stampare su un foglio?
Dim MaxRighe As Integer = e.MarginBounds.Height / Height
'In totale quante pagine
Dim MaxPagine As Integer = DataGridView1.Rows.Count - 1 / MaxRighe

Dim BrHeader As SolidBrush = New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
Dim HFont As Font = DataGridView1.ColumnHeadersDefaultCellStyle.Font

For Each col As DataGridViewColumn In DataGridView1.Columns
'Testo dell'Header
Dim ColText As String = col.Name
'Rettangolo dell'Header corrente
Rect = New Rectangle(Left, Top, col.Width, Height)
'Colora l'Header
e.Graphics.FillRectangle(BrHeader, Rect)
'Linee dell'Intestazione
e.Graphics.DrawRectangle(New Pen(Color.Gray, 1), Rect)
'Disegna il testo dell'Header
e.Graphics.DrawString(ColText, HFont, Brushes.Black, Left, _
Top + (e.Graphics.MeasureString(ColText, HFont).Height / 2))
'Calcola la prossima coordinata Left
Left += col.Width
Next

'Stampa la righe
Dim BrRow As SolidBrush = New SolidBrush(DataGridView1.DefaultCellStyle.BackColor)
Dim PnRow As Pen = New Pen(DataGridView1.GridColor)
Dim RigaCorrente As Integer = 0
Dim CelText As String = vbNullString

For RigaCorrente = RigheStampate To RigheStampate + MaxRighe
Left = e.MarginBounds.Left
Top += Height
For Each col As DataGridViewColumn In DataGridView1.Columns
'Testo della cella
If Len(DataGridView1.Rows(RigaCorrente).Cells(col.Name).Value()) <> 0 Then
CelText = DataGridView1.Rows(RigaCorrente).Cells(col.Name).Value.ToString
Else
CelText = vbNullString
End If


'Rettangolo dell'Intestazione corrente
Rect = New Rectangle(Left, Top, col.Width, Height)
'Colora lo sfondo della cella
e.Graphics.FillRectangle(BrRow, Rect)
'Disegna le linee del rettangolo della cella
e.Graphics.DrawRectangle(PnRow, Rect)
'Disegna il testo della cella
e.Graphics.DrawString(CelText, DataGridView1.Font, Brushes.Black, Left, _
Top + (e.Graphics.MeasureString(CelText, DataGridView1.Font).Height / 2))
'Calcola la prossima coordinata Left
Left += col.Width
Next
'Incrementa il contatore delle righe stampate
RigheStampate += 1

'Ci sono altre righe?
If RigheStampate >= DataGridView1.Rows.Count - 1 Then
e.Graphics.DrawString("Totale Ordine: " & TextBox4.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Blue, 520, _
Top + (e.Graphics.MeasureString("Totale Ordine: ", DataGridView1.Font).Height / 2) + 30)


' qui il problema ====================================='
Call Testo_Giust2(e, "Note: " & TextBox1.Text, Top, Font)
'===================================================



ControllaPagine = False
e.HasMorePages = False

'Azzera le variabili Static per un successivo utilizzo
RigheStampate = 0
PagineStampate = 0
Exit For
End If
Next RigaCorrente


'Devo controllare le pagine se non ho ancora terminato il numero di righe da
'Stampare
If ControllaPagine Then
'Incrementea il contatore delle pagine stampate
PagineStampate += 1

'Controlla se ci sono altre pagine da stampare...
If PagineStampate < MaxPagine Then
'Ci sono altre pagine
e.HasMorePages = True
Else
'Fine delle pagine
e.HasMorePages = False

'Azzera le variabili Static per un successivo utilizzo
RigheStampate = 0
PagineStampate = 0
End If
End If

PrintPreviewDialog1.Close()
'Distrugge gli oggetti
BrHeader.Dispose()
BrRow.Dispose()
PnRow.Dispose()

Catch Ex As Exception
If Err.Number = 91 Then
MessageBox.Show("Nessun record scelto nella griglia!", "Nessun record!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show(Err.Description & Space(2) & Err.Number)
End If
End Try
End Sub
===============================================================================================================

Alla fine del ciclo For Next (Righe DatagridView) inserisco una Sub routine Testo_Giust2() per giustificare il testo di una Textbox (note in fondo alla pagina): come fare ad inserire un nuovo salto pagina nel caso in cui sia necessario in quanto le note superano il margine basso della pagina?

===============================================================================================================

Public Sub Testo_Giust2(ByVal pr As System.Drawing.Printing.PrintPageEventArgs, ByVal Testo As String, ByVal Alto As Single, ByVal Font As System.Drawing.Font)

Testo = Testo.Replace(Chr(10), "")
Testo = Testo.Replace(Chr(13), Space(1))
Testo = Testo.Replace(Space(2), Space(1))

Dim LargRiga As Single = (pr.MarginBounds.Right - pr.MarginBounds.Left)
Dim ritStr() As String = Split(Testo, Space(1))
Static RigaControllo As String = vbNullString
Static RigaDaScriv As String = vbNullString
Dim NrParola As Integer = 0, i As Integer = 0
Static LunghParola As Integer = 0

Alto = Alto + 90

For i = 0 To UBound(ritStr) - 1


If pr.Graphics.MeasureString(RigaControllo, Font).Width <= LargRiga Then
RigaControllo = RigaControllo & ritStr(i) & Space(1)
LunghParola = ritStr(i).Length + 1

Else

RigaDaScriv = Mid(RigaControllo, 1, RigaControllo.Length - LunghParola)
CentraTesto(pr, Alto, RigaDaScriv, Font, LargRiga)
' qui passi a funzione CentraTesto
Alto = Alto + 15
'==============================================================================

if Alto= 750 then
' qui devo inserire un nuovo salto pagina'
end if
'==============================================================================
RigaControllo = Mid(RigaControllo, RigaDaScriv.Length + 1)
LunghParola = RigaControllo.Length
i = i - 1
End If

If i = UBound(ritStr) - 1 AndAlso pr.Graphics.MeasureString(RigaControllo, Font).Width <= LargRiga Then
RigaControllo = RigaControllo & ritStr(i + 1)
pr.Graphics.DrawString(RigaControllo, Font, Brushes.Black, pr.MarginBounds.Left, Alto)
End If


Next

RigaControllo = vbNullString
RigaDaScriv = vbNullString


End Sub


Allego file zip con sorgente per una migliore comprensione...grazie per l'aiuto
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5