internal RichEditOleInterface(RichTextBox rtb) { m_rich_edit_ole = IntPtr.Zero; m_text_document = IntPtr.Zero; // Allocate the ptr that EM_GETOLEINTERFACE will fill in. using (var buffer = Marshal_.Alloc(EHeap.CoTaskMem, typeof(IntPtr), 1)) { //Marshal.WriteIntPtr(ptr, IntPtr.Zero); // Clear it. if (Win32.SendMessage(rtb.Handle, Win32.EM_GETOLEINTERFACE, IntPtr.Zero, buffer.Value.Ptr) == IntPtr.Zero) { throw new Exception("RichTextBox.OleInterface - EM_GETOLEINTERFACE failed."); } // Read the returned pointer. using (var pRichEdit = Scope.Create(() => Marshal.ReadIntPtr(buffer.Value.Ptr), p => Marshal.Release(p))) { if (pRichEdit == IntPtr.Zero) { throw new Exception("RichTextBox.OleInterface - failed to get the pointer."); } { // Query for the IRichEditOle interface. var guid = new Guid("00020D00-0000-0000-c000-000000000046"); Marshal.QueryInterface(pRichEdit, ref guid, out m_rich_edit_ole); } { // Query for the ITextDocument interface var guid = new Guid("8CC497C0-A1DF-11CE-8098-00AA0047BE5D"); Marshal.QueryInterface(pRichEdit, ref guid, out m_text_document); } } } }
/// <summary> /// Render the contents of the RichTextBox for printing. /// Return the last character printed + 1 (printing start from this point for next page). /// http://support.microsoft.com/default.aspx?scid=kb;en-us;812425 /// The RichTextBox control does not provide any method to print the content of the RichTextBox. /// You can extend the RichTextBox class to use EM_FORMATRANGE message to send the content of /// a RichTextBox control to an output device such as printer.</summary> public static int Print(IntPtr rtb_handle, int charFrom, int charTo, PrintPageEventArgs e) { // Convert the unit used by the .NET framework (1/100 inch) // and the unit used by Win32 API calls ('twips' 1/1440 inch) const double anInch = 14.4; // Calculate the area to render and print var print_rect = Win32.RECT.FromLTRB( (int)(e.MarginBounds.Left * anInch), (int)(e.MarginBounds.Top * anInch), (int)(e.MarginBounds.Right * anInch), (int)(e.MarginBounds.Bottom * anInch)); // Calculate the size of the page var page_rect = Win32.RECT.FromLTRB( (int)(e.PageBounds.Left * anInch), (int)(e.PageBounds.Top * anInch), (int)(e.PageBounds.Right * anInch), (int)(e.PageBounds.Bottom * anInch)); // Create a format range using (var hdc = e.Graphics.GetHdcScope()) { var fmt_range = new Win32.FORMATRANGE { char_range = new Win32.CHARRANGE { max = charTo, // Indicate character from to character to min = charFrom, // }, hdc = hdc, // Use the same DC for measuring and rendering hdcTarget = hdc, // Point at printer hDC rc = print_rect, // Indicate the area on page to print rcPage = page_rect, // Indicate size of page }; // Get the pointer to the FORMATRANGE structure in non-GC memory using (var buffer = Marshal_.Alloc <Win32.FORMATRANGE>(EHeap.CoTaskMem)) { Marshal.StructureToPtr(fmt_range, buffer.Value.Ptr, false); // Send the rendered data for printing, then release and cached info var res = Win32.SendMessage(rtb_handle, (uint)Win32.EM_FORMATRANGE, new IntPtr(1), buffer.Value.Ptr); Win32.SendMessage(rtb_handle, (uint)Win32.EM_FORMATRANGE, (IntPtr)0, (IntPtr)0); return((int)res); // Return last + 1 character printed } } }