Click here to Skip to main content
15,881,173 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.7K   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

 
GeneralPrintDialog without user intervention Pin
Günter29-May-02 11:04
Günter29-May-02 11:04 
GeneralThanks.....a lot ! Pin
12-Mar-02 22:35
suss12-Mar-02 22:35 
GeneralSetting the page length of the dot matrix printer Pin
22-Feb-02 11:25
suss22-Feb-02 11:25 
GeneralRe: Setting the page length of the dot matrix printer Pin
E J20-Jan-03 16:01
E J20-Jan-03 16:01 
GeneralI NEEEEED HELP URGENTLYYYY Pin
Marko Frelih14-Feb-02 8:26
Marko Frelih14-Feb-02 8:26 
GeneralRe: I NEEEEED HELP URGENTLYYYY Pin
Joseph M. Newcomer16-Feb-02 17:36
Joseph M. Newcomer16-Feb-02 17:36 
GeneralPrinting in Win 98 Pin
11-Nov-01 4:24
suss11-Nov-01 4:24 
GeneralRe: Printing in Win 98 Pin
Joseph M. Newcomer19-Nov-01 1:27
Joseph M. Newcomer19-Nov-01 1:27 
The only problem I've found in Win98 is the font size; change the font size and it appears to work.
GeneralGreat. Thanks Pin
Kevin Pinkerton21-Aug-01 10:12
Kevin Pinkerton21-Aug-01 10:12 
GeneralDoesn't work on 98 Pin
Sam Levy26-Jul-01 21:36
Sam Levy26-Jul-01 21:36 
GeneralRe: Doesn't work on 98 Pin
Joseph M. Newcomer27-Jul-01 5:26
Joseph M. Newcomer27-Jul-01 5:26 
Generalprinter escape sequences Pin
Rick Crone11-Jul-01 5:00
Rick Crone11-Jul-01 5:00 
GeneralRe: printer escape sequences Pin
Joseph M. Newcomer12-Jul-01 5:29
Joseph M. Newcomer12-Jul-01 5:29 
GeneralFormFeed Pin
Rick Crone11-Jul-01 4:56
Rick Crone11-Jul-01 4:56 
GeneralRe: FormFeed Pin
Joseph M. Newcomer12-Jul-01 5:16
Joseph M. Newcomer12-Jul-01 5:16 
Generalproblem of printing several copies Pin
21-Jun-01 23:16
suss21-Jun-01 23:16 
GeneralUnderlining Pin
Andrew Stampor21-Jun-01 8:39
Andrew Stampor21-Jun-01 8:39 
GeneralRe: Underlining Pin
Joseph M. Newcomer21-Jun-01 11:24
Joseph M. Newcomer21-Jun-01 11:24 
GeneralPrinting OK on every printers Pin
13-Jun-01 2:55
suss13-Jun-01 2:55 
GeneralRe: Printing OK on every printers Pin
Joseph M. Newcomer13-Jun-01 4:53
Joseph M. Newcomer13-Jun-01 4:53 
GeneralThanks! Pin
Rick Crone2-May-01 7:47
Rick Crone2-May-01 7:47 
Generalline height Pin
3-Apr-01 2:58
suss3-Apr-01 2:58 
QuestionHow to print a document Pin
Dorian27-Jun-00 8:13
Dorian27-Jun-00 8:13 

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.