Microsoft KB Archive/132909

= How to customize the common print dialog box in an MFC application =

Article ID: 132909

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual Studio .NET 2003 Professional Edition</li></ul>

<ul> <li>Microsoft Visual Studio .NET 2003 Enterprise Developer</li></ul>

<ul> <li>Microsoft Visual Studio .NET 2002 Professional Edition</li></ul>

<ul> <li>Microsoft Visual Studio .NET 2002 Enterprise Developer</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q132909

<div class="summary_section">

SUMMARY
This article explains how to modify and use the standard print dialog box in a typical MFC application. Customizing the print dialog box involves modifying the Microsoft Windows default common print dialog box template. When you customize the common print dialog box, you can add new controls and remove existing controls.

<div class="moreinformation_section">

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

Step-by-Step Procedure
To implement a customized print dialog box, follow these steps: <ol> <li>To copy the PRINTDLGORD dialog box template (the ID is 1538) from Commdlg.rc to your application .rc file, follow these steps.

Note In Visual C++ 4.x and 5.0, this dialog box template resides in the file Include\Prnsetup.dlg. In Visual C++ 6.0, this file is located in the \Samples\Vc98\Mfc\General\Clipart on the MSDN CD. In Microsoft Visual C++ .NET, this file is located in the \Samples\Vc\Mfc\General\Clipart folder on the MSDN CD. <ol style="list-style-type: lower-alpha;"> <li>Open Commdlg.rc.</li> <li>Enable cut-and-pasting of resources in your project resource file: <ol style="list-style-type: lower-alpha;"> <li>If you are using Visual C++ .NET, open your project .rc file in Resource Editor mode (instead of the default Resource View mode). To do this, close the Resource View window of your project. Find your project .rc file in the Solution Explorer window. Right-click the .rc file in the Solution Explorer window, and then click Open With. In the Open With dialog box, click to select Resource Editor as the program with which to open the .rc file, and then click Open.</li> <li>If you are using Visual C++ 4.x or 5.0, add the line #include "windows.h" to the top of the file Include\prnsetup.dlg. Save and close this file. Reopen it as "Resources" by using the Open As box in the File Open dialog box.</li></ol> </li> <li>In the Resource Editor window of the Commdlg.rc file, click the PRINTDLGORD dialog resource, click Edit menu, and then click Copy. The PRINTDLGORD ID is 1538. This PRINTDLGORD symbol is defined in the header file Dlgs.h.</li> <li>In the Resource Editor window of your project .rc file, right-click Dialog and then click Paste. Now, save and close the project .rc file in the Resource Editor window, and then reopen the default Resource View window of your project. If you are using Visual C++ 6.0, there are no context menus for copying resources. On the File menu, click Open to open the Commdlg.rc file, and then open the Resource View menu for your project. Select the 1538 resource ID in Commdlg.rc, hold down the CTRL key, and then drag the dialog box resource from the Commdlg.rc window to your Resource View window.</li></ol> </li> <li><ol style="list-style-type: lower-alpha;"> <li>If you are using Visual C++ .NET, follow these steps: <ol> <li>Click Add Class on the Project menu.</li> <li>The Add Class menu is context-sensitive. The Add Class menu does not appear when the Resource View is active. Switch to Solution Explorer before you click Add Class on the Project menu.</li> <li>Expand the MFC category, click the MFC class template, and then click Open.</li> <li>Use the MFC Class Wizard to add a Visual C++ class for this dialog box template, such as CMyPrintDialog. Derive this new class from CPrintDialog.</li></ol> </li> <li>If you are using Visual C++ 6.0, click Add Class on the Project menu.

The Add Class menu is context-sensitive. The Add Class menu does not appear when the Resource View is active. Switch to ClassView before you click Add Class on the Project menu. Type the name for the class, set the Base Class as CPrintDialog, and then click OK.</li></ol> </li> <li>Change all references from CDialog to CPrintDialog in both the header and implementation file of the newly-created class. Note that this step is not necessary if you have derived your class directly from CPrintDialog.</li> <li> Because the constructor of CPrintDialog differs from CDialog, modify the constructor of CMyPrintDialog by using the following 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 }                   </li> <li>Make any desired changes to the copied dialog box template and associated source code. NOTE: Do not delete any of the controls that are present in the original dialog box template. Deleting the controls causes problems in the DoDataExchange function of CPrintDialog. Instead, either disable the unwanted controls or hide them in an overridden OnInitDialog member function of your CPrintDialog-derived class.</li> <li> Modify the CView-derived class (for example, CMyView) to use the customized print dialog box by using the following code: // Implementation file of the view (for example, in myview.cpp). ...     #include "MyPrintDialog.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 box will // be used. pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle; pInfo->m_pPD->m_pd.lpPrintTemplateName = MAKEINTRESOURCE(1538);

// 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); } </li></ol>

<div class="references_section">