HOWTO: Print from a Class Other Than CView

Last reviewed: July 2, 1997
Article ID: Q133275
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 4.1

SUMMARY

The standard method to support printing capability in an MFC application is to override the OnDraw(CDC* pDC) member function of the CView class that provides the pointer to the device context (pDC). If the application does not possess a CView class (for example, a dialog-based application), you can use the code listed in this article to obtain a pointer to the printing device context to implement printing.

MORE INFORMATION

In this example, CMyDialog is publicly derived from CDialog, OnPrintBuf() is a message handler that is mapped to BN_CLICKED, and SetPrintAlign() is used to select print settings specified to an application. The sample code prints the contents of the 'pbuf' buffer.

Sample Code

   void CMyDialog::OnPrintBuf()
       {
       char   pbuf[100] = "Hello World.";
       HDC    hdcPrn ;

       // Instantiate a CPrintDialog.
       CPrintDialog *printDlg =
           new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);

       // Initialize some of the fields in PRINTDLG structure.
       printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
       printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;

       // Display Windows print dialog box.
       printDlg->DoModal();

       // Obtain a handle to the device context.
       hdcPrn = printDlg->GetPrinterDC();
       if (hdcPrn != NULL)
           {
           CDC *pDC = new CDC;
           pDC->Attach (hdcPrn);      // attach a printer DC

           pDC->StartDoc("test");  // begin a new print job
                                   // for Win32 use
                                   // CDC::StartDoc(LPDOCINFO) override

           SetPrintAlign(pDC, hdcPrn);// Set the printing alignment

           pDC->StartPage();          // begin a new page
           pDC->TextOut(10, 10, pbuf);// write the string in pbuf
           pDC->EndPage();            // end a page

           pDC->EndDoc();             // end a print job

           pDC->Detach();             // detach the printer DC
           delete pDC;
           }

       delete printDlg;
       }

   void CMyDialog::SetPrintAlign(CDC *pDC, HDC hdcPrn)
       {
       short cxPage, cyPage;

       cxPage = ::GetDeviceCaps (hdcPrn, HORZRES) ;
       cyPage = ::GetDeviceCaps (hdcPrn, VERTRES) ;
       pDC->SetMapMode (MM_ISOTROPIC) ;
       pDC->SetWindowExt ( 1000, 1000) ;
       pDC->SetViewportExt (cxPage / 2, -cyPage / 2) ;
       pDC->SetViewportOrg (cxPage / 2,  cyPage / 2) ;
       pDC->SetTextAlign (TA_BASELINE | TA_CENTER) ;
       }

REFERENCES

For more information about the MFC printing, please see "Programming with the Microsoft Foundation Class Library" in Books Online.


Additional query words: 2.52 3.0 3.00 3.1 3.10
Keywords : kbcode kbprg kbprint MfcPrinting
Technology : kbMfc
Version : 1.0 1.5 1.51 1.52 2.0 2.1 4.0
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.