EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 08:17:12(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.