Print overview
This step-by-step article describes how to print the contents of a RichTextBox control. The RichTextBox control does not provide a way to print its contents. However, you can extend the RichTextBox class to use Em_formatrange messages. You can then send RichTextBox content to an output device, such as a printer.
Creating RichTextBoxPrintCtrl Controls
To extend the RichTextBox class and use Em_formatrange to print the contents of the RichTextBox control, follow these steps: 1. Use Microsoft Visual Basic. NET to create a new class library project named RichTextBoxPrintCtrl.
By default, Class1.vb is created.
2. Change the name of the Class1.vb file to RichTextBoxPrintCtrl.vb.
3. In Solution Explorer, right-click References, and then click Add Reference.
4. In the Add Reference dialog box, double-click System.Drawing.dll, and then double-click System.Windows.Forms.dll.
5. To add a reference, click OK.
6. Delete the existing node in "RichTextBoxPrintCtrl.vb".
7. Copy the following code into "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's used by the. NET Framework (1/100 inch)
' And the ' 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 ' a ' 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 to the whole DC (page size) (in twips)
Public Chrg as Charrange ' Range of text to draw (for the 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 to the last character printed + 1 (printing start to 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 of the same DC for measuring and rendering
Fmtrange.hdctarget = HDC ' point at printer hdc
fmtrange.rc = rectToPrint ' indicate the area of 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 of the device context handle obtained by a previous call
E.GRAPHICS.RELEASEHDC (HDC)
' Return to last + 1 character printer
return Res. ToInt32 ()
End Function
End Class
End Namespace
8. To create "RichTextBoxPrintCtrl.dll", on the Build menu, click Build Solution.
Test control
To test the control, follow these steps: 1. Create a new Windows application project using Visual Basic. NET.
By default, Form1.vb is created.
2. From the Toolbox, drag a button onto the Form1. Change the name to Btnpagesetup, and then changes text to page setup.
3. From the Toolbox, drag another button onto the Form1. Change the name to Btnprintpreview and the text to print preview.
4. From the Toolbox, drag another button onto the Form1. Change the name to Btnprint and the text to print.
5. In the Toolbox, double-click PrintDialog, PrintPreviewDialog, and PrintDocument, and then double-click PageSetupDialog to add the controls to the Form1.
6. Modify the Document property of "PrintDialog1", "PrintPreviewDialog1" and "PageSetupDialog1" to PrintDocument1.
7. On the Tools menu, click Customize Toolbox.
8. Click. NET Framework components, click Browse, click to select RichTextBoxPrintCtrl.dll, and then click OK.
9. From the Toolbox, drag "RichTextBoxPrintCtrl" onto the Form1.
10. In Solution Explorer, right-click Form1.vb, and then click View Code.
11. Add the following code to the Form1 class:
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.Cli Ck
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.CLI Ck
Printpreviewdialog1.showdialog ()
End Sub
12. To run the application, on the Debug menu, click Start.
13. Type the text in "RichTextBoxPrintCtrl".
14. To set the page settings, click Page Setup.
15. To preview the page, click Print Preview.
16. To print the contents of "RichTextBoxPrintCtrl", click "Print".
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.