打印和打印预览功能
今天,我们将制作一个能实现打印和打印预览功能的应用程序。程序运行结果如图所示。
  运行结果
技术要点
打印预览功能的实现
打印功能的实现
实现过程
新建项目
打开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()printform
            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()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。