Задача: необходимо выводить на печать содержимое RichTextBox.
Поиски в сети помогли сотворить следующее решение:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace RichTextBoxPrint
{
/// <summary>
/// Класс, добавляющий возможность печати из RichTextBox
/// </summary>
internal static class RichTextBoxPrint
{
public static void Print(this RichTextBox rtf, PrintDocument prn)
{
new PrintRtf(rtf, prn);
}
public static void Print(this RichTextBox rtf)
{
Print(rtf, new PrintDocument());
}
private class PrintRtf
{
public PrintRtf(RichTextBox rtf, PrintDocument prn)
{
Rtf = rtf;
Prn = prn;
Cnt = 0;
Prn.PrintPage += DoPrintPage;
Prn.EndPrint += DoEndPrint;
Prn.Print();
}
private RichTextBox Rtf;
private PrintDocument Prn;
private int Cnt;
#region Служебные определения
// 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 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 above 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);
#endregion
private int PrintChars(int charFrom, int charTo, PrintPageEventArgs e)
{
#region Mark starting and ending character
CHARRANGE cRange;
cRange.cpMin = charFrom;
cRange.cpMax = charTo;
#endregion
#region Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * AnInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * AnInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * AnInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * AnInch);
#endregion
#region Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * AnInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * AnInch);
rectPage.Left = (int)(e.PageBounds.Left * AnInch);
rectPage.Right = (int)(e.PageBounds.Right * AnInch);
#endregion
#region SendMessage
IntPtr hdc = e.Graphics.GetHdc();
FORMATRANGE fmtRange;
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
IntPtr res = IntPtr.Zero;
IntPtr wparam = new IntPtr(1);
// Move the pointer to the FORMATRANGE structure in memory
IntPtr lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);
// Send the rendered data for printing
res = SendMessage(Rtf.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);
#endregion
// Return last + 1 character printer
return res.ToInt32();
}
private void DoPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Print the content of the RichTextBox. Store the last character printed.
Cnt = PrintChars(Cnt, Rtf.TextLength, e);
// Look for more pages
e.HasMorePages = (Cnt < Rtf.TextLength);
}
private void DoEndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Prn.PrintPage -= DoPrintPage;
Prn.EndPrint -= DoEndPrint;
}
}
}
}
Все нормально работает, но есть маленькая ложка дегтя: если особо креативный пользователь установил у себя в настройках Windows цвет окна не белый (как у всех нормальных людей), а, например, серый, то на принтер содержимое RichTextBox печатается на этот самом "не белом" фоне (вне зависимости от установок RichTextBox.BackColor), что неприятно.
Вопрос: как можно добиться печати без этого фона?