Preview window closes when print button is clicked in print preview
-
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
-
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
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) -
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)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??
-
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??
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.