Microsoft KB Archive/131792

= PRB: OnClose Is Called When Closing Print Preview Dialog Box =

Article ID: 131792

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++ 5.0 Learning Edition</li></ul>

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

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

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

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

-

<div class="notice_section">

This article was previously published under Q131792

<div class="symptoms_section">

SYMPTOMS
OnClose is called when the user closes the Print Preview dialog box.

<div class="cause_section">

CAUSE
If your frame window class is CMainFrame, for example, you can override the OnClose member to perform special processing (such as putting up a message box asking users if they really want to quit), but note that CMainFrame::OnClose is also called when the user dismisses the Print Preview dialog by pressing ALT+F4 or by double-clicking the upper left corner of the dialog.

NOTE: OnClose is not called when the user clicks the Close button in the Print Preview dialog box.

<div class="resolution_section">

RESOLUTION
In your OnClose function, you do not want to do the same processing during a Print Preview close that you do during an application close. Therefore, you should make a special case in the OnClose code to check whether or not it is being called from Print Preview.

The following code demonstrates how to modify the OnClose to do nothing during the Print Preview close. void CMainFrame::OnClose {   if (m_bPreview ||       (AfxMessageBox(ID_EXIT_PROMPT, MB_YESNO) == IDYES)) {       CFrameWnd::OnClose; }   } In this example, ID_EXIT_PROMPT is a string table resource containing the string that you want to display in the message box. In addition, m_bPreview is a member that you need to add to the CMainFrame class; it is initialized to FALSE in the constructor of the CMainFrame class. Then, the m_bPreview variable is set again using an override of CFrameWnd::OnSetPreviewMode. // In .h file of CMainFrame class: class CMainFrame : public CFrameWnd {   BOOL m_bPreview;  // TRUE in print preview mode; FALSE otherwise ...   };

// In .cpp file of CMainFrame class: CMainFrame::CMainFrame {   m_bPreview = FALSE; }

void CMainFrame::OnSetPreviewMode(   BOOL bPreview, CPrintPreviewState* pModeStuff)

{   m_bPreview = bPreview; CFrameWnd::OnSetPreviewMode(bPreview, pModeStuff); }

<div class="status_section">

STATUS
This behavior is by design.

<div class="references_section">