Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Printing without the Document/View framework

0.00/5 (No votes)
29 May 2002 10  
Shows how to print without relying on the MFC Doc/View framework

Introduction

If you have a dialog based application and want to print, then you cannot take advantage of the Doc/View framework that MFC provides to do all the dirty work for you. The code presented below demonstrates the typical steps needed to:

  • prompt the user for the printing characteristics such as printer and paper size (via a call to CPrintDialog::DoModal)
  • Setup a printer DC and printing DOCINFO structure
  • Use callbacks to adjust settings such as start and end pages to print, or printing sizes
  • Print all pages to be printed
  • Cleanup

The callbacks

In order to keep the MFC Doc/View feel I recomend providing helper callback functions OnBeginPrinting, OnEndPrinting and OnPrint similar to the CView versions. A CDC and a CPrintInfo object is passed into each of these functions. You will have to provide these functions yourself. Typically you would undertake any initialisation neessary (such as creating GDI objects) in OnBeginPrinting. Your OnPrint function would be where you do the actual printing/drawing, and your OnEndPrinting function performs any cleanup necessary (such as deleting GDI objects created in OnBeginPrinting). You can call these functions whatever you want - I've used these names to be consistant with the CView names, and they are only used to show where the various initialisation/printing/cleanup code should be inserted by you.

Typical implementations would look like:

void OnBeginPrinting(CDC *pDC, CPrintInfo* pInfo)
{
    // maybe pre cache some pens or brushes, or modify the

    // properties of the DC

}

void OnPrint(CDC *pDC, CPrintInfo* pInfo)
{
    // Do your drawing/printing exactly as you would in a

    // CView::OnDraw function. The CPrintInfo structure

    // will give you the bounds and the current page number

}

void OnEndPrinting(CDC *pDC, CPrintInfo* pInfo)
{
    // Clean up pens or brushes, or restore the DC

}

The Printing code

void CMyDialog::Print() 
{
    CDC dc;
    CPrintDialog printDlg(FALSE);

    if (printDlg.DoModal() == IDCANCEL)     // Get printer settings from user

        return;

    dc.Attach(printDlg.GetPrinterDC());     // Get and attach a printer DC

    dc.m_bPrinting = TRUE;

    CString strTitle;                       // Get the application title

    strTitle.LoadString(AFX_IDS_APP_TITLE);

    DOCINFO di;                             // Initialise print document details

    ::ZeroMemory (&di, sizeof (DOCINFO));
    di.cbSize = sizeof (DOCINFO);
    di.lpszDocName = strTitle;

    BOOL bPrintingOK = dc.StartDoc(&di);    // Begin a new print job


    // Get the printing extents and store in the m_rectDraw field of a 

    // CPrintInfo object

    CPrintInfo Info;
    Info.m_rectDraw.SetRect(0,0, 
                            dc.GetDeviceCaps(HORZRES), 
                            dc.GetDeviceCaps(VERTRES));

    OnBeginPrinting(&dc, &Info);            // Call your "Init printing" function

    for (UINT page = Info.GetMinPage(); 
         page <= Info.GetMaxPage() && bPrintingOK; 
         page++)
    {
        dc.StartPage();                     // begin new page

        Info.m_nCurPage = page;
        OnPrint(&dc, &Info);                // Call your "Print page" function

        bPrintingOK = (dc.EndPage() > 0);   // end page

    }
    OnEndPrinting(&dc, &Info);              // Call your "Clean up" function


    if (bPrintingOK)
        dc.EndDoc();                        // end a print job

    else
        dc.AbortDoc();                      // abort job.


    dc.DeleteDC();                          // delete the printer DC

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here