京剧小生唱段小宴:如何实现richedit打印?

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 11:22:43
我在CScrollView上创建一页一页的richedit控键,以实现类似word的页面效果,但是怎么实现打印和打印预览呢?MFC默认的打印是与CScrollView对应的,我该怎么把它对应到richedit呢?

richedit传回字符串,在view类加个控件变量接受,不就可以了?

或者还有
GetDlgItemText(控件ID,接受字符串)函数
也同样可以接受richedit里面的字符串,然后送到打印机就可以了

下面是MSDN的回答
可以通知 Rich Edit 控件 (CRichEditCtrl) 为指定设备(如打印机)呈现输出。也可以指定输出设备,Rich Edit 控件将为该设备格式化其文本。

若要为特定设备格式化 Rich Edit 控件的部分内容,可以使用 FormatRange 成员函数。FormatRange 结构与此函数一起使用,指定格式化的文本范围和目标设备的设备上下文 (DC)。

为输出设备格式化文本后,可以使用 DisplayBand 成员函数将输出发送到该设备。通过重复使用 FormatRange 和 DisplayBand,打印 Rich Edit 控件内容的应用程序可以实现带区划分。(带区划分是为打印目的将输出划分为较小的部分。)

可以使用 SetTargetDevice 成员函数指定目标设备,Rich Edit 控件将为该设备格式化其文本。该函数对 WYSIWYG(所见即所得)格式很有用,这类格式的应用程序定位文本所使用的是默认打印机的字体标准,而不是屏幕的字体标准。

下面是示例,假设pmyRichEditCtrl是richedit的邦定变量,pMyPrinterDC是printer的DC

extern CRichEditCtrl* pmyRichEditCtrl;
// A pointer to a printer DC.
extern CDC* pMyPrinterDC;

FORMATRANGE fr;

// Get the page width and height from the printer.
long lPageWidth = ::MulDiv(pMyPrinterDC->GetDeviceCaps(PHYSICALWIDTH),
1440, pMyPrinterDC->GetDeviceCaps(LOGPIXELSX));
long lPageHeight = ::MulDiv(pMyPrinterDC->GetDeviceCaps(PHYSICALHEIGHT),
1440, pMyPrinterDC->GetDeviceCaps(LOGPIXELSY));
CRect rcPage(0, 0, lPageWidth, lPageHeight);

// Format the text and render it to the printer.
fr.hdc = pMyPrinterDC->m_hDC;
fr.hdcTarget = pMyPrinterDC->m_hDC;
fr.rc = rcPage;
fr.rcPage = rcPage;
fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;
pmyRichEditCtrl->FormatRange(&fr, TRUE);

// Update the display with the new formatting.
RECT rcClient;
pmyRichEditCtrl->GetClientRect(&rcClient);
pmyRichEditCtrl->DisplayBand(&rcClient);

预览就简单了吧?
把printer的DC改成你的预览对话框dc就可以了
需要自己做一个预览对话框
^_^