除了打印服务之外,Windows 窗体编程中也经常提供打印预览。 向应用程序添加打印预览的一种简单方法是将 PrintPreviewDialog 控件与 PrintPage 用于打印文件的事件处理逻辑结合使用。
使用 PrintPreviewDialog 控件预览文本文档
在 Visual Studio 中,使用 “解决方案资源管理器” 窗格,然后双击要打印的窗体。 这将打开视觉设计器。
在 “工具箱 ”窗格中,双击 PrintDocument 组件和 PrintPreviewDialog 组件,将其添加到窗体。
将
Button
添加到窗体,或使用窗体上已有的按钮。在窗体的可视化设计器中,选择该按钮。 在 属性 窗格中,选择 事件 筛选器按钮,然后双击
Click
事件以生成事件处理程序。Click
事件代码应可见。 在事件处理程序的作用域外,将两个私有字符串变量添加到名为documentContents
的类中:stringToPrint
。// Declare a string to hold the entire document contents. private string documentContents=""; // Declare a variable to hold the portion of the document that // is not printed. private string stringToPrint="";
' Declare a string to hold the entire document contents. Private documentContents As String ' Declare a variable to hold the portion of the document that ' is not printed. Private stringToPrint As String
返回
Click
事件处理程序代码,将 DocumentName 属性设置为要打印的文档,然后将文档的内容打开并读取到之前添加的字符串。string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath);
Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath)
在PrintPage事件处理程序中,就像在打印文档时一样,使用PrintPageEventArgs类的Graphics属性和文件内容来计算每页的行数并呈现文档的内容。 绘制每个页面后,检查它是否是最后一页,并相应地设置 HasMorePages 属性
PrintPageEventArgs
。 引发PrintPage
事件,直到HasMorePages
为false
。 文档完成呈现后,重置要呈现的字符串。 此外,请确保事件PrintPage
与其事件处理方法相关联。注释
如果在应用程序中实现了打印,步骤 5 和 6 可能已经完成。
在下面的代码示例中,事件处理程序用于以窗体上所用的同一字体打印“testPage.txt”文件。
void PrintDocument1_PrintPage(object sender, PrintPageEventArgs e) { int charactersOnPage = 0; int linesPerPage = 0; // Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page. e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic); // Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage); // Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); // If there are no more pages, reset the string to be printed. if (!e.HasMorePages) stringToPrint = documentContents; }
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page. e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) ' Remove the portion of the string that has been printed. stringToPrint = StringToPrint.Substring(charactersOnPage) ' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 ' If there are no more pages, reset the string to be printed. If Not e.HasMorePages Then stringToPrint = documentContents End If End Sub
将表单中的 PrintPreviewDialog 控件的 Document 属性设定为 PrintDocument 组件。
printPreviewDialog1.Document = printDocument1;
PrintPreviewDialog1.Document = PrintDocument1
在 PrintPreviewDialog 控件上调用 ShowDialog 方法。 请注意以下突出显示的代码,通常您会在按钮的事件处理方法中调用ShowDialog。 调用 ShowDialog 将 PrintPage 引发事件并将输出呈现给
PrintPreviewDialog
控件。 当用户选择对话框上的打印图标时,PrintPage
将再次引发该事件,将输出发送到打印机而不是预览对话框。 因此,字符串在步骤 4 的渲染过程结束时被重置。下面的代码示例显示了 Click 窗体上按钮的事件处理方法。 事件处理方法调用方法来读取文档并显示打印预览对话框。
private void Button1_Click(object sender, EventArgs e) { string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath) PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub