NET中打印包含有格式的 RichTextBox 的内容

打印

概要
本文逐步说明如何打印 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”的内容,请单击“打印”。

时间: 2024-07-30 13:33:23

NET中打印包含有格式的 RichTextBox 的内容的相关文章

Excel 2007中正确显示货币格式的数字

对于要在Excel中显示表示的货币的数值,怎样才能正确显示最标准的样式呢?请看本文介绍-- 1.选择包含要用货币符号显示的数字的单元格.         2.在"开始"选项卡上,单击"数字"旁边的"对话框启动器". 3.在"分类"框中,单击"货币"或"会计专用". 4.在"货币符号"框中,单击所需的货币符号. 注释 如果要显示没有货币符号的货币值,可以单击"

在Word 2003中打印多节文档一系列页

在Word中,您可以创建多节文档,每节都具有不同的页码编排.要指定一页或一系列页以便打印,您必须提供要打印的一系列页的页码和节号.在包含多个第 1 页的多节文档中,如果不同时提供节号,Word 将无法确定要打印哪个"第 1 页". 要打印分布在多个节中的一系列页,可以在"页码"框中使用以下语法: 页码节号-页码节号 例如,您可以键入 p1s1-p2s2 以打印第 1 节的第 1 页至第 2 节的第 2 页. 要在 Word 中打印一页或一系列页,请按照下列步骤操作:

在Word 2003中打印多节文档的一系列页

在 Word 中,您可以创建多节文档,每节都具有不同的页码编排.要指定一页或一系列页以便打印,您必须提供要打印的一系列页的页码和节号.在包含多个第 1 页的多节文档中,如果不同时提供节号,Word 将无法确定要打印哪个"第 1 页". 要打印分布在多个节中的一系列页,可以在"页码"框中使用以下语法: 页码节号-页码节号 例如,您可以键入 p1s1-p2s2 以打印第 1 节的第 1 页至第 2 节的第 2 页. 要在 Word 中打印一页或一系列页,请按照下列步骤操

php中时间戳和日期格式的转换

原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2009-1-22″) 结果:1232553600 说明:返回2009年1月22日0点0分0秒时间戳 二,PHP时间戳函数获取英文文本日期时间 示例如下: 便于比较,使用date将当时间戳与指定时间戳转换成系统时间 (1)打印明天此时的时间戳strtotime("+1 day") 当前时间:

php strstr查找字符串中是否包含某些字符的查找函数_php基础

PHP 判断字符串是否包含其它字符 以下几个函数均可用来判断某字符串是否包含另外一个字符串PHP 中判断一个字符串是否包含其它字符是很常见的操作. 虽然很简单,但还是写了几个函数,质量可能不是很高,权当锻炼. 如果这几个函数恰好能帮上你的忙,我将会很高兴的.这几个函数中,我比较喜欢第四个... 复制代码 代码如下: <?php /** * 以下几个函数均可用来判断某字符串是否包含另外一个字符串 * PHP 中判断一个字符串是否包含其它字符是很常见的操作. * 虽然很简单,但还是写了几个函数,质量

http响应头-如果某个网页的HTTP响应消息头中没有包含Content-Length,该如何判断流已读完而不被阻塞

问题描述 如果某个网页的HTTP响应消息头中没有包含Content-Length,该如何判断流已读完而不被阻塞 如题目所言,我有一个需求,需要模拟浏览器向指定URL交互,但是在取出内容时出现了问题,大多数网页都能正常抓取,因为响应头中包含了Content-Length:当遇到响应头中没有Content-Length时,代码就跑不下去了.如果一直使用readLine()方法一直读下去,如何判断流已读取到最末尾并不发生阻塞?跪求大神指点. public static void main(String

水晶报表如何在一页纸中打印相似的两个报表

问题描述 我已经做了一个报表,里面在页眉,详细资料,页脚中都有数据.报表的内容比较短,一般只占A4纸的一半长.现在要在一张纸中打印2张这样的报表.有两种情况,一个是这两个报表完成一样.另外一个是两张报表只是页眉上一张标"正联",一张标"副联".其他内容一样.各位有什么好方法.用的是VS2003自带的水晶报表 解决方案 解决方案二:有两种方法可以解觉你的问题:1.你可以把你已经做的报表改了成另外一种格式,把你所有的对象都放在说细资料节中设详细资料节中每页只显示两条记录

为何Windows 7中未包含 Windows Calendar

您可能已注意到 Windows 7 中未包含 Windows Calendar.但您可以通过安装其他公司提供的日历程序或使用 Windows Live(适用于http://www.aliyun.com/zixun/aggregation/18521.html">协同工作的计算机和联机服务的程序)管理日历和活动. 现在,Windows Live Mail 包含帮助您管理日历.电子邮件帐户和通讯录等的功能.Windows Live Mail 可能已安装在您的计算机上.若要查看是否已安装 Win

Python检测字符串中是否包含某字符集合中的字符

  这篇文章主要介绍了Python检测字符串中是否包含某字符集合中的字符,需要的朋友可以参考下 目的 检测字符串中是否包含某字符集合中的字符 方法 最简洁的方法如下,清晰,通用,快速,适用于任何序列和容器 代码如下: def containAny(seq,aset): for c in seq: if c in aset: return True return False 第二种适用itertools模块来可以提高一点性能,本质上与前者是同种方法(不过此方法违背了Python的核心观点:简洁,清