.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开发
[转贴].NET中打印包含有格式的 RichTextBox 的内容

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

HOW TO:使用 Microsoft Visual Basic .NET 打印 RichTextBox 的内容

文章 ID : 811401
最后更新日期 : 2004年1月12日
版本 : 3.0
有关本文的 Microsoft Visual Basic 6.0 版本,请参阅 146022.
本文逐步说明如何打印 RichTextBox 控件的内容。RichTextBox 控件不提供打印其内容的方法。但是,您可以扩展 RichTextBox 类以使用 EM_FORMATRANGE 消息。然后,您可以将 RichTextBox 的内容发送到某个输出设备,例如打印机。

创建 RichTextBoxPrintCtrl 控件

要扩展 RichTextBox 类并使用 EM_FORMATRANGE 来打印 RichTextBox 控件的内容,请按照下列步骤操作:
1. 使用 Microsoft Visual Basic .NET 新建一个名为 RichTextBoxPrintCtrl 的类库项目。

默认情况下,将创建 Class1.vb。
2. 将 Class1.vb 文件的名称更改为 RichTextBoxPrintCtrl.vb
3. 在解决方案资源管理器中,右键单击“引用”,然后单击“添加引用”。
4. 添加引用对话框中,双击“System.Drawing.dll”,然后双击“System.Windows.Forms.dll”。
5. 要添加引用,请单击“确定”。
6. 删除“RichTextBoxPrintCtrl.vb”中的现有节点。
7. 将以下代码复制到“RichTextBoxPrintCtrl.vb”中:
Option Explicit On

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing

Namespace RichTextBoxPrintCtrl
   Public Class RichTextBoxPrintCtrl
      Inherits RichTextBox
      ' Convert the unit that is used by the .NET framework (1/100 inch)
      ' and the unit that is used by Win32 API calls (twips 1/1440 inch)
      Private Const AnInch As Double = 14.4

      <StructLayout(LayoutKind.Sequential)> _
       Private Structure RECT
         Public Left As Integer
         Public Top As Integer
         Public Right As Integer
         Public Bottom As Integer
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Private Structure CHARRANGE
         Public cpMin As Integer          ' First character of range (0 for start of doc)
         Public cpMax As Integer          ' Last character of range (-1 for end of doc)
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Private Structure FORMATRANGE
         Public hdc As IntPtr             ' Actual DC to draw on
         Public hdcTarget As IntPtr       ' Target DC for determining text formatting
         Public rc As Rect                ' Region of the DC to draw to (in twips)
         Public rcPage As Rect            ' Region of the whole DC (page size) (in twips)
         Public chrg As CHARRANGE         ' Range of text to draw (see above declaration)
      End Structure

      Private Const WM_USER As Integer = &H400
      Private Const EM_FORMATRANGE As Integer = WM_USER + 57

      Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr

      ' Render the contents of the RichTextBox for printing
      '	Return the last character printed + 1 (printing start from this point for next page)
      Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer

         ' Mark starting and ending character
         Dim cRange As CHARRANGE
         cRange.cpMin = charFrom
         cRange.cpMax = charTo

         ' Calculate the area to render and print
         Dim rectToPrint As RECT
         rectToPrint.Top = e.MarginBounds.Top * AnInch
         rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
         rectToPrint.Left = e.MarginBounds.Left * AnInch
         rectToPrint.Right = e.MarginBounds.Right * AnInch

         ' Calculate the size of the page
         Dim rectPage As RECT
         rectPage.Top = e.PageBounds.Top * AnInch
         rectPage.Bottom = e.PageBounds.Bottom * AnInch
         rectPage.Left = e.PageBounds.Left * AnInch
         rectPage.Right = e.PageBounds.Right * AnInch

         Dim hdc As IntPtr = e.Graphics.GetHdc()

         Dim fmtRange As FORMATRANGE
         fmtRange.chrg = cRange                 ' Indicate character from to character to
         fmtRange.hdc = hdc                     ' Use the same DC for measuring and rendering
         fmtRange.hdcTarget = hdc               ' Point at printer hDC
         fmtRange.rc = rectToPrint              ' Indicate the area on page to print
         fmtRange.rcPage = rectPage             ' Indicate whole size of page

         Dim res As IntPtr = IntPtr.Zero

         Dim wparam As IntPtr = IntPtr.Zero
         wparam = New IntPtr(1)

         ' Move the pointer to the FORMATRANGE structure in memory
         Dim lparam As IntPtr = IntPtr.Zero
         lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
         Marshal.StructureToPtr(fmtRange, lparam, False)

         ' Send the rendered data for printing
         res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)

         ' Free the block of memory allocated
         Marshal.FreeCoTaskMem(lparam)

         ' Release the device context handle obtained by a previous call
         e.Graphics.ReleaseHdc(hdc)

         ' Return last + 1 character printer
         Return res.ToInt32()
      End Function

   End Class
End Namespace
8. 要创建“RichTextBoxPrintCtrl.dll”,请在“生成”菜单上单击“生成解决方案”。

测试控件

要测试该控件,请按照下列步骤操作:
1. 使用 Visual Basic .NET 新建一个 Windows 应用程序项目。

默认情况下,将创建 Form1.vb。
2. 从工具箱中,将一个按钮拖到 Form1 上。将名称更改为 btnPageSetup,然后将“文本”更改为页面设置
3. 从工具箱中,将另一个按钮拖到 Form1 上。将名称更改为 btnPrintPreview,然后将“文本”更改为打印预览
4. 从工具箱中,将另一个按钮拖到 Form1 上。将名称更改为 btnPrint,然后将“文本”更改为打印
5. 在工具箱中,依次双击“PrintDialog”、“PrintPreviewDialog”和“PrintDocument”,然后双击“PageSetupDialog”将这些控件添加到 Form1 中。
6. 将“PrintDialog1”、“PrintPreviewDialog1”和“PageSetupDialog1”的 Document 属性修改为PrintDocument1
7. 在“工具”菜单上,单击“自定义工具箱”。
8. 单击“.NET Framework 组件”,单击“浏览”,单击以选择“RichTextBoxPrintCtrl.dll”,然后单击“确定”。
9. 从工具箱中,将“RichTextBoxPrintCtrl”拖到 Form1 上。
10. 在解决方案资源管理器中,右键单击“Form1.vb”,然后单击“查看代码”。
11. 将以下代码添加到 Form1 类中:
   Private checkPrint As Integer

   Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
      checkPrint = 0
   End Sub

   Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      ' Print the content of the RichTextBox. Store the last character printed.
      checkPrint = RichTextBoxPrintCtrl1.Print(checkPrint, RichTextBoxPrintCtrl1.TextLength, e)

      ' Look for more pages
      If checkPrint < RichTextBoxPrintCtrl1.TextLength Then
         e.HasMorePages = True
      Else
         e.HasMorePages = False
      End If
   End Sub

   Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click.Click
      PageSetupDialog1.ShowDialog()
   End Sub

   Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
      If PrintDialog1.ShowDialog() = DialogResult.OK Then
         PrintDocument1.Print()
      End If
   End Sub

   Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
      PrintPreviewDialog1.ShowDialog()
   End Sub
12. 要运行该应用程序,请单击“调试”菜单上的“开始”。
13. 在“RichTextBoxPrintCtrl”中键入文本。
14. 要设置页面设置,请单击“页面设置”。
15. 要预览该页,请单击“打印预览”。
16. 要打印“RichTextBoxPrintCtrl”的内容,请单击“打印”。



相关文章

相关软件