问题
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();
}
来源:https://stackoverflow.com/questions/8982965/printing-in-mfc-application