Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Preview window closes when print button is clicked in print preview

Preview window closes when print button is clicked in print preview

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 4470909
    wrote on last edited by
    #1

    Hi Friends, I am facing a problem with print preview, actually in the print preview window when we click the print button the preview button closes and print dialog comes up. I want the preview window should not close. Can any one help me how to do that. thanks, pradeep

    R 1 Reply Last reply
    0
    • U User 4470909

      Hi Friends, I am facing a problem with print preview, actually in the print preview window when we click the print button the preview button closes and print dialog comes up. I want the preview window should not close. Can any one help me how to do that. thanks, pradeep

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #2

      You may need to create custom preview class. Have a look at [TN030: Customizing Printing and Print Preview^] I tried some thing now, sorry i don't have easy solution, create a new class CMyPreviewView derived from CPreviewView use #include <afxpriv.h> in stdafx.h overide the Print command handler in the preview view,

      BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
      ON_COMMAND(AFX_ID_PREVIEW_PRINT, &CMyPreviewView::OnPreviewPrint) // this called when print button is pressed
      END_MESSAGE_MAP()

      so that you have the control of print button in CMyPreviewView::OnPreviewPrint and here you won't close the preview window

      void CMyPreviewView::OnPreviewPrint()
      {
      // assuming you want to print the view
      m_pOrigView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
      }

      make your custom preview class as Document preview class by calling DoPrintPreview,

      void CMyViewToBePrinted::OnFilePrintPreview()
      {
      // In derived classes, implement special window handling here
      // Be sure to Unhook Frame Window close if hooked.

      // must not create this on the frame.  Must outlive this function
      CPrintPreviewState\* pState = new CPrintPreviewState;
      
      TRY
      {
      	// DoPrintPreview's return value does not necessarily indicate that
      	// Print preview succeeded or failed, but rather what actions are necessary
      	// at this point.  If DoPrintPreview returns TRUE, it means that
      	// OnEndPrintPreview will be (or has already been) called and the
      	// pState structure will be/has been deleted.
      	// If DoPrintPreview returns FALSE, it means that OnEndPrintPreview
      	// WILL NOT be called and that cleanup, including deleting pState
      	// must be done here.
      
      	if (!DoPrintPreview(AFX\_IDD\_PREVIEW\_TOOLBAR, this,
      							RUNTIME\_CLASS(CMyPreviewView), pState))
      	{
      		// In derived classes, reverse special window handling here for
      		// Preview failure case
      
      		TRACE(traceAppMsg, 0, "Error: DoPrintPreview failed.\\n");
      		AfxMessageBox(AFX\_IDP\_COMMAND\_FAILURE);
      		delete pState;      // preview failed to initialize, delete State now
      	}
      }
      CATCH\_ALL(e)
      {
      	delete pState;
      	THROW\_LAST();
      }
      END\_CATCH\_ALL
      

      }

      modify the View message maps as,

      BEGIN_MESSAGE_MAP(CMyViewToBePrinted, CView)
      // Standard printing commands
      ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)

      U 1 Reply Last reply
      0
      • R Rajkumar R

        You may need to create custom preview class. Have a look at [TN030: Customizing Printing and Print Preview^] I tried some thing now, sorry i don't have easy solution, create a new class CMyPreviewView derived from CPreviewView use #include <afxpriv.h> in stdafx.h overide the Print command handler in the preview view,

        BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
        ON_COMMAND(AFX_ID_PREVIEW_PRINT, &CMyPreviewView::OnPreviewPrint) // this called when print button is pressed
        END_MESSAGE_MAP()

        so that you have the control of print button in CMyPreviewView::OnPreviewPrint and here you won't close the preview window

        void CMyPreviewView::OnPreviewPrint()
        {
        // assuming you want to print the view
        m_pOrigView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
        }

        make your custom preview class as Document preview class by calling DoPrintPreview,

        void CMyViewToBePrinted::OnFilePrintPreview()
        {
        // In derived classes, implement special window handling here
        // Be sure to Unhook Frame Window close if hooked.

        // must not create this on the frame.  Must outlive this function
        CPrintPreviewState\* pState = new CPrintPreviewState;
        
        TRY
        {
        	// DoPrintPreview's return value does not necessarily indicate that
        	// Print preview succeeded or failed, but rather what actions are necessary
        	// at this point.  If DoPrintPreview returns TRUE, it means that
        	// OnEndPrintPreview will be (or has already been) called and the
        	// pState structure will be/has been deleted.
        	// If DoPrintPreview returns FALSE, it means that OnEndPrintPreview
        	// WILL NOT be called and that cleanup, including deleting pState
        	// must be done here.
        
        	if (!DoPrintPreview(AFX\_IDD\_PREVIEW\_TOOLBAR, this,
        							RUNTIME\_CLASS(CMyPreviewView), pState))
        	{
        		// In derived classes, reverse special window handling here for
        		// Preview failure case
        
        		TRACE(traceAppMsg, 0, "Error: DoPrintPreview failed.\\n");
        		AfxMessageBox(AFX\_IDP\_COMMAND\_FAILURE);
        		delete pState;      // preview failed to initialize, delete State now
        	}
        }
        CATCH\_ALL(e)
        {
        	delete pState;
        	THROW\_LAST();
        }
        END\_CATCH\_ALL
        

        }

        modify the View message maps as,

        BEGIN_MESSAGE_MAP(CMyViewToBePrinted, CView)
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)

        U Offline
        U Offline
        User 4470909
        wrote on last edited by
        #3

        Hi Rajkumar, Sorry for late reply. In our application the CPreviewView class was already customized but not the OnPreviewPrint function. so i derived that and added the code. Now when i click the print button in the print preview window the preview window remains and print dialog comes up, but whenever i give OK or CANCEL on the print dialog automatically both the print dialog & preview window closes. I am not understanding why it is happening like that. Did you tried the above in your application, how was the behaviour??

        R 1 Reply Last reply
        0
        • U User 4470909

          Hi Rajkumar, Sorry for late reply. In our application the CPreviewView class was already customized but not the OnPreviewPrint function. so i derived that and added the code. Now when i click the print button in the print preview window the preview window remains and print dialog comes up, but whenever i give OK or CANCEL on the print dialog automatically both the print dialog & preview window closes. I am not understanding why it is happening like that. Did you tried the above in your application, how was the behaviour??

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          Not Sure, I don't have time to debug. BTW, in my application, the preview window is not closing when the print dialog cancel button is pressed. You have the complete MFC source code in Visual Studio SDK folder, you can find the reason by debugging them.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups