Click here to Skip to main content
12,895,911 members (51,922 online)
Click here to Skip to main content
Add your own
alternative version


128 bookmarked
Posted 26 Nov 1999

Printing without the Document/View framework

, 29 May 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Shows how to print without relying on the MFC Doc/View framework


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

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

    CString strTitle;                       // Get the application 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;

    OnBeginPrinting(&dc, &Info);            // Call your "Init printing" function
    for (UINT page = Info.GetMinPage(); 
         page <= Info.GetMaxPage() && bPrintingOK; 
        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
        dc.AbortDoc();                      // abort job.

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

GeneralPrint View on dialog Pin
pengpengmaster29-Oct-03 15:00
memberpengpengmaster29-Oct-03 15:00 
GeneralRe: Print View on dialog Pin
vikas amin29-Sep-05 21:04
membervikas amin29-Sep-05 21:04 
GeneralThanx but..... Pin
atul arora24-Aug-03 5:01
memberatul arora24-Aug-03 5:01 
GeneralNice example! Pin
William Gonzales S.6-Aug-03 7:08
memberWilliam Gonzales S.6-Aug-03 7:08 
GeneralPrinting the contents of a CEdit control Pin
Anonymous4-Jun-03 21:00
sussAnonymous4-Jun-03 21:00 
GeneralRe: Printing the contents of a CEdit control Pin
Jonathan Craig19-Jun-03 4:00
memberJonathan Craig19-Jun-03 4:00 
GeneralAbout Date in vc++ Pin
Omarhh15-May-03 7:38
memberOmarhh15-May-03 7:38 
Generalcool Pin
kreep24-Apr-03 0:50
memberkreep24-Apr-03 0:50 
GeneralChange page size Pin
accarvajal27-Feb-03 9:29
memberaccarvajal27-Feb-03 9:29 
GeneralRe: Change page size Pin
Jonathan Craig17-Jun-03 3:55
memberJonathan Craig17-Jun-03 3:55 
Questionfor me it works on win2k but not on win98 ...?? Pin
Qadddd29-Jan-03 7:45
memberQadddd29-Jan-03 7:45 
Generalprinting directly(using StartDocPrinter/RWitePrinter does not work for landscape print) urgent Pin
parrhasarathy7-Jan-03 19:43
memberparrhasarathy7-Jan-03 19:43 
GeneralI need your help Pin
accarvajal27-Feb-03 9:44
memberaccarvajal27-Feb-03 9:44 
GeneralRe: printing directly(using StartDocPrinter/RWitePrinter does not work for landscape print) urgent Pin
Anonymous21-Oct-03 3:41
sussAnonymous21-Oct-03 3:41 
Generalprint from dialog Pin
olis17-Jul-02 22:09
memberolis17-Jul-02 22:09 
GeneralRe: print from dialog Pin
atul arora24-Aug-03 4:54
memberatul arora24-Aug-03 4:54 
Generalprint the content of screen as it is to the printer Pin
MINAKANNAN10-Jul-02 18:26
memberMINAKANNAN10-Jul-02 18:26 
GeneralRe: print the content of screen as it is to the printer Pin
N.K.SINGH1-Oct-03 17:22
memberN.K.SINGH1-Oct-03 17:22 
GeneralRe: print the content of screen as it is to the printer Pin
meenachi16-Oct-03 17:57
sussmeenachi16-Oct-03 17:57 
GeneralRe: print the content of screen as it is to the printer Pin
whils4-Feb-05 15:02
susswhils4-Feb-05 15:02 
GeneralProblem with show CPrintDialog Pin
Miro Kundacina6-Jul-02 0:04
memberMiro Kundacina6-Jul-02 0:04 
GeneralProblems in Debug Mode Pin
Greg Bowie13-Jun-02 3:54
memberGreg Bowie13-Jun-02 3:54 
GeneralRe: Problems in Debug Mode Pin
Roger Allen24-Jun-02 6:53
memberRoger Allen24-Jun-02 6:53 
GeneralRe: Problems in Debug Mode Pin
Anonymous26-Jun-02 0:56
memberAnonymous26-Jun-02 0:56 
QuestionDC leak? Pin
Roger Allen30-May-02 3:34
memberRoger Allen30-May-02 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 30 May 2002
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid