在此处查看类似问题的答案:
MVP 建议:
RTB 没有“分页符”之类的东西。分页符仅在您打印到纸上时才相关。您将需要像 Word 这样的文字处理器。谷歌 EM_FORMATRANGE 了解如何打印 RTB 的内容。
这个提议得到了一些投票:
richTextBox1.SelectedRtf = @"{\rtf1 \par \page}";
以及来自Windows 桌面开发指南的更多详细信息,其中包括以下代码:
下面的示例代码将 Rich Edit 控件的内容打印到指定的打印机。
// hwnd is the HWND of the rich edit control.
// hdc is the HDC of the printer. This value can be obtained for the
// default printer as follows:
//
// PRINTDLG pd = { sizeof(pd) };
// pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
//
// if (PrintDlg(&pd))
// {
// HDC hdc = pd.hDC;
// ...
// }
BOOL PrintRTF(HWND hwnd, HDC hdc)
{
DOCINFO di = { sizeof(di) };
if (!StartDoc(hdc, &di))
{
return FALSE;
}
int cxPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETX);
int cyPhysOffset = GetDeviceCaps(hdc, PHYSICALOFFSETY);
int cxPhys = GetDeviceCaps(hdc, PHYSICALWIDTH);
int cyPhys = GetDeviceCaps(hdc, PHYSICALHEIGHT);
// Create "print preview".
SendMessage(hwnd, EM_SETTARGETDEVICE, (WPARAM)hdc, cxPhys/2);
FORMATRANGE fr;
fr.hdc = hdc;
fr.hdcTarget = hdc;
// Set page rect to physical page size in twips.
fr.rcPage.top = 0;
fr.rcPage.left = 0;
fr.rcPage.right = MulDiv(cxPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSX));
fr.rcPage.bottom = MulDiv(cyPhys, 1440, GetDeviceCaps(hDC, LOGPIXELSY));
// Set the rendering rectangle to the pintable area of the page.
fr.rc.left = cxPhysOffset;
fr.rc.right = cxPhysOffset + cxPhys;
fr.rc.top = cyPhysOffset;
fr.rc.bottom = cyPhysOffset + cyPhys;
SendMessage(hwnd, EM_SETSEL, 0, (LPARAM)-1); // Select the entire contents.
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg); // Get the selection into a CHARRANGE.
BOOL fSuccess = TRUE;
// Use GDI to print successive pages.
while (fr.chrg.cpMin < fr.chrg.cpMax && fSuccess)
{
fSuccess = StartPage(hdc) > 0;
if (!fSuccess) break;
int cpMin = SendMessage(hwnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
if (cpMin <= fr.chrg.cpMin)
{
fSuccess = FALSE;
break;
}
fr.chrg.cpMin = cpMin;
fSuccess = EndPage(hdc) > 0;
}
SendMessage(hwnd, EM_FORMATRANGE, FALSE, 0);
if (fSuccess)
{
EndDoc(hdc);
}
else
{
AbortDoc(hdc);
}
return fSuccess;
}