Salut,
hier ein Beispiel:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.PrintPreviewDialog1 = New PrintPreviewDialog
With Me.PrintPreviewDialog1
intLastStartIndex = 0
.Document = PrintDocument1
.UseAntiAlias = True
.ShowDialog()
End With
End Sub
Private intLastStartIndex As Integer
''' <summary>
''' Prints the page header
''' </summary>
''' <param name="e">PrintPageEventArgs object</param>
''' <returns>the height of the printed header</returns>
Private Function PrintHeader(ByVal e As System.Drawing.Printing. _
PrintPageEventArgs) As Integer
Dim Font_Caption As New Font("Comic Sans MS", 12, _
FontStyle.Underline Or FontStyle.Bold)
Dim NameSize As Drawing.SizeF
NameSize = e.Graphics.MeasureString("Überschrift", Font_Caption)
e.Graphics.DrawString("Überschrift", Font_Caption, Brushes.Black, _
e.MarginBounds.Left + (e.MarginBounds.Width - NameSize.Width) / 2, _
e.MarginBounds.Top)
Font_Caption.Dispose()
Return NameSize.Height + 30
End Function
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
Dim headerHeight As Integer
Dim curTop As Integer = e.MarginBounds.Top
Dim curLeft As Integer = e.MarginBounds.Left
Dim TextSize As New Drawing.SizeF
Dim font_Text As New Font(Me.lstSongs.Font, FontStyle.Regular)
e.HasMorePages = False
' Ueberschrift
headerHeight = PrintHeader(e)
curTop += headerHeight
For i As Integer = intLastStartIndex To Me.lstSongs.Items.Count - 1
TextSize = e.Graphics.MeasureString( _
Me.lstSongs.Items(i).ToString(), font_Text)
If e.MarginBounds.Height >= (curTop + TextSize.Height) Then
' passt noch auf die Seite
e.Graphics.DrawString(Me.lstSongs.Items(i).ToString(), _
font_Text, Brushes.Black, curLeft, curTop)
curTop += TextSize.Height
Else
' reicht nicht mehr, auf rechte Seite wechseln
If curLeft = e.MarginBounds.Left Then
' zu rechter Seite wechseln
curLeft = e.MarginBounds.Left + e.MarginBounds.Width / 2
curTop = e.MarginBounds.Top + headerHeight
e.Graphics.DrawString(Me.lstSongs.Items(i).ToString(), _
font_Text, Brushes.Black, curLeft, curTop)
curTop += TextSize.Height
Else
' war schon auf rechter Seite -> naechste Seite
e.HasMorePages = True
intLastStartIndex = i
Exit For
End If
End If
Next
font_Text.Dispose()
End Sub Dazu wird verwendet:
* ein PrintDialog-Control: PrintDialog1
* ein PrintDocument-Control: PrintDocument1
* ein PrintPreviewDialog-Control: PrintPreviewDialog1
* Button: Button1
* lstSongs: ListBox mit den Songs.
Stefan
Web: http://www.vbtricks.de.vu/
VBTricks.de.vu. Meine Webseite zu VB und anderen Programmiersprachen. Verschiedene fortgeschrittene OCXe und komplette Projekte sind im Sourcecode verf?gbar. |