0

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.

DTdev
  • 571
  • 1
  • 16
  • 32
  • 3
    What does your MFC book say to do next? What have you tried? – Cody Gray Jan 24 '12 at 06:47
  • http://www.codersource.net/mfc/mfc-tutorials/mfc-print-tutorial.aspx But Still confused on how to start proceeding as it is not dialog based application – DTdev Jan 24 '12 at 06:50
  • Which document? Specific on every click or the user needs to browse first? – Sunscreen Jan 24 '12 at 09:11
  • No need to browse. File path will be hardcoded. Or if it is possible to browse, then also OK. I just want to print a text Document. – DTdev Jan 24 '12 at 11:34

1 Answers1

5

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();
}
A.Danesh
  • 795
  • 8
  • 40