在 MFC 应用程序中打印
如何使用基于 MFC 对话框的应用程序打印文档?我做了一个打印按钮.单击此按钮后,我想打印一些文档或一些文本.
How can I print a document using MFC Dialog Based Application? I have made a print button. After clicking on this button, I want print of some document or some text.
推荐答案
您可以创建一个不可见的 CHtmlEditCtrl
控件并使用 SetDocumentHTML(LPCTSTR)
方法将文本加载到其中然后调用 PrintDocument()
方法.
You can create an invisble CHtmlEditCtrl
control and load your text to it with SetDocumentHTML(LPCTSTR)
method and then call PrintDocument()
method.
void WaitForComplete(IHTMLDocument2* document)
{
BSTR ready;
document->get_readyState(&ready);
while(wcscmp(ready, L"complete"))
{
AfxPumpMessage();
document->get_readyState(&ready);
};
}
void CPrintInMFCDialogBasedAppDlg::OnBnClickedPrint()
{
CHtmlEditCtrl PrintCtrl;
if(!PrintCtrl.Create(NULL, WS_CHILD, CRect(0, 0, 0, 0), this, 1))
{
ASSERT(FALSE);
return; // Error!
}
CComPtr<IHTMLDocument2> document;
PrintCtrl.GetDocument(&document);
WaitForComplete(document);
PrintCtrl.SetDocumentHTML(_T("Hello!<BR>It is <B>my first</B> print!"));
WaitForComplete(document);
PrintCtrl.PrintDocument();
}
相关文章