NET中列印包含有格式的 RichTextBox 的內容

來源:互聯網
上載者:User
列印 概要
本文逐步說明如何列印 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”的內容,請單擊“列印”。


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。