Tipp 0043 Drucken mit Vorschau
Autor/Einsender:
Datum:
  Michael Werner
04.06.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Ein Tipp rund um das Drucken mit Hilfe der Klassen PrintDialog, PrintPreviewDialog, PageSetupDialog und PrintDocument. Die Dialoge für die Druckereinstellung, Seiteneinstellung und Druckvorschau werden vorgestellt, auch das Drucken und Positionieren von Text, Zeichnungen, Bildern, Anzeige des Druckbereichs, Drucken über mehreren Seiten, Drucken von Seitenzahlen usw.
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Private WithEvents PrintDoc As New Printing.PrintDocument
  Private iCurrPage As Integer
  Dim j As Integer
  Dim sText As String = _
       "Ein Tipp rund um das Drucken mit Hilfe der Klassen..."

  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load

    Dim s As String
    Dim i As Integer

    With ComboBox1
      For Each s In Printing.PrinterSettings.InstalledPrinters
        .Items.Add(s)
        If CStr(ComboBox1.Items.Item(i)) = _
              PrintDoc.PrinterSettings.PrinterName Then
          j = i
        End If
        i += 1
      Next s

      If .Items.Count > 0 Then
        ComboBox1.SelectedIndex = j
      Else
        MessageBox.Show("Es ist kein Drucker installiert!", _
              "Abbruch", MessageBoxButtons.OK, _
              MessageBoxIcon.Exclamation)
        Me.Close()
      End If
    End With
    PrintDoc.DocumentName = "Druckdokument"
    AddHandler PrintDoc.PrintPage, AddressOf printDoc_PrintPage
    iCurrPage = 0
  End Sub

  Private Sub Combobox1_SelectedIndexChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) Handles _
          ComboBox1.SelectedIndexChanged
    If ComboBox1.SelectedIndex <> -1 Then
      PrintDoc.PrinterSettings.PrinterName = ComboBox1.Text
    End If
    If ComboBox1.SelectedIndex = j Then
      Label1.Visible = True
    Else
      Label1.Visible = False
    End If
  End Sub

  Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, _
          ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    iCurrPage += 1

    Dim printrect As Rectangle = e.MarginBounds

    Select Case iCurrPage
     Case 1
       If CheckBox1.Checked Then
         e.Graphics.DrawRectangle(Pens.Black, printrect)
       End If

       Dim pt As Point = New Point(150, 150)
       e.Graphics.DrawImage(PictureBox1.Image, pt)

       Dim fntFont As New Font("Arial", 12, FontStyle.Bold)
       e.Graphics.DrawString( _
           sText1, _
           fntFont, _
           New SolidBrush(Color.Black), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText1, fntFont).Width) _
             * 0.5), CSng(200))

       Dim fntFont1 As New Font("Arial", 12)
       e.Graphics.DrawString( _
           sText, _
           fntFont, _
           New SolidBrush(Color.Black), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText, fntFont).Width) _
             * 0.5), CSng(300))

       Dim fntFont2 As New Font("Comic Sans Ms", 12, _
                                FontStyle.Regular)
       e.Graphics.DrawString( _
           sText, _
           fntFont2, _
           New SolidBrush(Color.DarkBlue), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText, fntFont1).Width) _
             * 0.5), CSng(700))

       If CheckBox2.Checked Then
         e.Graphics.DrawString( _
             "Seite " & iCurrPage.ToString, _
             New Font("Times New Roman", 10), _
             New SolidBrush(Color.Black), _
             e.MarginBounds.Right, _
             e.MarginBounds.Bottom)
       End If

       e.HasMorePages = True

     Case 2
       If CheckBox1.Checked Then
         e.Graphics.DrawRectangle(Pens.Black, printrect)
       End If

       e.Graphics.DrawEllipse( _
           New Pen(Color.MediumAquamarine, 60), _
           300, 300, 200, 200)

       Dim oPointsDreieck As Point() = { _
           New Point(250, 150), _
           New Point(400, 410), _
           New Point(620, 260)}
       Dim oPen8 As New Pen(Color.DarkRed, 3)
       e.Graphics.DrawPolygon(oPen8, oPointsDreieck)

       Dim fntFont As New Font("Arial", 12)
       e.Graphics.DrawString( _
           sText, _
           fntFont, _
           New SolidBrush(Color.Black), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText, fntFont).Width) _
             * 0.5), CSng(300))

       Dim fntFont2 As New Font("Time New Roman", 14, _
                                FontStyle.Bold)
       e.Graphics.DrawString( _
           sText, _
           fntFont2, _
           New SolidBrush(Color.MediumAquamarine), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText, fntFont2).Width) _
             * 0.5), CSng(600))

       Dim pt As Point = _
             New Point(e.PageBounds.Width - 200, _
             e.PageBounds.Height - 200)
       e.Graphics.DrawImage(PictureBox2.Image, pt)

       Dim fntFont3 As New Font("Arial", 14, FontStyle.Regular)
       e.Graphics.DrawString( _
           sText2, _
           fntFont3, _
           New SolidBrush(Color.DarkRed), _
           CSng(( _
             e.PageBounds.Width - _
             e.Graphics.MeasureString(sText2, fntFont3).Width) _
             * 0.5), CSng(e.PageBounds.Height - 250))

       If CheckBox2.Checked Then
         e.Graphics.DrawString( _
             "Seite " & iCurrPage.ToString, _
             New Font("Times New Roman", 12), _
             New SolidBrush(Color.Black), _
             e.MarginBounds.Right, _
             e.MarginBounds.Bottom)
       End If

       e.HasMorePages = False
       iCurrPage = 0
    End Select
  End Sub

  Private Sub PrinterPreview()
    iCurrPage = 0

    Dim printpreviewDlg As New PrintPreviewDialog
    With printpreviewDlg
      .Document = PrintDoc
      .WindowState = FormWindowState.Maximized
      .ShowDialog(Me)
    End With
    printpreviewDlg = Nothing
  End Sub

  Private Sub PrinterDialog()
    Dim printDlg As New PrintDialog

    With printDlg
      .Document = PrintDoc
      .PrinterSettings = PrintDoc.PrinterSettings
      .AllowPrintToFile = False
      If .ShowDialog(Me) = DialogResult.OK Then
        SelectPrinter(ComboBox1, .PrinterSettings.PrinterName)
        PrintDoc.Print()
      End If
    End With
    printDlg = Nothing
  End Sub

  Private Sub SelectPrinter(ByRef cboComboBox As ComboBox, _
          ByRef strPrinterName As String)

    Dim i As Integer
    For i = 0 To cboComboBox.Items.Count - 1
      If Convert.ToString(cboComboBox.Items(i)) = _
            strPrinterName Then
        cboComboBox.SelectedIndex = i
        Exit For
      End If
    Next i

    If i = j Then
      Label1.Visible = True
    Else
      Label1.Visible = False
    End If
  End Sub

  Private Sub PageSetupDlg()
    Dim pagesetupDlg As New PageSetupDialog

    With pagesetupDlg
      .PrinterSettings = PrintDoc.PrinterSettings
      .PageSettings = PrintDoc.DefaultPageSettings
      If .ShowDialog(Me) = DialogResult.OK Then
        .PageSettings.Margins = _
            Printing.PrinterUnitConvert.Convert( _
            .PageSettings.Margins, _
            Drawing.Printing.PrinterUnit.ThousandthsOfAnInch, _
            Drawing.Printing.PrinterUnit.HundredthsOfAMillimeter)
        SelectPrinter(ComboBox1, .PrinterSettings.PrinterName)
      End If
    End With
    pagesetupDlg = Nothing
  End Sub
End Class
 
Weitere Links zum Thema
PrintForm-Komponente
Windows Form drucken

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,2 kB) Downloads bisher: [ 2289 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 22. Januar 2012