Printing and Preview

OnFilePrint() is the event of MFC doc view application to print a document to printer. OnFilePrintPreview() is the event for preview of the document. It does not print on printer DC.

afx_msg void OnFilePrint();
afx_msg void OnFilePrintPreview();

OnFilePrint/Preview event goes to a sequence of events.

OnPreparePrinting() >> OnBeginPrinting() >> OnDraw() >> OnEndPrinting()

CPrintInfo

CPrintInfo is the Printing information structure of MFC. It holds all the required data members

struct CPrintInfo // 
{
	CPrintInfo();
	~CPrintInfo();

	CPrintDialog* m_pPD;     // pointer to print dialog

	BOOL m_bDocObject;       // TRUE if printing by IPrint interface
	BOOL m_bPreview;         // TRUE if in preview mode
	BOOL m_bDirect;          // TRUE if bypassing Print Dialog
	BOOL m_bContinuePrinting;// set to FALSE to prematurely end printing
	UINT m_nCurPage;         // Current page
	UINT m_nNumPreviewPages; // Desired number of preview pages
	CString m_strPageDesc;   // Format string for page number display
	LPVOID m_lpUserData;     // pointer to user created struct
	CRect m_rectDraw;        // rectangle defining current usable page area

	// these only valid if m_bDocObject
	UINT m_nOffsetPage;      // offset of first page in combined IPrint job
	DWORD m_dwFlags;         // flags passed to IPrint::Print

	void SetMinPage(UINT nMinPage); // Set min page 
	void SetMaxPage(UINT nMaxPage); // Set max page 
	UINT GetMinPage() const;        // Get min page 
	UINT GetMaxPage() const;        // Get max page 
	UINT GetFromPage() const;       // Get from page
	UINT GetToPage() const;         // Get to page
}

OnPreparePrinting() is a callback to collect print info attribute from user. Aplication must call base CView::DoPreparePrinting() with same print info. It displays the printer dialog and gets the printer DC.

BOOL CExampleView::OnPreparePrinting(CPrintInfo* pInfo)
{
   //The document has 2 pages.
   pInfo->SetMaxPage(2);
   return CView::DoPreparePrinting(pInfo);
}

OnPreparePrinting() call stack

Printing:
CMySDIView::OnPreparePrinting()
CView::OnFilePrint()
_AfxDispatchCmdMsg()

Previewing:
CMySDIView::OnPreparePrinting()
CPreviewView::SetPrintView()
CView::DoPrintPreview()
CView::OnFilePrintPreview()
_AfxDispatchCmdMsg()

OnBeginPrinting

OnBeginPrinting() is the startup callback of the application. Application can create GDI objects like pen, brush, etc which are needed for drawing. All memory allocation and handle creation should be in this even.

OnBeginPrinting() call stack

Printing:
CMySDIView::OnBeginPrinting()
CView::OnFilePrint()
_AfxDispatchCmdMsg()

Previewing:
CMySDIView::OnBeginPrinting()
CPreviewView::SetPrintView()
CView::DoPrintPreview()
CView::OnFilePrintPreview()
_AfxDispatchCmdMsg()

OnDraw()

OnDraw() is the draw callback of the application. Application should call all GDI drawing routines here. Actual drawing is done here.

OnDraw() call stack

Printing:
CMySDIView::OnDraw()
CView::OnPrint()
CView::OnFilePrint()
_AfxDispatchCmdMsg()

Previewing:
CMySDIView::OnDraw()
CView::OnPrint()
CPreviewView::OnDraw()
CView::OnPaint()
CWnd::OnWndMsg()
CWnd::WindowProc()
AfxCallWndProc()
AfxWndProc()
AfxWndProcBase()

OnEndPrinting()

OnEndPrinting() is the cleanup callback of the application. All memory allocation should be released and all GDI objects which are created should be closed. This even completes a cycle of printing or print preview process.

OnEndPrinting() call stack

Print preview:
CMySDIView::OnEndPrinting()
CView::OnEndPrintPreview()
CPreviewView::OnPreviewClose()
_AfxDispatchCmdMsg()

Printing:
CMySDIView::OnEndPrinting()
CView::OnFilePrint()
_AfxDispatchCmdMsg()

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#