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

Last reviewed: November 10, 1997
Article ID: Q140538
1.50 1.51 1.52 | 1.00 2.00 2.10 2.20 4.00 4.10 4.20
WINDOWS        | WINDOWS NT
kbprg kbprint kbhowto kbcode

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 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
    

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.

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;
}

REFERENCES

For more information about the MFC printing, please refer to the Programming with the Microsoft Foundation Class Library in Books Online.


Additional reference words: kbinf 1.50 1.51 1.52 1.00 2.00 2.10 2.20
2.50 2.51 2.52 2.10 3.00 3.10 3.20
KBCategory: kbprg kbprint kbhowto kbcode
KBSubcategory: MfcPrinting
Keywords : MfcPrinting kbcode kbhowto kbprg kbprint
Technology : kbMfc
Version : 1.50 1.51 1.52 | 1.00 2.00 2.10
Platform : NT WINDOWS


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: November 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.