Click here to Skip to main content
15,883,901 members
Articles / Desktop Programming / MFC

An Easy Way to Customise the Default Print Dialog in an MFC App

Rate me:
Please Sign up or sign in to vote.
4.62/5 (18 votes)
16 Jul 2002CPOL2 min read 230.5K   2.3K   42   57
A quick and easy way to customise the CPrintDialog object in an MFC application

Sample Image - PrintDialog.jpg

Overview

The CPrintDialog box is a standard system supplied resource used by just about every program under the sun that needs to print. But what if you need to customize it? What I present here is a quick and easy way to do this.

Stage 1: Copy the Common Dialogs Resource Template

The first stage is to create in your project an exact copy of the existing dialog template used by the CPrintDialog class. I did this by copying the default source of this and pasting it into my applications .rc file. The default content of these can be found in the file PrnSetup.Dlg of your Visual C++ VC98\Include directory. In fact, the default dialog templates for all the common dialog boxes can be found here, so if you need to know a particular control's ID number, this is the place to look! See also the file Dlgs.h which includes the #defines of the actual controls on all the common dialog templates.

C++
PRINTDLGORD DIALOG DISCARDABLE  32, 32, 288, 186
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU |
      DS_CONTEXTHELP | DS_3DLOOK
CAPTION "Print"
FONT 8, "MS Sans Serif"
BEGIN
    GROUPBOX        "Printer",grp4,8,4,272,84,WS_GROUP
    LTEXT           "&Name:",stc6,16,20,36,8
    COMBOBOX        cmb4,52,18,152,152,CBS_DROPDOWNLIST | CBS_SORT |
                    WS_VSCROLL | WS_GROUP | WS_TABSTOP
    PUSHBUTTON      "&Properties",psh2,212,17,60,14,WS_GROUP
    LTEXT           "Status:",stc8,16,36,36,10,SS_NOPREFIX
    LTEXT           "",stc12,52,36,224,10,SS_NOPREFIX | SS_LEFTNOWORDWRAP
    LTEXT           "Type:",stc7,16,48,36,10,SS_NOPREFIX
    LTEXT           "",stc11,52,48,224,10,SS_NOPREFIX | SS_LEFTNOWORDWRAP
    LTEXT           "Where:",stc10,16,60,36,10,SS_NOPREFIX
    LTEXT           "",stc14,52,60,224,10,SS_NOPREFIX | SS_LEFTNOWORDWRAP
    LTEXT           "Comment:",stc9,16,72,36,10,SS_NOPREFIX
    LTEXT           "",stc13,52,72,152,10,SS_NOPREFIX | SS_LEFTNOWORDWRAP
    CONTROL         "Print to fi&le",chx1,"Button",BS_AUTOCHECKBOX |
                    WS_GROUP | WS_TABSTOP,212,70,64,12
    GROUPBOX        "Print range",grp1,8,92,144,64,WS_GROUP
    CONTROL         "&All",rad1,"Button",BS_AUTORADIOBUTTON | WS_GROUP |
                    WS_TABSTOP,16,106,64,12
    CONTROL         "Pa&ges",rad3,"Button",BS_AUTORADIOBUTTON,16,122,36,12
    CONTROL         "&Selection",rad2,"Button",BS_AUTORADIOBUTTON,16,138,64,
                    12
    RTEXT           "&from:",stc2,52,124,20,8
    EDITTEXT        edt1,74,122,26,12,WS_GROUP | ES_NUMBER
    RTEXT           "&to:",stc3,100,124,16,8
    EDITTEXT        edt2,118,122,26,12,WS_GROUP | ES_NUMBER
    GROUPBOX        "Copies",grp2,160,92,120,64,WS_GROUP
    LTEXT           "Number of &copies:",stc5,168,108,68,8
    EDITTEXT        edt3,240,106,32,12,WS_GROUP | ES_NUMBER
    ICON            "",ico3,162,124,76,24,WS_GROUP | SS_CENTERIMAGE
    CONTROL         "C&ollate",chx2,"Button",BS_AUTOCHECKBOX | WS_GROUP |
                    WS_TABSTOP,240,130,36,12
    DEFPUSHBUTTON   "OK",IDOK,180,164,48,14,WS_GROUP
    PUSHBUTTON      "Cancel",IDCANCEL,232,164,48,14
END

You will have to modify it slightly. In the example above, you would need to change the PRINTDLGORD to the name of the dialog resource you want to call it locally in your own application. In my case, this was IDD_PRINT. Make sure that when you do this, the #define IDD_PRINT x exists in your resource.h file.

Stage 2: Add Controls and Wrap the Dialog Template

Add any controls to the dialog and lay it out as you need, then invoke ClassWizard so that you can wrap your new dialog resource in a class object. Name it what you want, but make sure you select the base class as CPrintDialog. Once this has been created, you can add message handler, etc. for any extra buttons that you add to your dialog resource.

Stage 3: Using Your New CMyPrintDialog Class

To get your application to use this new CPrintDialog dialog, you need to add the following code to your CView::OnPreparePrinting() function:

C++
// replace the standard CPrintDialog with our custom one!
delete pInfo->m_pPD ; // release previous MFC allocated dialog object
pInfo->m_pPD = new CMyPrintDialog(false) ;
pInfo->m_pPD->m_pd.nMinPage = 1 ;
pInfo->m_pPD->m_pd.nMaxPage = 0xffff ;
pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle() ;
pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(IDD_PRINT) ;
pInfo->m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE ;

Stage 4: Compile and Run Your Application

That's it! You just need to write your handler from there!

Enjoy!

License

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


Written By
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]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions

 
AnswerRe: How to set Number of copies? Pin
Jeffrey Walton13-May-03 13:44
Jeffrey Walton13-May-03 13:44 
QuestionNot finding resource? Pin
drhender30-Aug-02 9:15
drhender30-Aug-02 9:15 
QuestionHow to initialize my own controls ? Pin
Anonymous29-Aug-02 2:06
Anonymous29-Aug-02 2:06 
AnswerRe: How to initialize my own controls ? Pin
Roger Allen29-Aug-02 3:30
Roger Allen29-Aug-02 3:30 
GeneralRe: How to initialize my own controls ? Pin
Anonymous30-Aug-02 2:10
Anonymous30-Aug-02 2:10 
GeneralRe: How to initialize my own controls ? Pin
sachelis16-Jul-03 16:46
sachelis16-Jul-03 16:46 
Questionhow about PageSetup? Pin
Anonymous15-Aug-02 17:49
Anonymous15-Aug-02 17:49 
AnswerRe: how about PageSetup? Pin
Roger Allen15-Aug-02 23:59
Roger Allen15-Aug-02 23:59 
I did a quick check. You should be able to do this for Page Setup. The dialog resource is in PRNSETUP.DLG. You can copy that across to your .RC file as in the article. From there you can wrap it in base class derived from CPrintDialog. You will then have to edit the created class and change all occurances of CPRintDialog to CPageSetupDialog (as this is not listed in class wizard when you want to wrap the dialog).

The only thing I am not sure on is where CPageSetupDialog needs to be used to replace the existing page setup dialog, but you should be able to find this out by searching the MFC code for CPageSetupDialog and putting breakpoints on the existing code. You will then be able to trace the call stack and find out where it is used.

HTH


Roger Allen
Sonork 100.10016

I think I need a new quote, I am on the prowl, so look out for a soft cute furry looking animal, which is really a Hippo in disguise. Its probably me.
GeneralGreat!!! Pin
Mohit Khanna18-Jul-02 4:46
Mohit Khanna18-Jul-02 4:46 
GeneralRe: Great!!! Pin
Roger Allen18-Jul-02 4:56
Roger Allen18-Jul-02 4:56 
GeneralRe: Great!!! Pin
Mohit Khanna18-Jul-02 5:02
Mohit Khanna18-Jul-02 5:02 
GeneralRe: Great!!! Pin
Roger Allen18-Jul-02 5:11
Roger Allen18-Jul-02 5:11 
GeneralRe: Great!!! Pin
peirr lee25-Jul-02 16:35
peirr lee25-Jul-02 16:35 
GeneralVery nice! Pin
Nish Nishant17-Jul-02 14:53
sitebuilderNish Nishant17-Jul-02 14:53 
GeneralRe: Very nice! Pin
Roger Allen18-Jul-02 0:05
Roger Allen18-Jul-02 0:05 

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.