Printing In MFC Application

2019-09-15 08:17发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-09-15 08:23

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();
}
查看更多
登录 后发表回答