Click here to Skip to main content
15,867,957 members
Articles / Desktop Programming / MFC

A Simple Printing Mechanism

Rate me:
Please Sign up or sign in to vote.
4.41/5 (12 votes)
16 May 2000 230.4K   3K   49   47
Learn how to implement print support in your applications.

Introduction

A question that frequently arises is "How do I print?" In many cases, the full-blown print handling of a document/view class is inappropriate; in other cases, such as a dialog-based application, it is not available directly via the MFC library. This little class provides for some simple line-printer-simulation output, suitable for printing files, simple text, etc. This is the print mechanism I use for my logging control (and the code for this accompanies that project as well).

C++
void CMyClass::Print()
{
    CPrintDialog dlg(FALSE,
		      PD_ALLPAGES |
		      PD_HIDEPRINTTOFILE |
		      PD_NOPAGENUMS |
		      PD_RETURNDC |
		      PD_USEDEVMODECOPIES);
    if(has a selection)
	{ /* enable selection */
        dlg.m_pd.Flags |= PD_SELECTION;
    } /* enable selection */
    else
    { /* use selection */
        dlg.m_pd.Flags |= PD_NOSELECTION;
    } /* use selection */
    
    switch(dlg.DoModal())
    { /* DoModal */
    case 0:
    case IDCANCEL:
        return;
    case IDOK:
        break;
    default:
        ASSERT(FALSE); // impossible condition
        return;
    } /* DoModal */
     
    CDC dc;
    dc.Attach(dlg.m_pd.hDC);
    printer = new CPrinter(&dc);
    if(printer->StartPrinting())
    { /* success */
        for(some sample loop condition)
        { /* print line */
            CString s;
            ... // form the string you want to print
            printer->PrintLine(s);
        } /* print line */

        printer->EndPrinting();
    } /* success */
    delete printer;
    ::DeleteDC(dc.Detach());
} // CTraceList::Print

This is listed as a simple printing mechanism. It is not as elaborate as the one in our book Win32 Programming., but that is a pure-C version. This is a simpler, but pure MFC, example.

You can download just this printing code here, or you can download it as part of the Logging Control project.

CPrinter

CPrinter::CPrinter(CDC * dc)
Constructs a printer object with the DC of a printer. This is typically obtained from the CPrintDialog by using the flag PD_RETURNDC, then using CDC::FromHandle to get an MFC object for the DC.
CPrinter::EndPrinting()
This terminates the print job. It calls the necessary EndPage and EndDoc functions. If the CPrinter object is destroyed while a print job is active, the destructor will call this method.
int CPrinter::GetPageNumber()
This function can be called by a subclass's PageHeading routine to get the current page number.
virtual void CPrinter::PageHeading()
This method can be overridden in a subclass to print a page heading. The default effect in the base class is to print the program name on the left and the page number on the right.
void CPrinter::PrintLine(const CString & line)
This method prints a single line to the printer. No attempt is made to do line wrapping. Implicit left and top margins are assumed. If the line would overflow the existing printable page area, the current page is terminated and a new page is started. The page number is incremented, and heading is printed on the new page (see CPrinter::PageHeading()).
virtual void CPrinter::SetPrinterFont()
In the base class, this establishes the printer font as the stock ANSI_FIXED_FONT. A subclass may override this to provide its own font.
CPrinter::StartPrinting()
Starts a document on the printer. This must be called before any PrintLine calls are made. This sets up default margins and performs the necessary ::StartDoc call.

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

If you have questions or comments about this article, please leave a note below.

Copyright © 2000, The Joseph M. Newcomer Co. All Rights Reserved
www.flounder.com/mvp_tips.htm

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.


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
QuestionPrinting Problum Pin
lcloonkar10-Jan-10 4:15
lcloonkar10-Jan-10 4:15 
Questionwhen printer is not configured? Pin
723Alex20-Jul-07 10:41
723Alex20-Jul-07 10:41 
GeneralMFC Print preview problem Pin
Engineer Masood30-Mar-06 16:52
Engineer Masood30-Mar-06 16:52 
GeneralTnx Pin
quistiun8-Mar-06 18:26
quistiun8-Mar-06 18:26 
GeneralProblem found in StartPrinting Pin
apargeter24-Sep-05 9:14
apargeter24-Sep-05 9:14 
GeneralProblem when Print Arabic fonts Pin
bushka12-Jun-05 11:25
bushka12-Jun-05 11:25 
When I use arabic font in Visual C++, it appear correct in view but in printpreview window arabic characters overlaped to each other. I use This statement

CFont ffont;
ffont.CreateFont(-18,0,0,0,700,0,0,0,178,3,2,1,2,"Arial");

mapmode is MM_LOENGLISH

I need help. Any body have the solution pleas send to my email
thanks
GeneralRe: Problem when Print Arabic fonts Pin
Joseph M. Newcomer12-Jun-05 17:11
Joseph M. Newcomer12-Jun-05 17:11 
GeneralRe: Problem when Print Arabic fonts Pin
Anonymous13-Jun-05 5:54
Anonymous13-Jun-05 5:54 
GeneralPrinting files from VB/VC Pin
franklinjIssac4-Apr-05 1:50
franklinjIssac4-Apr-05 1:50 
GeneralRe: Printing files from VB/VC Pin
Joseph M. Newcomer4-Apr-05 10:25
Joseph M. Newcomer4-Apr-05 10:25 
QuestionHow could i print Multiple pages in Java Pin
Anonymous8-Feb-05 17:54
Anonymous8-Feb-05 17:54 
GeneralGood job but... Pin
scoroop12-Dec-04 6:00
scoroop12-Dec-04 6:00 
Generalprinting strings Pin
vivadot16-Jul-04 11:34
vivadot16-Jul-04 11:34 
GeneralRe: printing strings Pin
Joseph M. Newcomer16-Jul-04 21:32
Joseph M. Newcomer16-Jul-04 21:32 
GeneralIts not working Pls help Pin
Member 48497618-Jan-04 19:35
Member 48497618-Jan-04 19:35 
GeneralAuto Line Wrap Pin
Wouter Dhondt22-Jan-03 2:41
Wouter Dhondt22-Jan-03 2:41 
GeneralRe: Auto Line Wrap Pin
heggum26-Aug-03 11:36
heggum26-Aug-03 11:36 
GeneralGetting values from Print Setup Pin
Jack_pt4-Nov-02 8:58
Jack_pt4-Nov-02 8:58 
GeneralRe: Getting values from Print Setup Pin
Joseph M. Newcomer4-Nov-02 9:12
Joseph M. Newcomer4-Nov-02 9:12 
GeneralRe: Getting values from Print Setup Pin
Jack_pt4-Nov-02 17:18
Jack_pt4-Nov-02 17:18 
GeneralNT prob Pin
Wouter Dhondt29-Aug-02 22:20
Wouter Dhondt29-Aug-02 22:20 
GeneralCancel causes "Debug Assertion Failed"!!! Pin
Kristijan14-Aug-02 2:59
professionalKristijan14-Aug-02 2:59 
GeneralFixed width Probs Pin
dazinith11-Jun-02 8:21
dazinith11-Jun-02 8:21 
GeneralRe: Fixed width Probs Pin
Anonymous11-Jul-02 10:36
Anonymous11-Jul-02 10:36 
GeneralPrintDialog without user intervention Pin
Günter29-May-02 11:04
Günter29-May-02 11:04 

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.