Customizing the Common Print Dialog
This article describes how to customize the default print dialog to include extra options included in your application. This procedure works for all common dialog except for the file dialog, see the related codeguru article for that.
The basic steps are as follows:
- Subclass the CPrintDialog class in a new class (e.g. CMyPrintDialog).
- In the constructor, tell it to use a custom template.
#include <Dlgs.h> CMyPrintDialog::CMyPrintDialog(BOOL bPrintSetupOnly, DWORD dwFlags, CWnd* pParentWnd) : CPrintDialog(bPrintSetupOnly, dwFlags, pParentWnd) { m_pd.lpPrintTemplateName = (LPTSTR) MAKEINTRESOURCE(PRINTDLGORD); m_pd.Flags |= PD_ENABLEPRINTTEMPLATE; m_pd.hInstance = AfxGetInstanceHandle(); AfxGetApp()->GetPrinterDeviceDefaults(&m_pd); }The important thing is that the lpPrintTemplateName and hInstance members of the m_pd structure are filled in when the PD_ENABLEPRINTTEMPLATE flag is set.
- Create the custom template identified by PRINTDLGORD by modifying
a copy of the default template. The default template can be found in the file
PRNSETUP.DLG and the identifiers used in the dialog are defined in DLGS.H.
Choose "Resource Includes" from the VIEW menu and add the following
lines to the "read-only symbol directives".
#include <Dlgs.h> #include <PRNSETUP.DLG>
Now save the workspace and reopen it. You now have three new dialog resources, modify the print dialog (called 1538) and you can delete the others. Remove the include for PRNSETUP.DLG from the resource includes to prevent conflicts. Do not remove the DLGS.H include.
- Modify the OnPreparePrint function to use your customize print class.
BOOL CMyprintView::OnPreparePrinting(CPrintInfo* pInfo) { delete pInfo->m_pPD; pInfo->m_pPD = new CMyPrintDialog(FALSE); return DoPreparePrinting(pInfo); }
For other common dialogs you can repeat the procedure but subclass different base classes and use other templates.
Related MSDN articles
http://msdn.microsoft.com/library/sdkdoc/winui/commdlg3_4qlv.htmhttp://msdn.microsoft.com/library/sdkdoc/winui/commdlg3_68qg.htm

Comments
Error if I call the dialog second time
Posted by Legacy on 05/13/2003 12:00amOriginally posted by: Trifon
ReplyYes, I'd like to know how to print to a file too?
Posted by Legacy on 11/19/2002 12:00amOriginally posted by: jm
I have found trying to do this is Visual Basic, that the
name of the file dialog box will come up. Can you specify a file name in the program so that box doesn't come up?
Thanks in advance.
ReplyHekp! How to hook the printer port
Posted by Legacy on 11/02/2002 12:00amOriginally posted by: Ashok
Hai
ReplyI desperately in need of help. Whenever an application tries to print a job in any printer in Win 9x/ME and NT/2000
I should have throu my program to cancel or accept the job and also to get the details abt the job like author, application how many pages, graphics options etc.
Thank you
Ashok
Page Numbers From TO
Posted by Legacy on 09/04/2002 12:00amOriginally posted by: alan
Why is the 2nd radio button in print setup (Pages - From: To:) disabled?
This makes no sense. I am trying every possible flag but it stays disabled. ??
ReplyVC++ print problems with Windows 2000
Posted by Legacy on 01/08/2002 12:00amOriginally posted by: Bob Toney
VC++ application. It has been running on Windows 3.1 through Windows 98 with out a problem. New clients are using Windows 2000. The domodal to display the print options screen causes an error.
ReplyAny ideas would be appricated.
Why can't I see the print dialog?
Posted by Legacy on 06/12/2001 12:00amOriginally posted by: Xing Da
I do the job step by step as the example.
ReplyBut at the 3th step, I save the workspace and reopen it.
There isnot the print dialog displayed in the resource.
However, I do a little changing in the .rc by some other editors, and then reopen the workspace, those display normally.Can anybody tell me why?
very good, but what to do now?
Posted by Legacy on 05/03/2001 12:00amOriginally posted by: Dieter Hammer
Thanks for this good article. But how can I work with this dialog ? If i derive from CPrintDialog I can't work with it in classwizard, if I derive from CDialog ( and change with hand to CPrintDialog) it doesn't work. Even the downloaded code of the example doesn't work here. I can see the new controls, but I can't use them in the program. What do I wrong ?
ReplyTrouble with UNICODE
Posted by Legacy on 01/26/2001 12:00amOriginally posted by: Georg Solbach
Replyhow can I reduce the paper size options
Posted by Legacy on 12/09/2000 12:00amOriginally posted by: Erich Ruth
The print dialog box has a paper size options. There are
lots of options. How can I reduce the paper size options to just letter (8 1/2 x 11) and legal (8 1/2 x 14) size paper? I want to elminate all the other paper size options.
Please, any response you can give me will be greatly appreciated.
Sincerely,
ReplyErich J. Ruth
Help me....
Posted by Legacy on 12/07/2000 12:00amOriginally posted by: vikash
ReplyLoading, Please Wait ...