Probier mal dieses Formular ....
Imports System.Drawing.Printing
Public Class frmPrintRich
' Variablen für Druckfunktion
Private m_nFirstCharOnPage As Integer ' Variable für Druck
Private iPage As Short
Private iPages As Short
Private initPrint As Boolean = False
Dim richtextboxex1 As New RichTextBoxEx With {.Parent = Me, .Width = 300, _
.Height = 280}
Dim WithEvents btnPrint As New Button With {.Parent = Me, .Top = 300, .Text _
= "Print"}
Dim WithEvents printdocument1 As New printdocument
Dim WithEvents printdialog1 As New PrintDialog
Dim WithEvents printpreviewdialog1 As New PrintPreviewDialog
Private Sub frmPrintRich_Load(sender As System.Object, e As _
System.EventArgs) Handles MyBase.Load
Me.Size = New Size(310, 350)
End Sub
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As _
System.Drawing.Printing.PrintEventArgs) Handles printdocument1.BeginPrint
' Start at the beginning of the text
m_nFirstCharOnPage = 0
iPage = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e _
As System.Drawing.Printing.PrintPageEventArgs) Handles _
printdocument1.PrintPage
If initPrint = True Then iPages += CShort(1)
iPage += CShort(1)
' Code für Kopfzeile (Seitenanzahl)
Dim oPen6 As New Pen(Color.Black, 1.2)
Dim oFont1 As New Font("Arial", 10, FontStyle.Bold)
With e.MarginBounds
e.Graphics.DrawLine(oPen6, .X, .Y - 15, e.PageBounds.Width - .X - _
(e.PageBounds.Right - .Right), .Y - 15)
If initPrint = True Then
e.Graphics.DrawString("Seite: " & iPage & "/?", oFont1, _
Brushes.Black, .X, .Y - 35)
Else
e.Graphics.DrawString("Seite: " & iPage & "/" & iPages, oFont1, _
Brushes.Black, .X, .Y - 35)
End If
End With
With richtextboxex1
m_nFirstCharOnPage = .FormatRange(False, e, m_nFirstCharOnPage, _
.TextLength)
' Prüfen ob noch Seiten gedruckt werden müssen
e.HasMorePages = (m_nFirstCharOnPage < .TextLength)
End With
End Sub
Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As _
System.Drawing.Printing.PrintEventArgs) Handles printdocument1.EndPrint
' Variablen für Druck zurücksezen
richtextboxex1.FormatRangeDone()
initPrint = False
' In PrintPreviewDialog.Text die Anzahl Gesamtseiten ausgeben
printpreviewdialog1.Text = "Druck-Vorschau (Anzahl Seiten: " & _
iPages.ToString & ")"
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnPrint.Click
With printdialog1
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
' Einstellungen von Dialog an PringDocument übergeben
printdocument1.PrinterSettings = .PrinterSettings
With printdocument1.DefaultPageSettings.Margins()
.Bottom = 70
.Top = 70
.Left = 50
.Right = 50
End With
' Show Print Preview
iPages = 0
initPrint = True
printpreviewdialog1.Document = printdocument1
printpreviewdialog1.WindowState = FormWindowState.Maximized
printpreviewdialog1.ShowDialog()
End If
End With
End Sub
End Class |