public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wp, ref FORMATRANGE lp);
// send the EM_FORMATRANGE message to the RichTextBox to render or measure // a range of the document into a target specified by a FORMATRANGE structure. int FormatRange(RichTextBox rtb, bool render, ref FORMATRANGE fmt) { // render int nextChar = SendMessageFormatRange( rtb.Handle, EM_FORMATRANGE, render ? 1 : 0, ref fmt); // reset SendMessage(rtb.Handle, EM_FORMATRANGE, 0, 0); // return next character to print return nextChar; }
// build a FORMATRANGE structure with the proper page size and hdc // (the hdc must be released after the FORMATRANGE is used) FORMATRANGE GetFormatRange(PrintPageEventArgs e, int firstChar) { // get page rectangle in twips var rc = e.MarginBounds; rc.X = (int)(rc.X * 14.4 + .5); rc.Y = (int)(rc.Y * 14.4 + .5); rc.Width = (int)(rc.Width * 14.4 + .5); rc.Height = (int)(rc.Height * 14.40 + .5); // set up FORMATRANGE structure var hdc = e.Graphics.GetHdc(); var fmt = new FORMATRANGE(); fmt.hdc = fmt.hdcTarget = hdc; fmt.rc.SetRect(rc); fmt.rcPage = fmt.rc; fmt.cpMin = firstChar; fmt.cpMax = -1; // done return fmt; }
static extern int SendMessageFormatRange(IntPtr hWnd, uint wMsg, int wParam, ref FORMATRANGE lParam);