Here you go:
////////////////////////////////////////////////////////////////////////////////////////////////
//
// CopyRight 2009, www.perceler.com, Luc Pattyn
//
// Version 1.0
//
// use freely for non-commercial use as long as this notice remains present;
// ask me before using commercially.
//
////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace LP_Core {
/// /// LP\_RichTextBox is an extended RichTextBox offering Print functionality (courtesy Microsoft)
/// and a proprietary method to draw all the content to a bitmap
///
/// "http://support.microsoft.com/kb/812425"
\[System.ComponentModel.DesignerCategory("Code")\]
class LP\_RichTextBox : RichTextBox {
//Convert the unit used by the .NET framework (1/100 inch)
//and the unit used by Win32 API calls (twips 1/1440 inch)
private const double anInch = 14.4;
\[StructLayout(LayoutKind.Sequential)\]
private struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
\[StructLayout(LayoutKind.Sequential)\]
private struct CHARRANGE {
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
\[StructLayout(LayoutKind.Sequential)\]
private struct FORMATRANGE {
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM\_USER = 0x0400;
private const int EM\_FORMATRANGE = WM\_USER + 57;
\[DllImport("USER32.dll")\]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
// Render the contents of the RichTextBox for printing
// Return the last character printed + 1 (printing start from this point for next page)
// WARNING: I have not tested this method, I only used it as a starting point
// to create the next method!
public int Print(int charFrom, int charTo, PrintPageEventArgs e) {
//Calculate the area to render and print
RECT rect