Print Preview without the Doc/View Architecture


How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017

Environment: VC6 SP3, NT4 SP5, Windows 2000 Beta 3

This code allows you to make use of the MFC print preview architecture without using the document/ view architecture. This is accomplished by overriding some key functions from the MFC CView class and CPreview class. I have introduced two new classes with the required overrides called CWrapperView and CMyPrintPreview. I will explain how to integrate these classes into a new MFC project created without the use of the document/ view architecture.

Create a new MFC project without the document view architecture. Add WrapperView.cpp, WrapperView.h, and MyPreview.h to your project.

Open the ChildView.h file and include the header file WrapperView.h. Now change the parent class to CWrapperView and add the DECLARE_DYNCREATE macro to the class.

	class CChildView : public CWrapperView

Open MainFrm.h and change

	ChildView m_wndView;


	CChildView  *m_pView;

Open MainFrm.cpp and remove the following code from the OnCreate function

	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
	CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
		TRACE0("Failed to create view window\n");
		return -1;

And replace it with

	CCreateContext context;
	context.m_pNewViewClass = RUNTIME_CLASS(CChildView);
	context.m_pCurrentFrame = this;
	context.m_pCurrentDoc = NULL;
	context.m_pLastView = NULL;

	m_pView = STATIC_DOWNCAST(CChildView, CreateView(&context));
	if(m_pView != NULL)

Use the class wizard to add command handlers for ID_FILE_PRINT and ID_FILE_PRINT_PREVIEW to CMainFrame and edit the new functions to...

	void CMainFrame::OnFilePrint() 

	void CMainFrame::OnFilePrintPreview() 

OnFilePrintPreview has been overriden to take a pointer to the owner frame (in this case CMainFrame) as this is needed in the overriden DoPrintPreview function. The defualt implementation uses AfxGetMainWnd() but sometimes we don't want to use the main window we want to use another frame window derived from CFrameWnd

Open ChildView.cpp and add the following code

	void CChildView::OnFilePrint()


Now add a command handler to the CChildView message map


Open ChildView.h and add

		void OnFilePrint();

We have to add this overide for the OnFilePrint function because the default implementation in CView is protected and seeing as we need to invoke it from our frame window (CMainFrame, in the File Print Command Handler) we have to make it public in our derived view and then call the default implementation.

Now use the class wizard to add an OnDraw function to your view class and add code to call OnDraw from OnPaint.

	void CChildView::OnPaint() 
		CPaintDC dc(this); // device context for painting

		// Do not call CWnd::OnPaint() for painting messages

Now all that remains is to add some code to your OnDraw function.

	OnDraw(CDC *pDC)
		CRect client;
		client.DeflateRect(50, 50);
		CBrush brush(RGB(255, 0, 0));
		pDC->FillRect(&client, &brush);

The same process needs to take place if you have derived a class from CFrameWnd and want to add MFC print preview support to it. The only difference is that you have to do a bit more work by creating the view class yourself.


Download demo - 8 Kb
Download source - 21 Kb


  • OnDraw and multiPage

    Posted by leha on 06/13/2004 09:56pm

    May be more reasonable to use OnPrint (?) because it contains CPrintInfo, and doesn't need any additional parameters passing.( Espessially multipage docoments )

  • Why I can't use it in Static Library ?

    Posted by Legacy on 11/22/2000 12:00am

    Originally posted by: Jeff

    I can use it in Shared Dll.
    But it doesn't work in Static Libray , I have traced it and
    found that :
    pView->m_pToolBar = new CDialogBar;
    if (!pView->m_pToolBar->Create(pParent, MAKEINTRESOURCE(nIDResource),
    have some problems , Why ?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date