HOWTO: Customize the Common Print Dialog Box
ID: Q132909
|
The information in this article applies to:
-
The Microsoft Foundation Classes (MFC), included with:
-
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
-
Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1, 2.2, 4.0, 4.1, 4.2, 5.0, 6.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:
- 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:
- 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++. For Visual C++ 6.0, you can find this file in
..\samples\vc98\mfc\general\clipart on the MSDN CD that ships with
Visual C++ 6.0.
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.)
- In the resource browser window of the "from" file, select the
PRINTDLGORD (id 1538) dialog resource.
- 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.
- 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.
- 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.)
- 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.)
- 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
}
- 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 : kbcode kbprg kbprint kbMFC kbPrinting kbVC kbVC100 kbVC150 kbVC200 kbVC400 kbVC410 kbVC420 kbVC500 kbVC600 kbfaq
Version : 1.0 1.5 2.0 4.0 4.1 4.2 5.0 6.0
Platform : NT WINDOWS
Issue type : kbhowto
|