A Bare-Bones Printing Application
The Ezprint Application Shown in Figure 13-2 Demonstrates The Minimum Amount of Work A Document / View Application Must Do To Support Printing and Print Previewing.
Figure 13-2. The Ezprint Application Displaying a Print Preview.
An EZPrint "document" contains a blue circle 10 centimeters (1,000 units in the MM_LOMETRIC mapping mode) in diameter with a yellow interior The application's File menu contains just four items:.. Print, Print Preview, Print Setup, and Exit The Print and Print Preview commands are mapped to CView :: OnFilePrint and CView :: OnFilePrintPreview in CEZPrintView's message map, and the Print Setup command is mapped to CWinApp :: OnFilePrintSetup in CEZPrintApp's message map. AppWizard performed all the message mapping. The Print command displays a Print dialog box in which the user can specify printing options such as the desired printer, the print range, and the number of copies. Print preview puts the application in print preview mode. Print Setup displays a Print Setup dialog box. You can use the Print Setup Dialog Box To Choose a Printer, SELECT A Paper Size, And Specify The page Orientation-portrait or landscape.
I used AppWizard to create the EZPrint project. In the Step 4 dialog box (shown in Figure 13-3), I checked the Printing And Print Preview box to add printing support. Checking this box prompts AppWizard to make three modifications to the code that IT generates:
Add Print, Print Preview, and Print Setup commands to the File menu. Modify the message map to connect the Print, Print Preview, and Print Setup commands to MFC-provided command handlers. Override OnPreparePrinting, OnBeginPrinting, and OnEndPrinting in the view class.
AppWizard's OnPreparePrinting function includes a call to DoPreparePrinting. Its OnBeginPrinting and OnEndPrinting functions do nothing, so you can delete them if you do not use them. I left them in, but EZPrint would work just as well without them. All of EZPrint's printing code IS Found In The View Class, Whose Source Code Is Reproduces 13-3. Using Appwizard To Add Printing and Print Previewing Support.
There's not a lot to say about EZPrint's printing and print previewing capabilities other than that MFC does the bulk of the work. CEZPrintView :: OnDraw renders all the output, regardless of whether that output is destined for the screen, a printer, or a print preview window. So that the circle will have the same proportions regardless of where it is drawn, OnDraw does all of its drawing using the MM_LOMETRIC mapping mode. that's important, because pixel-per-inch values for screens and printers are rarely the same. If you drew to the screen and the printer in the MM_TEXT mapping mode, the circle would be a lot smaller on a 600 dpi printer than it would be on the screen. to get WYSIWYG results, you'd have to scale the circle's height and width manually during printing and print previewing using ratios derived from pixel-per-inch counts for the screen and printer. Using a mapping mode in which logical units scale to physical distances rather than pixel counts allows the GDI to do the scal ING AND Ensures That OnDraw CAN Produce Conscent Results No Matter Where The Output is Rendered.
Figure 13-4. The ezprint application.
EzprintView.h // EZPrintView.h: interface of the cezprintview class
//
///
#if! defined (
AFX_EZPRINTVIEW_H__3A83FDED_A3E6_11D2_8E53_006008A82731__Nincluded_) # define AFX_EZPRINTVIEW_H__3A83FDED_A3E6_11D2_8E53_006008A82731__inCluded_
#iF _MSC_VER> 1000
#pragma overce
#ENDIF / / 100 m _ _ _
Class CezprintView: Public CVIEW
{
protected: // Create from Serialization Only
CezprintView ();
Declare_Dyncreate (CEZPrintView)
// attributes
PUBLIC:
CEZPrintDoc * getDocument ();
// Operations
PUBLIC:
// Overrides
// ClassWizard Generated Virtual Function Overrides
// {{AFX_VIRTUAL (CEZPRINTVIEW)
PUBLIC:
Virtual void OnDraw (CDC * PDC); // Overridden to Draw this View
Virtual Bool PrecreateWindow (CreateStruct & Cs);
protected:
Virtual Bool Onprepareprinting (CPrintInfo * Pinfo);
Virtual Void OnBeginPrinting (CDC * PDC, CPrintInfo * Pinfo);
Virtual void OneendPrinting (CDC * PDC, CPrintInfo * Pinfo);
//}} AFX_VIRTUAL
// Implementation
PUBLIC:
Virtual ~ cezprintview ();
#ifdef _Debug
Virtual void assertvalid () const;
Virtual Void Dump (CDumpContext & DC) Const;
#ENDIF
protected:
// generated message map functions
protected:
// {{AFX_MSG (CEZPRINTVIEW)
// Note - The ClassWizard Will Add and Remove Member functions here.
// Do Not Edit What You See in these Blocks of generated code!
//}} AFX_MSG
Declare_message_map ()
}
#ifndef _debug // debug version in ezprintview.cpp
Inline CezprintDoc * CEZPRINTVIEW :: getDocument ()
{RETURN (CEZPRINTDOC *) m_pdocument;
#ENDIF
///
// {{AFX_INSERT_LOCATION}}
// Microsoft Visual C Will Insert Additional Declarations
// Immediately Before The Previous Line.
#ENDIF
//! Defined (
// afX_EZPRINTVIEW_H__3A83FDED_A3E6_11D2_8E53_006008A82731__included_)
EzprintView.cpp // EZPrintView.cpp: Implementation of The CEZPRINTVIEW CLASS
//
#include "stdafx.h" #include "ezprint.h"
#include "ezprintdoc.h"
#include "ezprintview.h"
#ifdef _Debug
#define new debug_new
#undef this_file
Static char this_file [] = __file__;
#ENDIF
///
// CEZPRINTVIEW
Implement_dyncreate (CEZPRINTVIEW, CVIEW)
Begin_MESSAGE_MAP (CEZPRINTVIEW, CVIEW)
// {{AFX_MSG_MAP (CEZPrintView)
// Note - The Classwizard Will Add and Remove Mapping Macros Here.
// Do Not Edit What You See in these Blocks of generated code!
//}} AFX_MSG_MAP
// Standard Printing CommANDS
ON_COMMAND (ID_FILE_PRINT, CView :: OnfilePrint)
ON_COMMAND (ID_FILE_PRINT_DIRECT, CView :: OnfilePrint)
ON_COMMAND (ID_FILE_PRINT_PREVIEW, CVIEW :: OnfilePrintpreview)
END_MESSAGE_MAP ()
///
// CezprintView Construction / Destruction
CEZPrintView :: CEZPrintView ()
{
}
CEZPRINTVIEW :: ~ CEZPrintView ()
{
}
Bool CezprintView :: PrecreateWindow (CreateStruct & Cs)
{
Return CView :: PrecreateWindow (CS);
}
///
// CEZPRINTVIEW DRAWING
Void CezprintView :: OnDraw (CDC * PDC)
{
CPEN PEN (PS_SOLID, 50, RGB (0, 0, 255);
CBRUSH Brush (RGB (255, 255, 0));
PDC-> setmapmode (mm_lometric);
CPEN * POLDPEN = PDC-> SELECTOBJECT (& PEN);
CBRUSH * POLDBRUSH = PDC-> SelectObject (& brush);
PDC-> Ellipse (100, -100, 1100, -1100);
PDC-> SELECTOBJECT (POLDBRUSH);
PDC-> SELECTOBJECT (POLDPEN);
}
///
// CEZPRINTVIEW Printing
Bool CezprintView :: OnPrepareprinting (CPrintInfo * Pinfo)
{
Return doprepareprinting (pinfo);
}
Void CezprintView :: OnBeginPrinting (CDC * / * PDC * /, CPrintInfo * / * Pinfo * /)
{
// Todo: add extra initialization before printing
}
Void CezprintView :: OnendPrinting (CDC * / * PDC * /, CPrintInfo * / * Pinfo * /)
{
// Todo: Add cleanup after Printing
}
///
// CezprintView Diagnostics # ifdef _Debug
Void CEZPRINTVIEW :: AssertValid () Const
{
CView :: assertvalid ();
}
Void CezprintView :: Dump (CDumpContext & DC) Const
{
CView :: DUMP (DC);
}
CEZPRINTDOC * CEZPRINTDOC * CEZPRINTVIEW :: getDocument () // Non-debug version is inline
{
Assert (m_pdocument-> iskindof (runtime_class (cezprintdoc));
Return (CEZPRINTDOC *) m_pdocument;
}
#ENDIF / / _ Debug
///
// CezprintView Message Handlers
Black-And-White Print Previews
MFC's print preview support is not perfect. EZPrint's preview page shows the circle in full-blown color even if the only printer attached to your PC is a black-and-white model. (Naturally, the circle will be printed in color if you Print It on a color printer.) You can add a nice touch to your print preview code by doing your rendering in shades of gray if Both The Following Conditions Are True When Onprint or OnDraw IS Called:
PINFO-> M_BPREVIEW IS NONZERO (Onprint) or PDC-> m_hdc is not equal to pdc-> m_hattribdc (onDraw). PDC-> GetDeviceCaps (Numcolors) Returns 2, Indicating That The Printer is a monochrome device.
You Can Convert RGB Color Values Into Shades of Gray with this Formula:
R / g / b = (red * 0.30) (Green * 0.59) (Blue * 0.11)
The Following Statement Creates A Gray Brush That Simulates on The Screen How Yellow (RGB (255, 255, 0)) WILL LOOK ON A MONOCHROME OUTPUT DEVICE:
CBRUSH Brush (RGB (227, 227, 227));
I got The value 227 by Plugging The Color Components 255, 255, And 0 Into The Color Conversion Formula.
TO See A Simple Demonstration of Black-And-White Print Previewing, Replace The Lines
CPEN PEN (PS_SOLID, 50, RGB (0, 0, 255);
CBRUSH Brush (RGB (255, 255, 0));
In Ezprint's Cprintview :: Ondraw Function with these:
Bool BMONO = (PDC-> getDeviceCaps (Numcolors) == 2) &&
(PDC-> M_HDC! = PDC-> m_hattribdc); // True Only for Preview Mode.
CPEN PEN (PS_SOLID, 50, BMONO® RGB (28, 28, 28): RGB (0, 0, 255));
CBRUSH Brush (BMONO® RGB (227, 227, 227): RGB (255, 255, 0));
Print previews will now be rendered in shades of gray when the default printer is a black-and-white model. Comparing m_hDC to m_hAttribDC is a sneaky way to detect print preview mode when CPrintInfo information is not available.