Microsoft & .NETVisual C#Customizing the Common Print Dialog

Customizing the Common Print Dialog content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article was contributed by Martijn van Welie.

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.hInstance = AfxGetInstanceHandle();

    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.


Download demo project – 54 Kb


Date Posted: December 20, 1999

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories