Click here to Skip to main content
11,411,922 members (64,951 online)
Click here to Skip to main content

An easy way to customise the default Print dialog in an MFC app

, 16 Jul 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
A quick and easy way to customise the CPrintDialog object in an MFC application

Sample Image - PrintDialog.jpg


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 customise 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 box's can be found here, so if you need to know a particular controls ID number, this is the place to look! See also the file Dlgs.h which includes the #define's of the actual controls on all the common dialog templates.

FONT 8, "MS Sans Serif"
    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 |
    CONTROL         "Pa&ges",rad3,"Button",BS_AUTORADIOBUTTON,16,122,36,12
    CONTROL         "&Selection",rad2,"Button",BS_AUTORADIOBUTTON,16,138,64,
    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 |
    DEFPUSHBUTTON   "OK",IDOK,180,164,48,14,WS_GROUP
    PUSHBUTTON      "Cancel",IDCANCEL,232,164,48,14

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 that 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 you 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:

    // 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!



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.

Comments and Discussions

QuestionNewbie Question PinmemberMember 372884115-Dec-08 19:15 
GeneralDialog Fails To Appear Pinmembermeow8812-Nov-07 15:27 
GeneralPrinter Status Dialog PinmemberKarthikeyan U17-Sep-06 23:26 
QuestionHow to call customize printDialog from another dialog instead of CView? Pinmembererha_kong24-Jun-06 16:52 
GeneralI cannot unzip "" Pinmemberrogerju13-Jun-06 23:36 
GeneralThank you Pinmemberdel Fuego8-Jun-06 6:05 
GeneralCollate icon PinmemberDavid Kentley26-Jan-06 6:29 
GeneralRe: Collate icon Pinmembermzhunix2-Apr-08 14:37 
AnswerRe: Collate icon Pinmemberpopsoftheyear16-Apr-09 9:33 
Thanks Mickaël. Much needed. And for anyone else with this problem, here's a slightly abridged version:

// The setup dialog doesn't have this collate icon and will crash
if (!(m_pd.Flags & PD_PRINTSETUP)) {
  // @@SW - From
  // plus an adjusted height and check for print setup vs print
  RECT rect1, rect2;
  // CWnd for static "Number of copies"
  CWnd* pCwnd = GetDlgItem(stc5);
  // CWnd for static "Icon Collate"
  pCwnd = GetDlgItem(ico3);
  // It's sized to 32 naturally, which cuts off the bottom portion of the collate icon.
  rect2.bottom = + 40;   
  rect2.left = rect1.left;
  rect2.right = rect1.right;
  pCwnd->MoveWindow(&rect2, true);

God bless
GeneralPrintDialog PinmemberF.Hashemi25-Sep-05 3:42 
Questionhow to custommize the default printsetup dialog? Pinmemberuseresu30-Mar-05 1:33 
GeneralResource File .rc Pinmembercjpm1008-Mar-05 7:16 
GeneralSetting the Print To File directory Pinmemberthermal32619-Jan-05 12:39 
GeneralSelect Printer Pinmemberpel-nn15-Nov-04 18:55 
QuestionHow to find the printer paper name? PinmemberValentin Mocanu8-Oct-04 4:44 
QuestionHow to enable print/print preview command in File menu? PinmemberSiang Loong30-Aug-04 3:20 
AnswerRe: How to enable print/print preview command in File menu? PinmemberSiang Loong30-Aug-04 9:24 
QuestionHow to set Landscape printing option by default PinmemberJHAKAS21-Aug-04 5:03 
QuestionCustomizing the printer NAME field? Pinmemberercarignan13-Aug-04 6:23 
GeneralCustomizing PageSetupDlg Pinmemberrajas18-Jun-04 12:41 
QuestionHow to enable Pages radio button? Pinmemberabicer29-Apr-04 1:20 
AnswerRe: How to enable Pages radio button? PinmemberIneluki11-May-04 3:24 
AnswerRe: How to enable Pages radio button? Pinmember-Dy5-Aug-05 4:36 
GeneralProperties Dlg Not Working PinsussTurab110-Mar-04 21:21 
GeneralNewbie question Pinmemberlapc3-Oct-03 4:42 
QuestionAnybody knows how to port this .NET? Pinmemberjwang774507-Sep-03 11:55 
AnswerRe: Anybody knows how to port this .NET? PinmemberCloaca19-Jan-04 19:10 
GeneralRe: Anybody knows how to port this .NET? PinsussAnonymous20-Jan-04 6:31 
GeneralRe: Anybody knows how to port this .NET? PinmemberTDWiseley23-Feb-07 6:56 
Generalit is not working for me PinmemberMuhammad Kamran25-Aug-03 6:48 
GeneralRe: it is not working for me PinmemberRoj8-Sep-06 23:35 
GeneralCaveat for resource only dll Pinmembermikeh8-Aug-03 12:15 
GeneralProblem with Number of Copies Spin Control PinmemberHal Director7-Aug-03 6:48 
GeneralRe: Problem with Number of Copies Spin Control PinmemberHal Director8-Aug-03 5:30 
GeneralLink Dynamically with MFC Pinmemberjwalto1@no.spam.umbc.edu13-May-03 10:09 
GeneralGood but how about Add Printer Listview Pinmemberalan9324-Oct-02 7:48 
GeneralExcellent info...question PinsussTom Pullara8-Oct-02 10:08 
GeneralCareful if loading into resource editor PinsussAnonymous8-Oct-02 0:43 
GeneralCPageSetupDialog Pinmemberdours7-Oct-02 1:19 
QuestionHow to set Number of copies? PinmemberThomasKennedy11-Sep-02 20:12 
AnswerRe: How to set Number of copies? PinmemberRoger Allen12-Sep-02 0:16 
AnswerRe: How to set Number of copies? Pinmemberjwalto1@no.spam.umbc.edu13-May-03 14:44 
QuestionNot finding resource? Pinsussdrhender30-Aug-02 10:15 
QuestionHow to initialize my own controls ? PinsussAnonymous29-Aug-02 3:06 
AnswerRe: How to initialize my own controls ? PinmemberRoger Allen29-Aug-02 4:30 
GeneralRe: How to initialize my own controls ? PinsussAnonymous30-Aug-02 3:10 
GeneralRe: How to initialize my own controls ? Pinmembersachelis16-Jul-03 17:46 
Questionhow about PageSetup? PinsussAnonymous15-Aug-02 18:49 
AnswerRe: how about PageSetup? PinmemberRoger Allen16-Aug-02 0:59 
GeneralGreat!!! PinmemberMohit Khanna18-Jul-02 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150414.5 | Last Updated 17 Jul 2002
Article Copyright 2002 by Roger Allen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid