HOWTO: Customize the Common Print Dialog Box

Last reviewed: July 31, 1997
Article ID: Q132909
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2,

         4.0, 4.1, 4.2, 5.0
    

SUMMARY

This article explains how the standard print dialog box can be modified and used in a typical MFC application. Customizing the print dialog involves modifying Window's default common print dialog template. When customizing the common print dialog, you can add new controls and/or remove existing controls.

MORE INFORMATION

In some situations, you may find it necessary to customize the standard print dialog box. This involves modifying the existing print dialog template and modifying the views implementation of printing to use this customized template.

Step-by-Step Procedure

Use this method to implement a customized print dialog:

  1. Copy the PRINTDLGORD dialog template from COMMDLG.RC to the application's .RC file. (In Visual C++ 4.x and 5.0, this dialog template resides in the file INCLUDE\PRNSETUP.DLG) To do this:

    a. Open MSVC\MFC\SAMPLES\APSTUDIO\COMMDLG.RC and your

          application's resource file by using AppStudio (the Resource Editor).
          This file is in \msdev\samples\mfc\general\clipart under 32-bit
          Visual C++.
    

          If you are using Visual C++ 4.x or 5.0, add the line
          #include "windows.h" to the top of the file the file
          INCLUDE\PRNSETUP.DLG. Save and close this file. Reopen it as
          "Resources". (See the "Open As" combo box in the File Open dialog
          box.)
    

    b. In the resource browser window of the "from" file, select the

          PRINTDLGORD (id 1538) dialog resource.
    

    c. As you hold down the CTRL key, drag the resource to the resource

          browser window of the "to" file.
    

          NOTE: Dragging the resource without holding the CTRL key moves
          the resource rather than copies it.
    

  2. Make the necessary changes to the copied dialog template.

    NOTE: None of the controls present in the original dialog template should be deleted. Deleting the controls will cause a problems in the DoDataExchange function of CPrintDialog. Instead, the unwanted controls should be disabled and/or hidden in an overridden OnInitDialog member function of your CPrintDialog-derived class.

  3. Use ClassWizard to add a C++ class (say, CMyPrintDialog) for this dialog template. Derive this new class from CDialog with PRINTDLGORD as the dialog ID. (NOTE: In Visual C++ 4.x and 5.0 this class can be derived directly from CPrintDialog.)

  4. Change all references from CDialog to CPrintDialog in both the header and implementation file of the newly created class. (This step is not necessary if you have derived your class directly from CPrintDialog.)

  5. Because the constructor of CPrintDialog differs from CDialog, modify the constructor of CMyPrintDialog using this code:

    (NOTE: this step is not necessary if you have derived your class directly from CPrintDialog.)

          // Header file of CMyPrintDialog
          class CMyPrintDialog : public CPrintDialog
          {
          // Construction
          public:
    

          // The arguments to the following constructor closely match
          // CPrintDialog. Note the difference in the second argument.
          CMyPrintDialog(BOOL bPrintSetupOnly,
    
                         // TRUE for Print Setup, FALSE for Print Dialog
                           DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES |
                                        PD_HIDEPRINTTOFILE,
                         // Combination of flags. Refer to the Windows SDK
                         // documentation for PRINTDLG structure for a
                         // description of all the flags that can be used.
                            CWnd* pParentWnd = NULL);
    
          // Rest of the class declaration
          ...
    
          DECLARE_MESSAGE_MAP()
          };
    
          // Implementation file of CMyPrintDialog
          CMyPrintDialog::CMyPrintDialog(BOOL bPrintSetupOnly,
            DWORD dwFlags /* = PD_ALLPAGES | PD_USEDEVMODECOPIES |
                          PD_HIDEPRINTTOFILE */,
            CWnd* pParentWnd /* = NULL */)
            : CPrintDialog(bPrintSetupOnly, dwFlags, pParentWnd)
          {
            //{{AFX_DATA_INIT(CMyPrintDialog)
            // NOTE: the ClassWizard will add member initialization here
            //}}AFX_DATA_INIT
          }
    
    

  6. Modify the CView-derived class (say, CMyView) to use the customized print dialog by using this code:

          //  Implementation file of the view (say, in myview.cpp)
          ...
          #include "myprintd.h"  // Include the CMyPrintDialog header file
          ...
    

          // Override OnPreparePrinting of the CView-derived class as below:
          BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
          {
    
             // Delete the CPrintDialog object created in the CPrintInfo
             // constructor, and substitute with customized print dialog.
             delete pInfo->m_pPD;
    
             // Construct and substitute with customized print dialog.
             pInfo->m_pPD = new CMyPrintDialog(FALSE);
    
             // Set the page range.
             pInfo->m_pPD->m_pd.nMinPage = 1;      // one based page numbers
             pInfo->m_pPD->m_pd.nMaxPage = 0xffff; // how many pages is unknown
    
             // Change the PRINTDLG struct so that the custom print dialog will
             // be used.
             pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();
             pInfo->m_pPD->m_pd.lpPrintTemplateName =
                                                  MAKEINTRESOURCE(PRINTDLGORD);
    
             // Set the Flags of the PRINTDLG structure as shown, else the
             // changes will have no effect
             pInfo->m_pPD->m_pd.Flags |= PD_ENABLEPRINTTEMPLATE;
    
             // For details about these flags, refer to the SDK documentation
             // on the PRINTDLG structure.
    
             return DoPreparePrinting(pInfo);
          }
    
    

REFERENCES

For more information, please see the PRINTDLG structure in Windows SDK documentation and OnPreparePrinting and CPrintDialog in Foundation Class documentation.


Additional query words: CFileDialog
Keywords : MfcPrinting kbprg kbprint kbprint
Technology : kbMfc
Version : 1.0 1.5 2.0 4.0 4.1 4.2
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.