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:

  1. Subclass the CPrintDialog class in a new class (e.g. CMyPrintDialog).
  2. 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.

  3. 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.

  4. 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.htm
http://msdn.microsoft.com/library/sdkdoc/winui/commdlg3_68qg.htm

Downloads

Download demo project – 54 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read