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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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();
}