|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThanks to Chris Maunder for introducing the printing method in Printing without the Document/View framework. Many people have been asking for print preview without the doc/view framework as well. However, so far, no body has proposed a way to do that yet. I have studied on how Microsoft implements print preview in doc/view framework using the class In order to use the default print preview functionality in non doc/view framework, like dialog based applications, I played a trick by creating temporary How to useAdd a function // In your header file ///////////// private: CSingleDocTemplate* m_pTemplate; public: BOOL m_bPrintPreview; void PrintPreview(); //////////////////////////////////// // In your class constructor /////// #include "ViewPrintPreview.h" CGridCtrl::CGridCtrl(...) { // Add initialization m_pTemplate=NULL; m_bPrintPreview=FALSE; } //////////////////////////////////// // In your class /////////////////// void CGridCtrl::PrintPreview() { if (m_bPrintPreview) { AfxGetApp()->m_pMainWnd->SetFocus(); return; } CFrameWnd* pOldFrame=(CFrameWnd*)AfxGetThread()->m_pMainWnd; pOldFrame->ShowWindow(SW_HIDE); //added by eric if (!m_pTemplate) { m_pTemplate = new CSingleDocTemplate( IDR_MENU, NULL, RUNTIME_CLASS(CFrameWnd), RUNTIME_CLASS(CViewPrintPreview)); AfxGetApp()->AddDocTemplate(m_pTemplate); } CFrameWnd * pFrameWnd = m_pTemplate->CreateNewFrame( NULL, NULL ); m_bPrintPreview=TRUE; m_pTemplate->InitialUpdateFrame( pFrameWnd, NULL, FALSE); CViewPrintPreview* pView=(CViewPrintPreview*)pFrameWnd->GetActiveView(); pView->m_pCtrl=this; pView->m_pOldFrame=pOldFrame; AfxGetApp()->m_pMainWnd=pFrameWnd; pFrameWnd->SetWindowText(_T("Koay Kah Hoe Print Preview")); pFrameWnd->ShowWindow(SW_SHOWMAXIMIZED); pView->OnFilePrintPreview(); } A Add the view class Now, you can view the preview window already. However, this is not the end of the story yet, the toolbar buttons cannot update themselves as normally they should be. After some investigation, I found that the window normally calls the message BOOL CGridCtrlDemoDlg::ContinueModal()
{
if (m_Grid.m_bPrintPreview) // m_Grid is your control class
// send WM_IDLEUPDATECMDUI message to update toolbar state
// This is normally called by OnIdle function
// in SDI or MSI applications.
// Dialog based applications don't call OnIdle,
// so send the message from here instead
AfxGetApp()->m_pMainWnd->SendMessageToDescendants(WM_IDLEUPDATECMDUI,
(WPARAM)TRUE, 0, TRUE, TRUE);
return CDialog::ContinueModal();
}
Special precaution has to be taken when you use the print preview. As the FAQ on compilation using static libraryIn dialog based applications, the resource file for the print preview is not included. This problem can be solved by simply including the resource file afxprint.rc to your project file. Open your resource file as text explicitly, add the following line in "#include ""afxprint.rc"" // printing/print preview resources\r\n" At the end of the resource file, do the actual including again as follows, below #include afxprint.rc // printing/print preview resources FAQ on print button in preview toolbarWhen pressing the print button in preview toolbar, the Add a line like below in the message map of your dialog class BEGIN_MESSAGE_MAP(CMyDlg, CDialog) //{{AFX_MSG_MAP(CMyDlg) ... //}}AFX_MSG_MAP ON_COMMAND(ID_FILE_PRINT, OnFilePrint) END_MESSAGE_MAP() Then of course, you must have the handler function In cases where there are more than one printing function in the application, but there is only one general Finally, I wish you happy previewing! :) ViewPrintPreview header file#if !defined(AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_) #define AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 // ViewPrintPreview.h : header file // #include "GridCtrl.h" ///////////////////////////////////////////////////////////////////////////// // CViewPrintPreview view class CViewPrintPreview : public CView { protected: CViewPrintPreview(); // protected constructor used by dynamic creation DECLARE_DYNCREATE(CViewPrintPreview) // Attributes public: CGridCtrl *m_pCtrl; CFrameWnd *m_pOldFrame; // Operations public: virtual void OnFilePrintPreview(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CViewPrintPreview) protected: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView); //}}AFX_VIRTUAL // Implementation protected: virtual ~CViewPrintPreview(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif // Generated message map functions protected: //{{AFX_MSG(CViewPrintPreview) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional // declarations immediately before the previous line. #endif //!defined(AFX_VIEWPRINTPREVIEW_H__137FC880_1607_11D3_9317_8F51A5F9742F__INCLUDED_) ViewPrintPreview implementation file// ViewPrintPreview.cpp : implementation file // #include "stdafx.h" #include "ViewPrintPreview.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CViewPrintPreview IMPLEMENT_DYNCREATE(CViewPrintPreview, CView) CViewPrintPreview::CViewPrintPreview() { m_pOldFrame=NULL; } CViewPrintPreview::~CViewPrintPreview() { } BEGIN_MESSAGE_MAP(CViewPrintPreview, CView) //{{AFX_MSG_MAP(CViewPrintPreview) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CViewPrintPreview drawing void CViewPrintPreview::OnDraw(CDC* pDC) { CDocument* pDoc = GetDocument(); // TODO: add draw code here } ///////////////////////////////////////////////////////////////////////////// // CViewPrintPreview diagnostics #ifdef _DEBUG void CViewPrintPreview::AssertValid() const { CView::AssertValid(); } void CViewPrintPreview::Dump(CDumpContext& dc) const { CView::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CViewPrintPreview message handlers void CViewPrintPreview::OnFilePrintPreview() { CView::OnFilePrintPreview(); } BOOL CViewPrintPreview::OnPreparePrinting(CPrintInfo* pInfo) { return DoPreparePrinting(pInfo); } void CViewPrintPreview::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) { m_pCtrl->OnBeginPrinting(pDC, pInfo); } void CViewPrintPreview::OnPrint(CDC* pDC, CPrintInfo* pInfo) { m_pCtrl->OnPrint(pDC, pInfo); } void CViewPrintPreview::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) { m_pCtrl->OnEndPrinting(pDC, pInfo); } void CViewPrintPreview::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView) { CView::OnEndPrintPreview(pDC, pInfo, point, pView); // Show the original frame m_pOldFrame->ShowWindow(SW_SHOW); // Restore main frame pointer AfxGetApp()->m_pMainWnd=m_pOldFrame; m_pCtrl->m_bPrintPreview=FALSE; // Kill parent frame and itself GetParentFrame()->DestroyWindow(); }
| |||||||||||||||||||||