NET中打印包含有格式的 RichTextBox 的内容
2024-07-21 02:25:12
供稿:网友
,欢迎访问网页设计爱好者web开发。概要
本文逐步说明如何打印 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”的内容,请单击“打印”。