Click here to Skip to main content
12,627,002 members (34,970 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

A Simple Printing Mechanism

, 16 May 2000
Rate this:
Please Sign up or sign in to vote.
Learn how to implement print support in your applications.
  • Download source files - 6 Kb

    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).

    void CMyClass::Print()
        CPrintDialog dlg(FALSE,
    		      PD_ALLPAGES |
    		      PD_NOPAGENUMS |
    		      PD_RETURNDC |
        if(has a selection)
    	{ /* enable selection */
            dlg.m_pd.Flags |= PD_SELECTION;
        } /* enable selection */
        { /* use selection */
            dlg.m_pd.Flags |= PD_NOSELECTION;
        } /* use selection */
        { /* DoModal */
        case 0:
        case IDCANCEL:
        case IDOK:
            ASSERT(FALSE); // impossible condition
        } /* DoModal */
        CDC dc;
        printer = new CPrinter(&dc);
        { /* success */
            for(some sample loop condition)
            { /* print line */
                CString s;
                ... // form the string you want to print
            } /* print line */
        } /* success */
        delete printer;
    } // 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(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.

    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.

    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.

    Send mail to with questions or comments about this article.
    Copyright © 2000, <!--webbot bot="Substitution" s-variable="CompanyLongName" startspan -->The Joseph M. Newcomer Co.<!--webbot bot="Substitution" endspan i-checksum="64444" --> All Rights Reserved
  • 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


    About the Author

    Joseph M. Newcomer
    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]

    You may also be interested in...

    Comments and Discussions

    QuestionHow could i print Multiple pages in Java Pin
    Anonymous8-Feb-05 18:54
    sussAnonymous8-Feb-05 18:54 
    GeneralGood job but... Pin
    scoroop12-Dec-04 7:00
    memberscoroop12-Dec-04 7:00 
    Generalprinting strings Pin
    vivadotnet16-Jul-04 12:34
    membervivadotnet16-Jul-04 12:34 
    GeneralRe: printing strings Pin
    Joseph M. Newcomer16-Jul-04 22:32
    memberJoseph M. Newcomer16-Jul-04 22:32 
    GeneralIts not working Pls help Pin
    jssuthar18-Jan-04 20:35
    memberjssuthar18-Jan-04 20:35 
    GeneralAuto Line Wrap Pin
    Wouter Dhondt22-Jan-03 3:41
    memberWouter Dhondt22-Jan-03 3:41 
    GeneralRe: Auto Line Wrap Pin
    heggum26-Aug-03 12:36
    memberheggum26-Aug-03 12:36 
    GeneralGetting values from Print Setup Pin
    Jack_pt4-Nov-02 9:58
    memberJack_pt4-Nov-02 9:58 
    GeneralRe: Getting values from Print Setup Pin
    Joseph M. Newcomer4-Nov-02 10:12
    memberJoseph M. Newcomer4-Nov-02 10:12 
    GeneralRe: Getting values from Print Setup Pin
    Jack_pt4-Nov-02 18:18
    memberJack_pt4-Nov-02 18:18 
    GeneralNT prob Pin
    Kwakkie29-Aug-02 23:20
    memberKwakkie29-Aug-02 23:20 
    GeneralCancel causes "Debug Assertion Failed"!!! Pin
    Kristijan14-Aug-02 3:59
    sussKristijan14-Aug-02 3:59 
    GeneralFixed width Probs Pin
    dazinith11-Jun-02 9:21
    memberdazinith11-Jun-02 9:21 
    GeneralRe: Fixed width Probs Pin
    Anonymous11-Jul-02 11:36
    sussAnonymous11-Jul-02 11:36 
    GeneralPrintDialog without user intervention Pin
    Günter29-May-02 12:04
    memberGünter29-May-02 12:04 
    GeneralThanks.....a lot ! Pin
    Michel Dimanche12-Mar-02 23:35
    memberMichel Dimanche12-Mar-02 23:35 
    GeneralSetting the page length of the dot matrix printer Pin
    kumarseema22-Feb-02 12:25
    memberkumarseema22-Feb-02 12:25 
    GeneralRe: Setting the page length of the dot matrix printer Pin
    Been there20-Jan-03 17:01
    sussBeen there20-Jan-03 17:01 
    Marko Frelih14-Feb-02 9:26
    memberMarko Frelih14-Feb-02 9:26 
    Joseph M. Newcomer16-Feb-02 18:36
    memberJoseph M. Newcomer16-Feb-02 18:36 
    GeneralPrinting in Win 98 Pin
    Brad Loyd11-Nov-01 5:24
    memberBrad Loyd11-Nov-01 5:24 
    GeneralRe: Printing in Win 98 Pin
    Joseph M. Newcomer19-Nov-01 2:27
    memberJoseph M. Newcomer19-Nov-01 2:27 
    GeneralGreat. Thanks Pin
    Kevin Pinkerton21-Aug-01 11:12
    memberKevin Pinkerton21-Aug-01 11:12 
    GeneralDoesn't work on 98 Pin
    Sam Levy26-Jul-01 22:36
    memberSam Levy26-Jul-01 22:36 
    GeneralRe: Doesn't work on 98 Pin
    Joseph M. Newcomer27-Jul-01 6:26
    memberJoseph M. Newcomer27-Jul-01 6:26 

    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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.161205.3 | Last Updated 17 May 2000
    Article Copyright 2000 by Joseph M. Newcomer
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid