[VB.NET]打印和打印预览功能

打印和打印预览功能

实例说明

在本实例中,我们将制作一个能实现打印和打印预览功能的应用程序。程序运行结果如图60-1所示。

图60-1 运行结果

技术要点

l 打印预览功能的实现

l 打印功能的实现

实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"PrintExample",然后选择保存路径。单击"确认"。

■ 添加控件和设置属性

向当前窗体上添加三个Button控件,将他们的Text属性改为和界面一致。

■ 添加组件类和要打印的文件

通过菜单"项目|添加组件"为当前项目添加一个组件类,并添加一个需要打印的文件。

■ 添加代码

'组件类中的代码

Imports System

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Drawing

Imports System.Drawing.Printing

Imports System.IO

Namespace Microsoft.Samples.WinForms.VB.PrintingExample5

Public Class TextFilePrintDocument

Inherits PrintDocument

Private printFont As Font

Private streamToPrint As StreamReader

Public Sub New(streamToPrint As StreamReader)

MyBase.New

Me.streamToPrint = streamToPrint

End Sub

'Override OnBeginPrint to set up the font we are going to use

Overrides Protected Sub OnBeginPrint(ev As PrintEventArgs)

MyBase.OnBeginPrint(ev)

printFont = new Font("Arial", 10)

End Sub

'Override the OnPrintPage to provide the printing logic for the document

Overrides Protected Sub OnPrintPage(ev As PrintPageEventArgs)

MyBase.OnPrintPage(ev)

Dim lpp As Single = 0

Dim yPos As Single = 0

Dim count As Integer = 0

Dim leftMargin As Single = ev.MarginBounds.Left

Dim topMargin As Single = ev.MarginBounds.Top

Dim line as String

'Work out the number of lines per page

'Use the MarginBounds on the event to do this

lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

'Check count first so that we don't read line that we won't print

line=streamToPrint.ReadLine()

While ((count < lpp) And Not(line Is Nothing))

yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))

ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat())

count = count + 1

if (count < lpp) then

line=streamToPrint.ReadLine()

end if

End While

If (line <> Nothing) Then

ev.HasMorePages = True

Else

ev.HasMorePages = False

End If

End Sub

End Class

End Namespace

'主窗体中的代码

Imports System

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Drawing

Imports System.Drawing.Printing

Imports System.IO

Namespace Microsoft.Samples.WinForms.VB.PrintingExample5

Public Class PrintForm

Inherits System.Windows.Forms.Form

Private storedPageSettings As PageSettings

Public Sub New ()

MyBase.New

PrintForm = Me

InitializeComponent()

AddHandler printButton.Click, AddressOf printButton_Click

AddHandler pageSetupButton.Click, AddressOf pageSetupButton_Click

AddHandler printPreviewButton.Click, AddressOf printPreviewButton_Click

End Sub

'页面设置

Private Sub pageSetupButton_Click(sender As object, e As System.EventArgs)

Try

Dim psDlg As New PageSetupDialog

If (storedPageSettings Is Nothing) Then

storedPageSettings = new PageSettings()

End If

psDlg.PageSettings = storedPageSettings

psDlg.ShowDialog

Catch ex As Exception

MessageBox.Show("An error occurred - " + ex.Message)

End Try

End Sub

'开始打印

Private Sub printButton_Click(sender As object, e As System.EventArgs)

Try

Dim streamToPrint As StreamReader = new StreamReader ("PrintMe.Txt")

Try

'使用缺省打印机

Dim pd As TextFilePrintDocument = new TextFilePrintDocument(streamToPrint)

If Not (storedPageSettings Is Nothing) Then

pd.DefaultPageSettings = storedPageSettings

End If

Dim dlg As New PrintDialog()

dlg.Document = pd

Dim result As DialogResult = dlg.ShowDialog()

If (result = System.Windows.Forms.DialogResult.OK) Then

pd.Print()

End If

Finally

streamToPrint.Close()

End Try

Catch ex As Exception

MessageBox.Show("An error occurred printing the file - " + ex.Message)

End Try

End Sub

'打印预览

Private Sub printPreviewButton_Click(sender As object, e As System.EventArgs)

Try

Dim streamToPrint As StreamReader = new StreamReader ("PrintMe.Txt")

Try

Dim pd As TextFilePrintDocument = new TextFilePrintDocument(streamToPrint)

If Not (storedPageSettings Is Nothing) Then

pd.DefaultPageSettings = storedPageSettings

End If

Dim dlg As New PrintPreviewDialog()

dlg.Document = pd

dlg.ShowDialog()

Finally

streamToPrint.Close()

End Try

Catch ex As Exception

MessageBox.Show("An error occurred - " + ex.Message)

End Try

End Sub

Public Overloads Overrides Sub Dispose()

MyBase.Dispose()

components.Dispose()

End Sub

Shared Sub Main()

System.Windows.Forms.Application.Run(New PrintForm())

End Sub

End Class

End Namespace

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

小结

在打印和打印预览时,我们可以使用PrintPreviewDialog,PrintPreviewControl控件和PrintDocument控件来实现。

你可能感兴趣的:([VB.NET])