您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

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

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

  nge 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_B

上一页  [1] [2] [3] 下一页

在google里搜索更多NET中打印包含有格式的 RichTextBox 的内容

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明