Click here to Skip to main content
13,662,151 members
Click here to Skip to main content
Add your own
alternative version


111 bookmarked
Posted 26 Nov 1999
Licenced CPOL

Print Previewing without the Document/View Framework

, 26 Nov 1999
Rate this:
Please Sign up or sign in to vote.
Want to preview your printing without relying on the doc/view framework?

Sample Image


Thanks to Chris Maunder for introducing the printing method in Printing without the Document/View framework. Many people have been asking for print preview without the doc/view framework as well. However, so far, no body has proposed a way to do that yet. I have studied on how Microsoft implements print preview in doc/view framework using the class CPreviewView, which is derived from CSrollView. The print preview is called from CView::OnFilePrintPreview( ) and CView::DoPrintPreview. They are undocumented, the source code can be found from VC\mfc\src\Viewprev.cpp. The implementation relies on CView and CFrameWnd framework extensively.

In order to use the default print preview functionality in non doc/view framework, like dialog based applications, I played a trick by creating temporary CFrameWnd and CView objects and then calling the default OnFilePrintPreview( ) from the temporary view class. I borrowed Chris Maunder's CGridCtrl control from MFC Grid control (derived from CWnd) article as an example to illustrate the implementation. I included another demo project, which was modified from Iuri Apollonio's article: Generic printing class (and how to print a list control content). The code was developed under VC5 environment and has been tested in Windows 98 and NT platforms without any problem. :)

How to use

Add a function PrintPreview( ) in your control class (CGridCtrl in this case) or the class where you put your OnBeginPrinting, OnPrint, etc. Also, add two member variables to the class and initialize them:

// In your header file /////////////
    CSingleDocTemplate* m_pTemplate;
    BOOL m_bPrintPreview;
    void PrintPreview();

// In your class constructor ///////
#include "ViewPrintPreview.h"

    // Add initialization

// In your class ///////////////////
void CGridCtrl::PrintPreview()
    if (m_bPrintPreview)

    CFrameWnd* pOldFrame=(CFrameWnd*)AfxGetThread()->m_pMainWnd;
    pOldFrame->ShowWindow(SW_HIDE); //added by eric

    if (!m_pTemplate)
        m_pTemplate = new CSingleDocTemplate(

    CFrameWnd * pFrameWnd = m_pTemplate->CreateNewFrame( NULL, NULL );

    m_pTemplate->InitialUpdateFrame( pFrameWnd, NULL, FALSE);

    CViewPrintPreview* pView=(CViewPrintPreview*)pFrameWnd->GetActiveView();

    pFrameWnd->SetWindowText(_T("Koay Kah Hoe Print Preview"));

A CSingleDocTemplate object is used to create a frame and a view window. The view class CViewPrintPreview has no chance to show itself, as it is suppressed by preview view immediately. The m_pMainWnd pointer is changed to the new CFrameWnd so that the preview class can use it as parent frame. (Has any one done this before?) The original m_pMainWnd is saved in the view class, when preview is ended, it will restore the pointer back to the original value. Then, OnFilePrintPreview is called.

Add the view class CViewPrintPreview and its header file to the project. The main job of the view class is to pass the printing functions OnBeginPrinting, OnPrint and OnEndPrinting to the control class or wherever you place them. This must be done through the view class as the print preview calls these functions from view class. When the preview window is closed, the program must restore the m_pMainWnd pointer back to its original value. Then, the frame and view window must be destroyed. This is done in the function OnEndPrintPreview.

Now, you can view the preview window already. However, this is not the end of the story yet, the toolbar buttons cannot update themselves as normally they should be. After some investigation, I found that the window normally calls the message WM_IDLEUPDATECMDUI to update the toolbar's state from the OnIdle function in SDI or MDI application. For dialog based applications, the OnIdle function is not called. Thus, the message WM_IDLEUPDATECMDUI is not sent to update toolbar. We have to send the message ourselves. I overrode the virtual function ContinueModal( ) in the main dialog class, to do the job. You must include the header file AfxPriv.h in order to recognize the WM_IDLEUPDATECMDUI message.

BOOL CGridCtrlDemoDlg::ContinueModal()
    if (m_Grid.m_bPrintPreview) // m_Grid is your control class
        // send WM_IDLEUPDATECMDUI message to update toolbar state
        // This is normally called by OnIdle function
        // in SDI or MSI applications.
        // Dialog based applications don't call OnIdle,
        // so send the message from here instead
            (WPARAM)TRUE, 0, TRUE, TRUE);

    return CDialog::ContinueModal();

Special precaution has to be taken when you use the print preview. As the m_pMainWnd pointer is pointed to the new frame window when doing preview, elsewhere in your application that uses this pointer could cause your program to crash. You can use the m_bPrintPreview as an indicator to determine which window the m_pMainWnd is pointed to.

FAQ on compilation using static library

In dialog based applications, the resource file for the print preview is not included. This problem can be solved by simply including the resource file afxprint.rc to your project file. Open your resource file as text explicitly, add the following line in TEXTINCLUDE 3 section below #include afxres.rc:

"#include ""afxprint.rc"" // printing/print preview resources\r\n"

At the end of the resource file, do the actual including again as follows, below #include afxres.rc:

#include afxprint.rc // printing/print preview resources

FAQ on print button in preview toolbar

When pressing the print button in preview toolbar, the ID_FILE_PRINT command message is sent. Thus, you must create a message handler to the ID_FILE_PRINT message at your main dialog class, or wherever the message could be caught.

Add a line like below in the message map of your dialog class


Then of course, you must have the handler function OnFilePrint(). See the given example on how it is done.

In cases where there are more than one printing function in the application, but there is only one general ID_FILE_PRINT message, special processing is needed to ensure that the correct printing function is called. There are two possible solutions. The first way is to use a variable identifying which printing should take place. The message handler should refer to this variable to call the correct printing function. The other way is to reroute the message mapping by overriding OnCmdMsg(). Each printable dialog or control has its own message handler for ID_FILE_PRINT. When a particular dialog or control is active, the message should be routed to the active dialog or control first.

Finally, I wish you happy previewing! :)

ViewPrintPreview header file

#if !defined(AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_)
#define AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// ViewPrintPreview.h : header file

#include "GridCtrl.h"

// CViewPrintPreview view

class CViewPrintPreview : public CView
    CViewPrintPreview(); // protected constructor used by dynamic creation

// Attributes
    CGridCtrl *m_pCtrl;
    CFrameWnd *m_pOldFrame;

// Operations
    virtual void OnFilePrintPreview();

// Overrides
    // ClassWizard generated virtual function overrides
    virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnEndPrintPreview(CDC* pDC, 
       CPrintInfo* pInfo, POINT point, CPreviewView* pView);

// Implementation
    virtual ~CViewPrintPreview();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;

    // Generated message map functions


// Microsoft Developer Studio will insert additional
// declarations immediately before the previous line.


ViewPrintPreview implementation file

// ViewPrintPreview.cpp : implementation file

#include "stdafx.h"
#include "ViewPrintPreview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// CViewPrintPreview




BEGIN_MESSAGE_MAP(CViewPrintPreview, CView)

// CViewPrintPreview drawing

void CViewPrintPreview::OnDraw(CDC* pDC)
    CDocument* pDoc = GetDocument();
    // TODO: add draw code here

// CViewPrintPreview diagnostics

#ifdef _DEBUG
void CViewPrintPreview::AssertValid() const

void CViewPrintPreview::Dump(CDumpContext& dc) const
#endif //_DEBUG

// CViewPrintPreview message handlers

void CViewPrintPreview::OnFilePrintPreview()

BOOL CViewPrintPreview::OnPreparePrinting(CPrintInfo* pInfo) 
    return DoPreparePrinting(pInfo);

void CViewPrintPreview::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) 
    m_pCtrl->OnBeginPrinting(pDC, pInfo);

void CViewPrintPreview::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
    m_pCtrl->OnPrint(pDC, pInfo);

void CViewPrintPreview::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) 
    m_pCtrl->OnEndPrinting(pDC, pInfo);

void CViewPrintPreview::OnEndPrintPreview(CDC* pDC, 
      CPrintInfo* pInfo, POINT point, CPreviewView* pView) 
    CView::OnEndPrintPreview(pDC, pInfo, point, pView);
    // Show the original frame
    // Restore main frame pointer
    // Kill parent frame and itself


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Koay Kah Hoe
Malaysia Malaysia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionPrint preview and print button Pin
MaxMax1421-Jul-12 6:07
memberMaxMax1421-Jul-12 6:07 
GeneralPrint Command Pin
Adeel Mirza22-Feb-10 23:58
memberAdeel Mirza22-Feb-10 23:58 
Generalprint preview Pin
yunchol1-Oct-09 23:54
memberyunchol1-Oct-09 23:54 
Generalwrong focus after returning from preview mode Pin
fbruender26-Nov-08 6:39
memberfbruender26-Nov-08 6:39 
GeneralBug fix at the end of CPrinterJob::OnPrepareDC(...) Pin
Christoph Conrad3-Sep-07 5:54
memberChristoph Conrad3-Sep-07 5:54 
GeneralRe: Bug fix at the end of CPrinterJob::OnPrepareDC(...) Pin
Christoph Conrad3-Sep-07 20:54
memberChristoph Conrad3-Sep-07 20:54 
GeneralMenuBars /toolbars / control bar state modified when close print preview Pin
nbarbedette22-Aug-06 5:03
membernbarbedette22-Aug-06 5:03 
GeneralRe: MenuBars /toolbars / control bar state modified when close print preview Pin
nbarbedette23-Aug-06 0:31
membernbarbedette23-Aug-06 0:31 
Questionhow to able the button "prev" and "zoom out" Pin
sohori26-Mar-06 15:59
membersohori26-Mar-06 15:59 
QuestionCrash after print preview Pin
chris17522-Mar-06 5:48
memberchris17522-Mar-06 5:48 
GeneralGridLines Pin
mrlp103-Feb-06 5:50
membermrlp103-Feb-06 5:50 
GeneralCompile problem Pin
MSS-Software7-Nov-05 23:29
memberMSS-Software7-Nov-05 23:29 
GeneralRe: Compile problem Pin
jani0114-Aug-07 1:20
memberjani0114-Aug-07 1:20 
GeneralRe: Compile problem Pin
Christoph Conrad3-Sep-07 20:59
memberChristoph Conrad3-Sep-07 20:59 
GeneralProgram crashes Pin
Krishna Kumar N3-Nov-05 21:59
memberKrishna Kumar N3-Nov-05 21:59 
Generalprint preview specific page Pin
georgeser16-Aug-05 21:14
membergeorgeser16-Aug-05 21:14 
GeneralBrief overview of the control.... Pin
TuPacMansur16-Jun-05 16:57
sussTuPacMansur16-Jun-05 16:57 
Generaldisable Print button on Preview dialog Pin
Zoltan31-May-05 12:41
memberZoltan31-May-05 12:41 
GeneralRe: disable Print button on Preview dialog Pin
TuPacMansur16-Jun-05 16:45
sussTuPacMansur16-Jun-05 16:45 
GeneralRe: disable Print button on Preview dialog Pin
Zoltan10-Jul-05 12:35
memberZoltan10-Jul-05 12:35 
QuestionRe: disable Print button on Preview dialog Pin
skyramesh6-Oct-07 19:30
memberskyramesh6-Oct-07 19:30 
Generalwhy CViewPrintPreview directly catch the ID_FILE_PRINT Pin
sunshimin31-Jan-05 22:01
membersunshimin31-Jan-05 22:01 
GeneralCursor Pin
mkauma8-Dec-04 11:46
membermkauma8-Dec-04 11:46 
GeneralRe: Cursor Pin
UMER91116-Jun-05 16:50
memberUMER91116-Jun-05 16:50 
Questionhow to implement more than one printing function Pin
parrotlabelca3-Sep-04 18:56
sussparrotlabelca3-Sep-04 18:56 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180810.1 | Last Updated 27 Nov 1999
Article Copyright 1999 by Koay Kah Hoe
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid