Printing In MFC Application

牧云@^-^@ 提交于 2020-01-06 14:19:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!