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”的內容,請單擊“打印”。