CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> Windows Programming >> Printing >> Print Preview


Print Previewing without the Document/View Framework
Rating:

Koay Kah Hoe (view profile)
June 15, 1999


(continued)



Sample Image

Environment: VC5, NT4, win98

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 in this column yet. (Except for the unknown 'm_bPrintPreview' suggestion, but seems that there is no such a free ticket.) 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 CViewOnFilePrintPreview( ) and CViewDoPrintPreview. 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 call the default OnFilePrintPreview( ) from the temporary view class. I've borrowed Chris Maunder's CGridCtrl control from MFC Grid control (derived from CWnd) article as an example to illustrate the implementation. The code was developed under VC5 environment and have been tested in Windows 98 and NT platform 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 /////////////
private
    CSingleDocTemplate* m_pTemplate;
public
    BOOL m_bPrintPreview;
    void PrintPreview();
////////////////////////////////////

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

CGridCtrlCGridCtrl(...)
{
    // Add initialization
    m_pTemplate=NULL;
    m_bPrintPreview=FALSE;
}
////////////////////////////////////

// In your class ///////////////////
void CGridCtrlPrintPreview()
{
    if (m_bPrintPreview)
    {
        AfxGetApp()->m_pMainWnd->SetFocus();
        return;
    }

    CFrameWnd* pOldFrame=(CFrameWnd*)AfxGetThread()->m_pMainWnd;

    if (!m_pTemplate)
    {
        m_pTemplate = new CSingleDocTemplate(
            IDR_MENU,
            NULL,
            RUNTIME_CLASS(CFrameWnd),
            RUNTIME_CLASS(CViewPrintPreview));
        AfxGetApp()->AddDocTemplate(m_pTemplate);
    }

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

    m_pTemplate->InitialUpdateFrame( pFrameWnd, NULL);

    CViewPrintPreview* pView=(CViewPrintPreview*)pFrameWnd->GetActiveView();
    pView->m_pCtrl=this;
    pView->m_pOldFrame=pOldFrame;

    AfxGetApp()->m_pMainWnd=pFrameWnd;
    pFrameWnd->SetWindowText(_T("Koay Kah Hoe Print Preview"));
    pFrameWnd->ShowWindow(SW_SHOWMAXIMIZED);
    pView->OnFilePrintPreview();
}

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 do this before?) The original m_pMainWnd is saved in the view class, when preview is ended, it will restore the pointer back to 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 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 them 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 application, 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 overrided the virtual function ContinueModal( ) in the main dialog class to do the job


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
        AfxGetApp()->m_pMainWnd->SendMessageToDescendants(WM_IDLEUPDATECMDUI,
            (WPARAM)TRUE, 0, TRUE, TRUE);

    return CDialogContinueModal();
}

Special precaution has to be taken when you use the print preview. As a new window is created for print preview, the main dialog or control is still accessible by the user. When a preview window is opened, changes made at the main dialog or control could reflect to the preview window causing some unexpected result. You might need to disable any editing while previewing by referring to the public Boolean variable m_bPrintPreview. Another thing is, 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. Again, you can use the m_bPrintPreview as an indicator to determine which window the m_pMainWnd is pointed to.

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
{
protected
    CViewPrintPreview();           // protected constructor used by dynamic creation
    DECLARE_DYNCREATE(CViewPrintPreview)

// Attributes
public
    CGridCtrl *m_pCtrl;
    CFrameWnd *m_pOldFrame;

// Operations
public
    virtual void OnFilePrintPreview();

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CViewPrintPreview)
    protected
    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);
    //}}AFX_VIRTUAL


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

    // Generated message map functions
protected
    //{{AFX_MSG(CViewPrintPreview)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

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

#endif // !defined(AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_)

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__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CViewPrintPreview

IMPLEMENT_DYNCREATE(CViewPrintPreview, CView)

CViewPrintPreview::CViewPrintPreview()
{
    m_pOldFrame=NULL;
}

CViewPrintPreview::~CViewPrintPreview()
{
}


BEGIN_MESSAGE_MAP(CViewPrintPreview, CView)
    //{{AFX_MSG_MAP(CViewPrintPreview)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewPrintPreview drawing

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

/////////////////////////////////////////////////////////////////////////////
// CViewPrintPreview diagnostics

#ifdef _DEBUG
void CViewPrintPreview::AssertValid() const
{
    CView::AssertValid();
}

void CViewPrintPreview::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CViewPrintPreview message handlers

void CViewPrintPreview::OnFilePrintPreview()
{
    CViewOnFilePrintPreview();
}

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
    m_pOldFrame->ShowWindow(SW_SHOW);
    // Restore main frame pointer
    AfxGetApp()->m_pMainWnd=m_pOldFrame;
    m_pCtrl->m_bPrintPreview=FALSE;
    // Kill parent frame and itself
    GetParentFrame()->DestroyWindow();
}

Downloads

Download demo project - 100 Kb

History

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Solving problems with staic library (VC6) - jig (08/29/2004)
Can you print just one page? - Legacy CodeGuru (05/08/2003)
RUNTIME_CLASS problem with lib... - Legacy CodeGuru (03/07/2003)
Broken in .NET - Legacy CodeGuru (12/11/2002)
keyboard flesh for scrollbar. - Legacy CodeGuru (11/14/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES