.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
[原创]打印richtextbox的解决方案

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

在实际应用中,我们经常会遇到要打印文本框的情况,自行换行是一个必须解决的首要问题,最初我也不太懂MeasureString这个函数,走了许多弯路,用正则表达式进行单字匹配,执行的效率低效果又不太理想。
后来发贴求解,有一熟手提醒,于是自己写下代码,供后来者学习研究。
'============================================
'Power by:landlordh
'Print richtextbox or mulitline textbox
'============================================

    Private intcurrentchar As Integer

    Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Pdoc.PrintPage
        Try
            Dim g As Graphics
            g = e.Graphics
            Dim pen As New Drawing.Pen(Color.Black, 3)
            Dim font As New Font("Arial", 9)
            Dim font1 As New Font("Arial", 12)
            Dim font2 As New Font("Arial", 10)
            Dim intPrintAreaHeight As Integer
            Dim intPrintAreaWidth As Integer
            intPrintAreaHeight = Pdoc.DefaultPageSettings.PaperSize.Height - Pdoc.DefaultPageSettings.Margins.Top - Pdoc.DefaultPageSettings.Margins.Bottom
            intPrintAreaWidth = Pdoc.DefaultPageSettings.PaperSize.Width - Pdoc.DefaultPageSettings.Margins.Left - Pdoc.DefaultPageSettings.Margins.Right
            If Pdoc.DefaultPageSettings.Landscape Then
                Dim intTemp As Integer = intPrintAreaHeight
                intPrintAreaHeight = intPrintAreaWidth
                intPrintAreaWidth = intTemp
            End If
           'Dim intLineCount As Integer = CType((intPrintAreaHeight / font.Height), Integer)
            Dim rectPrintingArea As RectangleF = New RectangleF(Pdoc.DefaultPageSettings.Margins.Left, Pdoc.DefaultPageSettings.Margins.Top, intPrintAreaWidth, intPrintAreaHeight)
            Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
            Dim intLinesFilled As Integer
            Dim intCharsFitted As Integer

            'draw page title
            g.DrawString("主题:" & topic.Text, font1, Brushes.Black, 0, 10)
            g.DrawLine(pen, 0, 12 + font1.Height, intPrintAreaWidth, 17 + font1.Height)
            g.MeasureString(word.Text.Substring(intcurrentchar), font, New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
            g.DrawString(word.Text.Substring(intcurrentchar), font, Brushes.Black, rectPrintingArea)
            intcurrentchar += intCharsFitted
            intLineCount += intLinesFilled
            g.DrawLine(pen, 0, 12 + font1.Height + intPrintAreaHeight + 6, intPrintAreaWidth, 12 + font1.Height + intPrintAreaHeight + 11)
            g.DrawString("Power by:LandlordAspx", font2, Brushes.Black, 625, 12 + font1.Height + intPrintAreaHeight + 12)
            'g.DrawString("共(" & Pdoc.PrinterSettings.ToPage & ")页", font2, Brushes.Black, 320, 12 + font1.Height + intPrintAreaHeight + 12)
            If intcurrentchar < (word.Text.Length - 1) Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
                intcurrentchar = 0
            End If
        Catch ex As Exception
            MessageBox.Show("系统执行时发生以下错误:" & Chr(10) & "错误源:" & ex.Source.ToString & Chr(10) & "错误信息:" & ex.Message.ToString & Chr(10) & Chr(10) & "请记录错误信息,并与管理员联系。", "系统提示")
        End Try
    End Sub

    Private Sub Printing()
        Dim PrintDialog1 As New PrintDialog
        PrintDialog1.Document = Pdoc
        PrintDialog1.AllowSelection = True
        PrintDialog1.AllowPrintToFile = True
        PrintDialog1.AllowSomePages = True
        PrintDialog1.PrintToFile = False

        If PrintDialog1.ShowDialog() = DialogResult.OK Then
            Pdoc.Print()
        End If
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Trim(word.Text) <> "" Then
            Try
                Pdoc.DocumentName = "LandlordAspx RichTextBox打印"
                Pdoc.DefaultPageSettings.Margins.Top = 34
                Pdoc.DefaultPageSettings.Margins.Bottom = 70
                Pdoc.DefaultPageSettings.Margins.Left = 0
                Pdoc.DefaultPageSettings.Margins.Right = 18
                Printing()
            Catch ex As Exception
            End Try
        End If
    End Sub




相关文章

相关软件