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
1<structlayout(layoutkind.sequential)> _
2 Private Structure RECT
3 Public Left As Integer
4 Public Top As Integer
5 Public Right As Integer
6 Public Bottom As Integer
7 End Structure
8
9 <structlayout(layoutkind.sequential)> _
10 Private Structure CHARRANGE
11 Public cpMin As Integer ' First character of range (0 for start of doc)
12 Public cpMax As Integer ' Last character of range (-1 for end of doc)
13 End Structure
14
15 <structlayout(layoutkind.sequential)> _
16 Private Structure FORMATRANGE
17 Public hdc As IntPtr ' Actual DC to draw on
18 Public hdcTarget As IntPtr ' Target DC for determining text formatting
19 Public rc As Rect ' Region of the DC to draw to (in twips)
20 Public rcPage As Rect ' Region of the whole DC (page size) (in twips)
21 Public chrg As CHARRANGE ' Range of text to draw (see above declaration)
22 End Structure
23
24 Private Const WM_USER As Integer = &H400
25 Private Const EM_FORMATRANGE As Integer = WM_USER + 57
26
27 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
28
29 ' Render the contents of the RichTextBox for printing
30 ' Return the last character printed + 1 (printing start from this point for next page)
31 Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer
32
33 ' Mark starting and ending character
34 Dim cRange As CHARRANGE
35 cRange.cpMin = charFrom
36 cRange.cpMax = charTo
37
38 ' Calculate the area to render and print
39 Dim rectToPrint As RECT
40 rectToPrint.Top = e.MarginBounds.Top * AnInch
41 rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
42 rectToPrint.Left = e.MarginBounds.Left * AnInch
43 rectToPrint.Right = e.MarginBounds.Right * AnInch
44
45 ' Calculate the size of the page
46 Dim rectPage As RECT
47 rectPage.Top = e.PageBounds.Top * AnInch
48 rectPage.Bottom = e.PageBounds.Bottom * AnInch
49 rectPage.Left = e.PageBounds.Left * AnInch
50 rectPage.Right = e.PageBounds.Right * AnInch
51
52 Dim hdc As IntPtr = e.Graphics.GetHdc()
53
54 Dim fmtRange As FORMATRANGE
55 fmtRange.chrg = cRange ' Indicate character from to character to
56 fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
57 fmtRange.hdcTarget = hdc ' Point at printer hDC
58 fmtRange.rc = rectToPrint ' Indicate the area on page to print
59 fmtRange.rcPage = rectPage ' Indicate whole size of page
60
61 Dim res As IntPtr = IntPtr.Zero
62
63 Dim wparam As IntPtr = IntPtr.Zero
64 wparam = New IntPtr(1)
65
66 ' Move the pointer to the FORMATRANGE structure in memory
67 Dim lparam As IntPtr = IntPtr.Zero
68 lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
69 Marshal.StructureToPtr(fmtRange, lparam, False)
70
71 ' Send the rendered data for printing
72 res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)
73
74 ' Free the block of memory allocated
75 Marshal.FreeCoTaskMem(lparam)
76
77 ' Release the device context handle obtained by a previous call
78 e.Graphics.ReleaseHdc(hdc)
79
80 ' Return last + 1 character printer
81 Return res.ToInt32()
82 End Function
83
84 End Class
85 End Namespace
86
87
88`
898\. | 要创建“RichTextBoxPrintCtrl.dll”,请在“生成”菜单上单击“生成解决方案”。
90
91### 测试控件
92
93要测试该控件,请按照下列步骤操作: 1\. | 使用 Visual Basic .NET 新建一个 Windows 应用程序项目。
94
95默认情况下,将创建 Form1.vb。
96---|---
972\. | 从工具箱中,将一个按钮拖到 Form1 上。将名称更改为 btnPageSetup ,然后将“文本”更改为 页面设置 。
983\. | 从工具箱中,将另一个按钮拖到 Form1 上。将名称更改为 btnPrintPreview ,然后将“文本”更改为 打印预览 。
994\. | 从工具箱中,将另一个按钮拖到 Form1 上。将名称更改为 btnPrint ,然后将“文本”更改为 打印 。
1005\. | 在工具箱中,依次双击“PrintDialog”、“PrintPreviewDialog”和“PrintDocument”,然后双击“PageSetupDialog”将这些控件添加到 Form1 中。
1016\. | 将“PrintDialog1”、“PrintPreviewDialog1”和“PageSetupDialog1”的 **Document** 属性修改为 PrintDocument1 。
1027\. | 在“工具”菜单上,单击“自定义工具箱”。
1038\. | 单击“.NET Framework 组件”,单击“浏览”,单击以选择“RichTextBoxPrintCtrl.dll”,然后单击“确定”。
1049\. | 从工具箱中,将“RichTextBoxPrintCtrl”拖到 Form1 上。
10510\. | 在解决方案资源管理器中,右键单击“Form1.vb”,然后单击“查看代码”。
10611\. | 将以下代码添加到 Form1 类中: `
107
108
109 Private checkPrint As Integer
110
111 Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
112 checkPrint = 0
113 End Sub
114
115 Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
116 ' Print the content of the RichTextBox. Store the last character printed.
117 checkPrint = RichTextBoxPrintCtrl1.Print(checkPrint, RichTextBoxPrintCtrl1.TextLength, e)
118
119 ' Look for more pages
120 If checkPrint < RichTextBoxPrintCtrl1.TextLength Then
121 e.HasMorePages = True
122 Else
123 e.HasMorePages = False
124 End If
125 End Sub
126
127 Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click.Click
128 PageSetupDialog1.ShowDialog()
129 End Sub
130
131 Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
132 If PrintDialog1.ShowDialog() = DialogResult.OK Then
133 PrintDocument1.Print()
134 End If
135 End Sub
136
137 Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
138 PrintPreviewDialog1.ShowDialog()
139 End Sub
140
141`
14212\. | 要运行该应用程序,请单击“调试”菜单上的“开始”。
14313\. | 在“RichTextBoxPrintCtrl”中键入文本。
14414\. | 要设置页面设置,请单击“页面设置”。
14515\. | 要预览该页,请单击“打印预览”。
14616\. | 要打印“RichTextBoxPrintCtrl”的内容,请单击“打印”。</structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)>