Click here to Skip to main content
12,290,502 members (39,661 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

MFC Print preview extension DLL for dialogs

, 26 Dec 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
An MFC extension library, so your dialogs can have easy print/print preview support.

Sample Image - preview.jpg


After trying to make use of Koay's code to allow print preview in a dialog, I released that it was a lot of work to get print preview in a dialog working, so I took the code and wrapped it up in an MFC extension DLL which your MFC applications can link to. So now to get print and preview options working, you just need to derive your CDialog class from the exported class CPrintPreviewDialog. Once you have done that, then you can override the standard Doc/View print preview options to provide your printed output.

  • virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
  • virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
  • virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
  • virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
  • virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

Using the extension DLL

The PrintExtension project builds the library and DLL file that will be used by your project. Once that has been built, you will need to add the following statements in your MFC application to start making use of its features:

// generally, this would go in your stdafx.h file
#include "PrintExtension.h"

The library will be automatically linked to your project. You just have to make sure that the PrintExtension.DLL (PrintExtensiond.dll for debug) file is in the same directory as your application executable.

You then need to convert your CDialog derived class to derive from the CPrintPreviewDialog class. This can be done by doing a Find/Replace on the .h/.cpp dialog source files from CDialog to CPrintPreviewDialog.

To add the preview and print functionality, you need to add buttons or menu options for the ID's ID_FILE_PRINT and ID_FILE_PRINT_PREVIEW.

Override your required printing functions to generate output, usually a minimum of OnPrint() and OnPreparePrinting().

Everything else should work automatically.

Notes on use

If you are using this in a dialog application (non SDI/MDI), then you will also need to add a menu resource to your application. The menu content does not matter, it just needs to exist. It should have the ID IDR_MAINFRAME, and will be used when the hidden frame is created. As the frame is never seen by the user, the menu options never become active, but it is required for the frame window to be created successfully.

In OnPreparePrinting(), you must call the base class (CPrintPreviewDialog::OnPreparePrinting(pInfo);), before exiting the function so that a required print preview function is called. If you do not, you get an ASSERT as listed in some of the comments below.

Problems encountered

There were some fiddely problems to make sure that, when you clicked on Print in the preview view, the dialog handled the command and not any open background document (if doing this in an SDI or MDI MFC app).


This MFC extension DLL draws heavily on the code by Koay Kah Hoe in the article Print Previewing without the Document/View Framework.

Revision history

  • 5th December 2002 - Version 1
  • 27th December 2002 - Demo project added and some extra notes added to article text



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


About the Author

Roger Allen
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Xjkstar13-May-14 17:09
memberXjkstar13-May-14 17:09 
GeneralMy vote of 3 Pin
zhanglipeng9-Jan-13 14:23
memberzhanglipeng9-Jan-13 14:23 
QuestionRe: memory leak Pin
Michael Adamus29-Apr-10 5:09
memberMichael Adamus29-Apr-10 5:09 
GeneralPrint Preview [modified] Pin
Adeel Mirza16-Feb-10 18:20
memberAdeel Mirza16-Feb-10 18:20 
GeneralCannot compile, Pin
angel42025-Feb-09 13:31
memberangel42025-Feb-09 13:31 
Generala problem and workaround Pin
min_2_max24-Dec-07 14:06
membermin_2_max24-Dec-07 14:06 
QuestionCan that be part of my code, instead of a DLL Pin
AlexEvans18-May-07 21:45
memberAlexEvans18-May-07 21:45 
QuestionHow can i break print/preview page for if i'm printing records ane by one from MS Access DAO database Pin
Kushwaha4-Sep-06 19:57
memberKushwaha4-Sep-06 19:57 
AnswerAnother Assertion Error Figured Out Pin
chris17510-Jul-06 4:18
memberchris17510-Jul-06 4:18 
AnswerAssert Message Figured Out Pin
chris1757-Jul-06 2:43
memberchris1757-Jul-06 2:43 
QuestionCan I distribute it Pin
cathyliu62021-Mar-06 6:34
membercathyliu62021-Mar-06 6:34 
QuestionDid someone use DLL in DOTNET ? Pin
Luke_216-Feb-06 3:06
memberLuke_216-Feb-06 3:06 

First, thx for Roger for all his contribution with his codes.

Well, I tried to use the DLL in my Doc / View App, with a Dialog based Class, and finally compiled it. But, when I click on my buttons nothing happen .... There are named ID_FILE_PRINT and ID_FILE_PRINT_PREVIEW and after following the code, there is no relationship between buttons and OnPreparePrinting function .... I just needed to adapt my CView print routine in a CDialog class, in CView it's automatic but in CDialog no. And I wonder if it is supposed to be with the DLL. For information, I'm in Static DLL project and can't change this propertie at all.

Hope to have news soon.

| Luke_21 |
QuestionQuestions? Pin
Muhammad Waqas Butt12-Aug-05 22:21
memberMuhammad Waqas Butt12-Aug-05 22:21 
GeneralCrashes when opening dialog Pin
MrP227-May-05 4:27
memberMrP227-May-05 4:27 
GeneralRe: Crashes when opening dialog Pin
Terry Beddingfield30-Dec-05 7:32
memberTerry Beddingfield30-Dec-05 7:32 
GeneralRe-entrance in MultiView SDI App. Pin
loadcom4-Nov-04 13:35
memberloadcom4-Nov-04 13:35 
GeneralRe: Re-entrance in MultiView SDI App. Pin
min_2_max20-Dec-07 1:44
membermin_2_max20-Dec-07 1:44 
GeneralRe: Re-entrance in MultiView SDI App. Pin
min_2_max24-Dec-07 13:55
membermin_2_max24-Dec-07 13:55 
GeneralCrashes on close with close button Pin
okjak26-Oct-04 5:22
memberokjak26-Oct-04 5:22 
GeneralRe: Crashes on close with close button Pin
tangxingbin22-Mar-05 20:53
membertangxingbin22-Mar-05 20:53 
GeneralRe: Crashes on close with close button Pin
N n MISHRA11-Jun-05 4:39
memberN n MISHRA11-Jun-05 4:39 
GeneralRe: Crashes on close with close button Pin
varandas795-Mar-06 3:58
membervarandas795-Mar-06 3:58 
QuestionStill crashes on close with close button Pin
Mr. S3-Apr-06 4:08
memberMr. S3-Apr-06 4:08 
AnswerRe: Crashes on close with close button Pin
Eduard Huguet30-Nov-06 0:08
memberEduard Huguet30-Nov-06 0:08 
GeneralPage setup Pin
rpar2-Aug-04 19:09
memberrpar2-Aug-04 19:09 
GeneralPrev Page always disabled on Print Preview Pin
Anonymous26-Jul-04 4:26
sussAnonymous26-Jul-04 4:26 
GeneralRe: Prev Page always disabled on Print Preview Pin
kenji tetsuya30-Aug-05 3:47
memberkenji tetsuya30-Aug-05 3:47 
GeneralRe: Prev Page always disabled on Print Preview Pin
Member 418928114-Dec-08 1:24
memberMember 418928114-Dec-08 1:24 
GeneralPrev Page always disabled on Print Preview Pin
Anonymous26-Jul-04 4:26
sussAnonymous26-Jul-04 4:26 
GeneralPrev Page is disabled Pin
Monk De Wolly De Honk26-Jul-04 4:17
sussMonk De Wolly De Honk26-Jul-04 4:17 
GeneralPrev Page is disabled Pin
Monk De Wolly De Wonk26-Jul-04 4:17
sussMonk De Wolly De Wonk26-Jul-04 4:17 
GeneralPrev Page is disabled Pin
Monk De Holly De Wonk26-Jul-04 4:14
sussMonk De Holly De Wonk26-Jul-04 4:14 
GeneralRe: Prev Page is disabled Pin
Roger Allen26-Jul-04 5:35
memberRoger Allen26-Jul-04 5:35 
GeneralRe: Prev Page is disabled Pin
Anonymous18-Nov-04 11:03
sussAnonymous18-Nov-04 11:03 
GeneralRe: Prev Page is disabled Pin
min_2_max22-Dec-07 22:09
membermin_2_max22-Dec-07 22:09 
Generalabout the class CMyPreviewView : public CPreviewView Pin
ybugchen30-Jun-04 21:29
memberybugchen30-Jun-04 21:29 
GeneralRe: about the class CMyPreviewView : public CPreviewView Pin
Roger Allen7-Jul-04 6:18
memberRoger Allen7-Jul-04 6:18 
GeneralJust a hint for unicode users Pin
Jesterka19-May-04 1:05
memberJesterka19-May-04 1:05 
Questionhow to compile program if use MFC in static library? Pin
kisa9016-Mar-04 13:03
memberkisa9016-Mar-04 13:03 
AnswerRe: Facing the same problem( how to compile program if use MFC in static library?) Pin
N n MISHRA2-May-05 7:58
memberN n MISHRA2-May-05 7:58 
GeneralGood but not profect Pin
sortfish26-Feb-04 20:51
membersortfish26-Feb-04 20:51 
Questionhow can this dll invoke in visual foxpro? Pin
lin_zhang12-Feb-04 20:20
memberlin_zhang12-Feb-04 20:20 
Generalrelease can not work?! Pin
sortfish1-Feb-04 21:05
membersortfish1-Feb-04 21:05 
GeneralDemo program doesn't work Pin
hwjang1112-Oct-03 2:49
memberhwjang1112-Oct-03 2:49 
GeneralRe: Demo program doesn't work Pin
ahatzig29-Jan-04 23:40
memberahatzig29-Jan-04 23:40 
GeneralHelp for Newbie Pin
Alan Bartholomew10-Jul-03 8:13
memberAlan Bartholomew10-Jul-03 8:13 
GeneralDialog bar button update fix. Pin
Roger Allen2-Jan-03 4:40
memberRoger Allen2-Jan-03 4:40 
GeneralRe: Dialog bar button update fix. Pin
Izidor6-Jan-03 5:53
memberIzidor6-Jan-03 5:53 
GeneralRe: Dialog bar button update fix. Pin
Izidor11-Jan-03 21:57
memberIzidor11-Jan-03 21:57 
GeneralRe: Dialog bar button update fix. Pin
Hans Dietrich26-Jul-03 3:25
memberHans Dietrich26-Jul-03 3:25 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160518.1 | Last Updated 27 Dec 2002
Article Copyright 2002 by Roger Allen
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid