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).
if(has a selection)
dlg.m_pd.Flags |= PD_SELECTION;
dlg.m_pd.Flags |= PD_NOSELECTION;
printer = new CPrinter(&dc);
for(some sample loop condition)
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
EndDoc functions. If the
CPrinter object is destroyed while a print job is active, the destructor will call this method.
- 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
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
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 email@example.com 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