VB.Net Tutorial/GUI/PrintPreview Dialog

Материал из VB Эксперт
Перейти к: навигация, поиск

PrintPreview page count

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
public class PrintPreviewPagesOut
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private currentPageNumber As Integer = 1
    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        If ppvdlgShapes.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ppvdlgShapes.Document.Print()
        End If
    End Sub
    Private Sub pdocShapes_PrintPage(ByVal sender As System.Object, _
     ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocShapes.PrintPage
        Select Case currentPageNumber
            Case 1
                e.Graphics.DrawRectangle(Pens.Green, e.MarginBounds())
                e.HasMorePages = True
                currentPageNumber += 1
            Case 2
                e.Graphics.DrawRectangle(Pens.Green, e.MarginBounds())
                e.HasMorePages = True
                currentPageNumber += 1
            Case 3
                e.Graphics.DrawEllipse(Pens.Blue, e.MarginBounds())
                e.HasMorePages = False
                currentPageNumber = 1
        End Select
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.btnPreview = New System.Windows.Forms.Button
        Me.pdocShapes = New System.Drawing.Printing.PrintDocument
        Me.ppvdlgShapes = New System.Windows.Forms.PrintPreviewDialog
        Me.SuspendLayout()
        "
        "btnPreview
        "
        Me.btnPreview.Location = New System.Drawing.Point(112, 48)
        Me.btnPreview.Name = "btnPreview"
        Me.btnPreview.Size = New System.Drawing.Size(64, 24)
        Me.btnPreview.TabIndex = 1
        Me.btnPreview.Text = "Preview"
        "
        "ppvdlgShapes
        "
        Me.ppvdlgShapes.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.ppvdlgShapes.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.ppvdlgShapes.ClientSize = New System.Drawing.Size(400, 300)
        Me.ppvdlgShapes.Document = Me.pdocShapes
        Me.ppvdlgShapes.Enabled = True
        Me.ppvdlgShapes.Name = "ppvdlgShapes"
        Me.ppvdlgShapes.Visible = False
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 142)
        Me.Controls.Add(Me.btnPreview)
        Me.Name = "Form1"
        Me.Text = "UsePrintPreviewDialog"
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents btnPreview As System.Windows.Forms.Button
    Friend WithEvents pdocShapes As System.Drawing.Printing.PrintDocument
    Friend WithEvents ppvdlgShapes As System.Windows.Forms.PrintPreviewDialog
End Class

Print, PrintPreview Dialog and Print Dialog

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing.Printing
public class PrintPreviewDemo
   public Shared Sub Main
        Application.Run(New MainForm)
   End Sub
End class

Public Class MainForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents printButton As System.Windows.Forms.Button
    Friend WithEvents previewButton As System.Windows.Forms.Button
    Friend WithEvents previewControlButton As System.Windows.Forms.Button
    Friend WithEvents pageSetupButton As System.Windows.Forms.Button
    Friend WithEvents pageSetupDialog1 As System.Windows.Forms.PageSetupDialog
    Friend WithEvents printDialog1 As System.Windows.Forms.PrintDialog
    Friend WithEvents printDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents printPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.printButton = New System.Windows.Forms.Button()
        Me.previewButton = New System.Windows.Forms.Button()
        Me.previewControlButton = New System.Windows.Forms.Button()
        Me.pageSetupButton = New System.Windows.Forms.Button()
        Me.pageSetupDialog1 = New System.Windows.Forms.PageSetupDialog()
        Me.printDialog1 = New System.Windows.Forms.PrintDialog()
        Me.printDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.printPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
        Me.SuspendLayout()
        "
        "printButton
        "
        Me.printButton.Location = New System.Drawing.Point(24, 16)
        Me.printButton.Name = "printButton"
        Me.printButton.Size = New System.Drawing.Size(112, 23)
        Me.printButton.TabIndex = 3
        Me.printButton.Text = "Print"
        "
        "previewButton
        "
        Me.previewButton.Location = New System.Drawing.Point(24, 80)
        Me.previewButton.Name = "previewButton"
        Me.previewButton.Size = New System.Drawing.Size(112, 23)
        Me.previewButton.TabIndex = 6
        Me.previewButton.Text = "Preview Dialog"
        "
        "previewControlButton
        "
        Me.previewControlButton.Location = New System.Drawing.Point(24, 48)
        Me.previewControlButton.Name = "previewControlButton"
        Me.previewControlButton.Size = New System.Drawing.Size(112, 23)
        Me.previewControlButton.TabIndex = 4
        Me.previewControlButton.Text = "Preview Control"
        "
        "pageSetupButton
        "
        Me.pageSetupButton.Location = New System.Drawing.Point(24, 112)
        Me.pageSetupButton.Name = "pageSetupButton"
        Me.pageSetupButton.Size = New System.Drawing.Size(112, 23)
        Me.pageSetupButton.TabIndex = 5
        Me.pageSetupButton.Text = "Page Setup"
        "
        "pageSetupDialog1
        "
        Me.pageSetupDialog1.MinMargins = New System.Drawing.Printing.Margins(50, 50, 50, 50)
        Me.pageSetupDialog1.ShowHelp = True
        "
        "printDialog1
        "
        Me.printDialog1.Document = Me.printDocument1
        "
        "printDocument1
        "
        "
        "printPreviewDialog1
        "
        Me.printPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.printPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.printPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.printPreviewDialog1.Enabled = True
        Me.printPreviewDialog1.Location = New System.Drawing.Point(44, 58)
        Me.printPreviewDialog1.MaximumSize = New System.Drawing.Size(0, 0)
        Me.printPreviewDialog1.Name = "printPreviewDialog1"
        Me.printPreviewDialog1.Opacity = 1
        Me.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
        Me.printPreviewDialog1.Visible = False
        "
        "MainForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(160, 150)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.printButton, Me.previewButton, Me.previewControlButton, Me.pageSetupButton})
        Me.Name = "MainForm"
        Me.Text = "MainForm"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Dim totalPages As Integer = 13
    Dim page As Integer
    Dim maxPage As Integer
    Dim myFont As Font = Nothing
    Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
        printDocument1.DocumentName = "myFile.txt"
        printDocument1.PrinterSettings.FromPage = 1
        printDocument1.PrinterSettings.ToPage = totalPages
        printDocument1.PrinterSettings.MinimumPage = 1
        printDocument1.PrinterSettings.MaximumPage = totalPages
        printDialog1.AllowSomePages = True
        If printDialog1.ShowDialog() = DialogResult.OK Then
            If printDialog1.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages Then
                page = printDocument1.PrinterSettings.FromPage
                maxPage = printDocument1.PrinterSettings.ToPage
            Else
                page = 1
                maxPage = totalPages
            End If
        End If
        printDocument1.Print()
    End Sub
    Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage
        Dim g As Graphics = e.Graphics
        g.DrawString("Hello, " & vbCrLf & "Printer" & vbCrLf & "Page: " & page, myFont, Brushes.Black, RectangleF.op_Implicit(e.MarginBounds))
        page = page + 1
        e.HasMorePages = (page <= maxPage)
    End Sub

    Private Sub previewControlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previewControlButton.Click
        page = 1
        maxPage = totalPages
        printPreviewDialog1.Document = printDocument1
        printPreviewDialog1.ShowDialog()
    End Sub
    Private Sub previewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previewButton.Click
        page = 1
        maxPage = totalPages
        Dim dlg As CustomPrintPreviewDialog = New CustomPrintPreviewDialog()
        dlg.Document = printDocument1
        dlg.ShowDialog()
    End Sub
    Private Sub pageSetupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pageSetupButton.Click
        pageSetupDialog1.Document = printDocument1
        pageSetupDialog1.ShowDialog()
    End Sub
    Private Sub printDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles printDocument1.BeginPrint
        myFont = New Font("Lucide Console", 72)
    End Sub
    Private Sub printDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles printDocument1.EndPrint
        myFont.Dispose()
        myFont = Nothing
    End Sub
End Class


Public Class CustomPrintPreviewDialog
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents printPreviewControl1 As System.Windows.Forms.PrintPreviewControl
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.printPreviewControl1 = New System.Windows.Forms.PrintPreviewControl()
        Me.SuspendLayout()
        "
        "printPreviewControl1
        "
        Me.printPreviewControl1.AutoZoom = False
        Me.printPreviewControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.printPreviewControl1.Name = "printPreviewControl1"
        Me.printPreviewControl1.Size = New System.Drawing.Size(376, 398)
        Me.printPreviewControl1.TabIndex = 1
        Me.printPreviewControl1.UseAntiAlias = True
        Me.printPreviewControl1.Zoom = 0.30000001192092896
        "
        "CustomPrintPreviewDialog
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(376, 398)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.printPreviewControl1})
        Me.Name = "CustomPrintPreviewDialog"
        Me.Text = "CustomPrintPreviewDialog"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub printPreviewControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles printPreviewControl1.Click
        If (Control.ModifierKeys And Keys.Shift) = 0 Then
            printPreviewControl1.Zoom = printPreviewControl1.Zoom * 2.0
        Else
            printPreviewControl1.Zoom = printPreviewControl1.Zoom / 2.0
        End If
    End Sub
    Public Property Document() As PrintDocument
        Get
            Return printPreviewControl1.Document
        End Get
        Set(ByVal Value As PrintDocument)
            printPreviewControl1.Document = Value
        End Set
    End Property
End Class