65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Silverlight 中使用 VB.NET 从带有分页的数据网格打印数据

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.64/5 (7投票s)

2011年8月5日

CPOL

2分钟阅读

viewsIcon

36868

downloadIcon

1821

本示例演示如何在 VB .NET 中打印和显示打印预览。它展示了如何使用 `PrintDocument` 对象进行打印,如何使用 `PrintDialog` 控件进行打印,以及如何使用 `PrintPreviewDialog` 显示打印预览。

引言

本示例演示如何在 VB.NET 中打印和显示打印预览。它展示了如何使用 `PrintDocument` 对象进行打印,如何使用 `PrintDialog` 控件进行打印,以及如何使用 `PrintPreviewDialog` 显示打印预览。

为了打印文档或提供预览,程序会创建一个 `PrintDocument` 对象。它将事件处理程序分配给该对象的 `BeginPrint`、`QueryPageSettings`、`PrintPage` 和 `EndPrint` 事件。`PrintPage` 是生成输出的事件,也是唯一必需的事件。在这个程序中,子程序 `PreparePrintDocument` 创建 `PrintDocument` 并设置其 `PrintPage` 事件处理程序。

事件处理程序在其作为参数传递的 `Graphics` 对象上进行绘制。它使用 `e.MarginBounds` 参数来了解页面上的页边距位置。最后,它将 `e.HasMorePages` 设置为 `False`,以指示打印输出已完成。

' Make and return a PrintDocument object that's ready
' to print the paragraphs.
Private Function PreparePrintDocument() As PrintDocument
    ' Make the PrintDocument object.
    Dim print_document As New PrintDocument

    ' Install the PrintPage event handler.
    AddHandler print_document.PrintPage, AddressOf _
        Print_PrintPage

    ' Return the object.
    Return print_document
End Function

' Print the next page.
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
    As System.Drawing.Printing.PrintPageEventArgs)
    ' Draw a rectangle at the margins.
    e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

    ' Draw a thick, dashed ellipse.
    Dim dotted_pen As New Pen(Color.Black, 5)
    dotted_pen.DashStyle = Drawing2D.DashStyle.Dash
    e.Graphics.DrawEllipse(dotted_pen, e.MarginBounds)
    dotted_pen.Dispose()

    ' Draw a thick diamond.
    Dim x0 As Integer = e.MarginBounds.X
    Dim y0 As Integer = e.MarginBounds.Y
    Dim wid As Integer = e.MarginBounds.Width
    Dim hgt As Integer = e.MarginBounds.Height
    Dim pts() As Point = { _
        New Point(x0, y0 + hgt \ 2), _
        New Point(x0 + wid \ 2, y0), _
        New Point(x0 + wid, y0 + hgt \ 2), _
        New Point(x0 + wid \ 2, y0 + hgt) _
    }
    e.Graphics.DrawPolygon(New Pen(Color.Black, 5), pts)

    ' There are no more pages.
    e.HasMorePages = False
End Sub

要显示打印预览,程序使用 `PreparePrintDocument` 函数创建一个 `PrintDocument` 对象,并将结果保存在 `PrintPreviewDialog` 的 `Document` 属性中。它调用对话框的 `ShowDialog` 方法,其余操作将自动完成。用户可以使用对话框放大和缩小,检查打印输出的不同页面(在这个程序中,打印输出只有一页),并打印文档。

' Display a print preview.
Private Sub btnPrintPreview_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintPreview.Click
    ' Make a PrintDocument and attach it to 
    ' the PrintPreview dialog.
    dlgPrintPreview.Document = PreparePrintDocument()

    ' Preview.
    dlgPrintPreview.WindowState = FormWindowState.Maximized
    dlgPrintPreview.ShowDialog()
End Sub

要显示打印对话框,程序使用 `PreparePrintDocument` 函数创建一个 `PrintDocument` 对象,并将结果保存在 `PrintDialog` 的 `Document` 属性中。它调用对话框的 `ShowDialog` 方法,其余操作将自动完成。用户可以使用对话框选择打印机并更改打印机的设置,然后启动或取消打印输出。

' Print with the print dialog.
Private Sub btnPrintWithDialog_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintWithDialog.Click
    ' Make a PrintDocument and attach it to 
    ' the Print dialog.
    dlgPrint.Document = PreparePrintDocument()

    ' Display the print dialog.
    dlgPrint.ShowDialog()
End Sub

要立即将文档打印到当前选定的打印机,程序使用 `PreparePrintDocument` 函数创建一个 `PrintDocument` 对象,并调用该对象的 `Print` 方法。

' Print immediately.
Private Sub btnPrintNow_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintNow.Click
    ' Make a PrintDocument object.
    Dim print_document As PrintDocument = _
        PreparePrintDocument()

    ' Print immediately.
    print_document.Print()
End Sub

历史

  • 2011 年 8 月 4 日:初始版本
© . All rights reserved.