打印和打印预览功能  
发布时间: 2008-2-18 文章流量: 2855 次 文章来源: 本站
作品名称   打印和打印预览功能  
作品编号  
编程工具  
数 据 库  
论文字数   2000 字
运行环境  
论文价格   200 元
推荐指数   ☆☆☆☆☆
包含内容  
整理日期  
作品简介
实例说明
在本实例中,我们将制作一个能实现打印和打印预览功能的应用程序。程序运行结果如图60-1所示。
 
图60-1  运行结果
技术要点
 打印预览功能的实现
 打印功能的实现
实现过程
■ 新建项目
打开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控件来实现。
下载地址  
购买说明 计算机毕业设计完整作品。
包括源程序、可执行文件、开题报告、论文、英文文献和中文翻译。
请加客户服务联系 15303601058 联系QQ:380894045/279018395 EMAIL:CareF@CareF.CN 获取作品的详细资料。
相关链接  
上一篇: 连接数据库(一)
下一篇: MP3播放器
信息回复  
版权所有 Copyright 2005-2008 悠索科技 Inc. All Rights Reserved
联系QQ: 380894045 279018395 EMAIL:CareF@CareF.CN
黑ICP备06003839号 黑ICP备08000316号