Microsoft KB Archive/140538

= HOWTO: How to Do Away with the Print Dialog Box in MFC Printing =

Article ID: 140538

Article Last Modified on 11/21/2006

-

APPLIES TO

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

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 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++ 4.2 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q140538

<div class="summary_section">

SUMMARY
By design, a Print dialog box is displayed in MFC printing. This dialog box allows the user to change the printer settings before a printer device context (DC) is created. However, there are cases when the print dialog box is not a good idea. This article provides a way to bypass the Print dialog box for a print job.

<div class="moreinformation_section">

MORE INFORMATION
CView::OnPreparePrinting is the first virtual function called during the print process. It is also called during print preview. AppWizard provides an implementation of OnPreparePrinting that calls CView::DoPreparePrinting. DoPreparePrinting displays the print dialog box for actual printing but not for print preview.

Therefore, in OnPreparePrinting, if CPrintInfo::m_bPreview is FALSE (an actual print job), set it to TRUE before calling DoPreparePrinting. This makes DoPreparePrinting believe that the current context is that of print preview so the print dialog box isn't shown. After DoPreparePrinting returns, reset the variable to FALSE to let the print job go on uninterrupted.

The following sample code prevents the Print dialog box from appearing without affecting the print process.

Sample Code
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) {     if ( pInfo->m_bPreview )        // normal print preview return DoPreparePrinting(pInfo);

// actual printing pInfo->m_bPreview = TRUE; BOOL bRetval = DoPreparePrinting( pInfo ); pInfo->m_bPreview = FALSE; return bRetval; }

<div class="references_section">