国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

NET中打印包含有格式的 RichTextBox 的內(nèi)容

2024-07-21 02:25:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
,歡迎訪(fǎng)問(wèn)網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)。概要
本文逐步說(shuō)明如何打印 richtextbox 控件的內(nèi)容。richtextbox 控件不提供打印其內(nèi)容的方法。但是,您可以擴(kuò)展 richtextbox 類(lèi)以使用 em_formatrange 消息。然后,您可以將 richtextbox 的內(nèi)容發(fā)送到某個(gè)輸出設(shè)備,例如打印機(jī)。


創(chuàng)建 richtextboxprintctrl 控件
要擴(kuò)展 richtextbox 類(lèi)并使用 em_formatrange 來(lái)打印 richtextbox 控件的內(nèi)容,請(qǐng)按照下列步驟操作: 1. 使用 microsoft visual basic .net 新建一個(gè)名為 richtextboxprintctrl 的類(lèi)庫(kù)項(xiàng)目。

默認(rèn)情況下,將創(chuàng)建 class1.vb。
2. 將 class1.vb 文件的名稱(chēng)更改為 richtextboxprintctrl.vb。
3. 在解決方案資源管理器中,右鍵單擊“引用”,然后單擊“添加引用”。
4. 在添加引用對(duì)話(huà)框中,雙擊“system.drawing.dll”,然后雙擊“system.windows.forms.dll”。
5. 要添加引用,請(qǐng)單擊“確定”。
6. 刪除“richtextboxprintctrl.vb”中的現(xiàn)有節(jié)點(diǎn)。
7. 將以下代碼復(fù)制到“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. 要?jiǎng)?chuàng)建“richtextboxprintctrl.dll”,請(qǐng)?jiān)凇吧伞辈藛紊蠁螕簟吧山鉀Q方案”。

測(cè)試控件
要測(cè)試該控件,請(qǐng)按照下列步驟操作: 1. 使用 visual basic .net 新建一個(gè) windows 應(yīng)用程序項(xiàng)目。

默認(rèn)情況下,將創(chuàng)建 form1.vb。
2. 從工具箱中,將一個(gè)按鈕拖到 form1 上。將名稱(chēng)更改為 btnpagesetup,然后將“文本”更改為頁(yè)面設(shè)置。
3. 從工具箱中,將另一個(gè)按鈕拖到 form1 上。將名稱(chēng)更改為 btnprintpreview,然后將“文本”更改為打印預(yù)覽。
4. 從工具箱中,將另一個(gè)按鈕拖到 form1 上。將名稱(chēng)更改為 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 類(lèi)中:
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. 要運(yùn)行該應(yīng)用程序,請(qǐng)單擊“調(diào)試”菜單上的“開(kāi)始”。
13. 在“richtextboxprintctrl”中鍵入文本。
14. 要設(shè)置頁(yè)面設(shè)置,請(qǐng)單擊“頁(yè)面設(shè)置”。
15. 要預(yù)覽該頁(yè),請(qǐng)單擊“打印預(yù)覽”。
16. 要打印“richtextboxprintctrl”的內(nèi)容,請(qǐng)單擊“打印”。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 酉阳| 江永县| 金华市| 海宁市| 海伦市| 苏州市| 枣庄市| 太白县| 东丽区| 乐平市| 临朐县| 马鞍山市| 深泽县| 昆山市| 大化| 田东县| 左贡县| 澎湖县| 惠来县| 固阳县| 徐州市| 黔西县| 吴江市| 长汀县| 佛山市| 合江县| 大竹县| 五台县| 临西县| 怀仁县| 南召县| 肇州县| 徐闻县| 宜州市| 惠安县| 隆昌县| 临漳县| 包头市| 临漳县| 石河子市| 天台县|