Click here to Skip to main content
Click here to Skip to main content

Printing without the Document/View framework

By , 29 May 2002
 

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, 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
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionPrinting without the Document/View frameworkmemberkimba1111 Sep '12 - 6:41 
GeneralTerrific!memberMike Gaskey6 Dec '11 - 9:49 
GeneralRe: Terrific!adminChris Maunder6 Dec '11 - 9:50 
Generalmemory leakmemberlichongbin7 Mar '11 - 22:56 
QuestionHow should i use this way to print a Recordset ?membersofe_Jane26 Nov '09 - 21:06 
QuestionHow to programmatically make the printing fail through OnPrintmemberel davo25 Aug '08 - 17:18 
Questionwhat is AFX_IDS_APP_TITLE?memberkamal21 Nov '06 - 22:39 
AnswerRe: what is AFX_IDS_APP_TITLE?memberS Douglas28 Nov '06 - 23:07 
GeneralRe: what is AFX_IDS_APP_TITLE?memberkamal29 Nov '06 - 22:50 
GeneralRe: what is AFX_IDS_APP_TITLE?memberS Douglas30 Nov '06 - 4:05 
Generalthanks a lot!memberkamal6 Dec '06 - 18:30 
GeneralRe: thanks a lot!memberS Douglas7 Dec '06 - 6:19 
QuestionExcellent One!! A small question...memberkvrnkiran9 Oct '06 - 20:05 
GeneralPrinting a CRichEditView from a CWinThreadmemberalmc4 Jan '06 - 9:53 
QuestionPrinting without the Document / View FrameworkmemberBuckBrown25 Oct '05 - 14:27 
GeneralDifferences between CDC::StartPage()/CDC::EndPage() and StartPagePrinter()/EndPagePrinter()memberRYU^^3 May '05 - 15:02 
GeneralPreviewmemberZoltan17 Apr '05 - 6:30 
GeneralSomeone Has To Know How To Do This!memberrrrtek3 Mar '05 - 18:12 
GeneralRe: Someone Has To Know How To Do This!memberRYU^^3 May '05 - 17:29 
GeneralChanging the page size.memberrtek1 Mar '05 - 12:23 
GeneralDC problemmembersitu10 Jan '05 - 8:37 
After MSDN, free the DC creat by using CPrintDialog.
 
have a look here
QuestionHow can I select type of paper or number of copies ?membermym@neunet.com.ar13 Oct '04 - 10:13 
GeneralErrors in article 'Printing without the Document/View framework'sussLuka Lednicki23 Jul '04 - 7:50 
Questionscrollview printing??memberxxhimanshu28 Jun '04 - 17:59 
GeneralPrinting without Print Dialog boxmembersakthisaravanan28 Jun '04 - 0:47 
GeneralRe: Printing without Print Dialog boxmemberRYU^^3 May '05 - 17:37 
AnswerRe: Printing without Print Dialog boxmemberDanPetitt6 Nov '05 - 11:36 
GeneralRe: Printing without Print Dialog boxmemberkevin_rf29 Jan '09 - 5:37 
GeneralPrinting on a laser printermemberEversman4 Jun '04 - 9:05 
GeneralRe: Printing on a laser printermemberNFetscher8 Jun '04 - 4:13 
GeneralPrinter Orientation and Default Printermemberrsrryan19 May '04 - 9:51 
GeneralRe: Printer Orientation and Default PrintersussAnonymous27 May '04 - 21:00 
GeneralRe: Printer Orientation and Default PrintermemberJeremy Davis2 Aug '04 - 4:36 
GeneralRe: Printer Orientation and Default Printermemberrtek1 Mar '05 - 10:13 
GeneralRe: Printer Orientation and Default PrintermemberJeremy Davis1 Mar '05 - 21:47 
GeneralRe: Printer Orientation and Default Printermemberrtek2 Mar '05 - 5:57 
GeneralRe: Printer Orientation and Default Printermemberrtek2 Mar '05 - 6:29 
GeneralRe: Printer Orientation and Default PrintermemberJeremy Davis2 Mar '05 - 21:56 
GeneralRe: Printer Orientation and Default Printermemberrtek3 Mar '05 - 11:20 
QuestionRe: Printer Orientation and Default Printer [modified]memberZombie_Inc7 Feb '07 - 11:23 
Generalview printed documents without reprint themsussashalhoub10 May '04 - 20:26 
GeneralOnPrint() functionsussanamika10108 Mar '04 - 8:23 
GeneralWriteing directly to printer buffersussIvica Crnjac12 Feb '04 - 3:27 
GeneralPrint without eject (scroll) whole pagemembericyboyvn10 Dec '03 - 17:56 
QuestionWhy the page select checkbox is always grayed?sussAnonymous7 Dec '03 - 18:27 
GeneralProblems when printing from a dialogsussRishi Patel14 Nov '03 - 0:37 
GeneralRe: Problems when printing from a dialogsusssfzz4 Dec '03 - 9:41 
GeneralRe: Problems when printing from a dialogmemberRishi Patel4 Dec '03 - 21:05 
GeneralRe: Problems when printing from a dialogmembersfzz5 Dec '03 - 4:47 
GeneralRe: Problems when printing from a dialogmemberjssuthar18 Jan '04 - 19:29 

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

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