• MFC对话框的打印预览实现


    1.创建MFC对话框工程,选择在共享库中调用。

    2.添加文件

    MainFrm.h, MyPreviewView.h, WrapperView.h, MainFrm.cpp, WrapperView.cpp

    分别为:

    MainFrm.h

     1 // MainFrm.h : interface of the CMainFrame class
     2 //
     3 /////////////////////////////////////////////////////////////////////////////
     4  
     5 #if !defined(AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_)
     6 #define AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_
     7  
     8 #if _MSC_VER > 1000
     9 #pragma once
    10 #endif // _MSC_VER > 1000
    11 #include "wrapperview.h"
    12  
    13 class CMainFrame : public CFrameWnd
    14 {  
    15 public:    
    16     CMainFrame(DRAWFUN pDraw,CWnd*pOldW,CWnd* pCallW,BOOL bDirect,LPCTSTR stTitle=NULL);   
    17     CWnd*   pOldWnd;
    18     CWnd*   pCallWnd;
    19     DRAWFUN Draw;
    20     BOOL    bDirectPrint;
    21 protected:
    22     DECLARE_DYNAMIC(CMainFrame)
    23 public:
    24 // Overrides
    25     // ClassWizard generated virtual function overrides
    26     //{{AFX_VIRTUAL(CMainFrame)
    27     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    28     virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
    29     //}}AFX_VIRTUAL
    30 // Implementation
    31 public:
    32     virtual ~CMainFrame();
    33 #ifdef _DEBUG
    34     virtual void AssertValid() const;
    35     virtual void Dump(CDumpContext& dc) const;
    36 #endif
    37  
    38 protected:  // control bar embedded members
    39     CStatusBar  m_wndStatusBar;
    40     CToolBar    m_wndToolBar;
    41     CWrapperView  *m_pView;
    42  
    43 // Generated message map functions
    44 protected:
    45     //{{AFX_MSG(CMainFrame)
    46     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);   
    47     //}}AFX_MSG
    48     DECLARE_MESSAGE_MAP()
    49 public:
    50     afx_msg void OnClose();
    51 };
    52 /////////////////////////////////////////////////////////////////////////////
    53 //{{AFX_INSERT_LOCATION}}
    54 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    55 #endif // !defined(AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_)

    MyPreviewView.h

    //MyPreviewView.h
    
    #if !defined(AFX_MYPREVIEWVIEW_H__9FCA8680_907C_11D3_8412_9D0C7145F5C4__INCLUDED_)
    #define AFX_MYPREVIEWVIEW_H__9FCA8680_907C_11D3_8412_9D0C7145F5C4__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // MyPreviewView.h : header file
    //
    #include <afxpriv.h>
    
    /////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView view
    
    class CMyPreviewView : public CPreviewView
    {
    protected:
        CMyPreviewView();           // protected constructor used by dynamic creation
        virtual ~CMyPreviewView();
        DECLARE_DYNCREATE(CMyPreviewView)
    
    public:
        friend class CWrapperView;
    
    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CMyPreviewView)
        protected:
        virtual void OnDraw(CDC* pDC);      // overridden to draw this view
        virtual void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView);
        //}}AFX_VIRTUAL
    
    // Implementation
    protected:
    
    #ifdef _DEBUG
        virtual void AssertValid() const;
        virtual void Dump(CDumpContext& dc) const;
    #endif
    
        virtual void OnActivateView(BOOL bActivate,CView* pActivateView, CView* pDeactiveView);
    
        // Generated message map functions
        //{{AFX_MSG(CMyPreviewView)
        afx_msg void OnPreviewClose();
        afx_msg void OnPreviewPrint();
            // NOTE - the ClassWizard will add and remove member functions here.
        //}}AFX_MSG
    
        friend BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd);
    
        DECLARE_MESSAGE_MAP()
    };
    
    /////////////////////////////////////////////////////////////////////////////
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_MYPREVIEWVIEW_H__9FCA8680_907C_11D3_8412_9D0C7145F5C4__INCLUDED_)

    WrapperView.h

     1 //WrapperView.h
     2 
     3 #if !defined(AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_)
     4 #define AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_
     5 
     6 #if _MSC_VER > 1000
     7 #pragma once
     8 #endif // _MSC_VER > 1000
     9 // WrapperView.h : header file
    10 //
    11 
    12 #include "MyPreviewView.h"
    13 #include "MainFrm.h"
    14 
    15 /////////////////////////////////////////////////////////////////////////////
    16 // CWrapperView view
    17 
    18 class CWrapperView : public CScrollView
    19 {
    20 protected:
    21     DECLARE_DYNCREATE(CWrapperView)
    22     
    23     CFrameWnd*    m_pFrameWnd;
    24 public:
    25     CWrapperView();
    26     virtual ~CWrapperView();
    27 
    28     virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    29     void OnFilePrintPreview(CFrameWnd *pFrame);
    30     BOOL DoPrintPreview(UINT nIDResource, CView* pPrintView,CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState);
    31     void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo,POINT, CMyPreviewView* pView);
    32 // Overrides
    33     // ClassWizard generated virtual function overrides
    34     //{{AFX_VIRTUAL(CWrapperView)
    35     protected:
    36     virtual void OnPrint(CDC* pDC,CPrintInfo* pInfo);      // overridden to draw this view
    37     virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    38     virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    39     //}}AFX_VIRTUAL
    40 
    41 // Implementation
    42 protected:
    43     
    44 #ifdef _DEBUG
    45     virtual void AssertValid() const;
    46     virtual void Dump(CDumpContext& dc) const;
    47 #endif
    48 
    49     // Generated message map functions
    50 protected:
    51     //{{AFX_MSG(CWrapperView)
    52         // NOTE - the ClassWizard will add and remove member functions here.
    53     afx_msg void OnFilePrint();
    54     //}}AFX_MSG
    55     DECLARE_MESSAGE_MAP()
    56 };
    57 
    58 /////////////////////////////////////////////////////////////////////////////
    59 
    60 //{{AFX_INSERT_LOCATION}}
    61 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    62 
    63 #endif // !defined(AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_)

    MainFrm.cpp

      1 // MainFrm.cpp : implementation of the CMainFrame class
      2 //
      3 
      4 #include "stdafx.h"
      5 #include "MainFrm.h"
      6 
      7 #ifdef _DEBUG
      8 #define new DEBUG_NEW
      9 #undef THIS_FILE
     10 static char THIS_FILE[] = __FILE__;
     11 #endif
     12 
     13 
     14 /////////////////////////////////////////////////////////////////////////////
     15 // CMainFrame
     16 
     17 IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
     18 
     19 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
     20     //{{AFX_MSG_MAP(CMainFrame)
     21     ON_WM_CREATE()    
     22     ON_WM_CLOSE()
     23     //}}AFX_MSG_MAP
     24 END_MESSAGE_MAP()
     25 
     26 static UINT indicators[] =
     27 {
     28     ID_SEPARATOR,           // status line indicator
     29     ID_INDICATOR_CAPS,
     30     ID_INDICATOR_NUM,
     31     ID_INDICATOR_SCRL,
     32 };
     33 
     34 /////////////////////////////////////////////////////////////////////////////
     35 // CMainFrame construction/destruction
     36 
     37 CMainFrame::CMainFrame(DRAWFUN pDraw,CWnd*pOldW,CWnd* pCallW,BOOL bDirect,LPCTSTR stTitle)
     38 {
     39     // TODO: add member initialization code here    
     40     Draw    =pDraw;
     41     pOldWnd    =pOldW;
     42     pCallWnd=pCallW;
     43     bDirectPrint=bDirect;
     44     CString ss;
     45     if(stTitle!=NULL)
     46         ss=stTitle;
     47     else
     48     {        
     49         pOldW->GetWindowText(ss);
     50         if(!ss.IsEmpty())
     51             ss=ss+_T(" (预览)");
     52         else
     53             ss=ss+_T("打印预览");
     54     }
     55 
     56     if(!Create(NULL,ss,WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,CRect(200,200,500,500)))
     57         TRACE0("Failed to create view window\n");
     58 }
     59 
     60 
     61 CMainFrame::~CMainFrame()
     62 {
     63     pOldWnd->ShowWindow(SW_SHOW);
     64 
     65     pCallWnd=NULL;
     66     pOldWnd    =NULL;
     67     bDirectPrint=FALSE;
     68 }
     69 
     70 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
     71 {
     72     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
     73         return -1;    
     74 
     75     CCreateContext context;
     76     context.m_pNewViewClass = RUNTIME_CLASS(CWrapperView);
     77     context.m_pCurrentFrame = this;
     78     context.m_pCurrentDoc = NULL;
     79     context.m_pLastView = NULL;
     80     m_pView = STATIC_DOWNCAST(CWrapperView, CreateView(&context)); //CreateView(&context);
     81     if(NULL!=m_pView){
     82         if(!bDirectPrint)
     83             m_pView->ShowWindow(SW_SHOW);
     84         SetActiveView(m_pView);
     85     }    
     86     SetIcon(pOldWnd->GetIcon(FALSE),FALSE);
     87     SetIcon(pOldWnd->GetIcon(TRUE),TRUE);
     88     ShowWindow(SW_MAXIMIZE);    
     89 
     90     CWinApp *pApp=AfxGetApp();        
     91     pApp->m_pMainWnd=this;
     92     if(bDirectPrint)
     93     {
     94         m_pView->SendMessage(WM_COMMAND, ID_FILE_PRINT);        
     95     }
     96     else
     97     {
     98         m_pView->OnFilePrintPreview(this);    
     99         pOldWnd->ShowWindow(SW_HIDE);
    100     }
    101 
    102     return 0;
    103 }
    104 
    105 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    106 {
    107     if( !CFrameWnd::PreCreateWindow(cs) )
    108         return FALSE;
    109     // TODO: Modify the Window class or styles here by modifying
    110     //  the CREATESTRUCT cs
    111 
    112     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
    113     cs.lpszClass = AfxRegisterWndClass(0);
    114     return TRUE;
    115 }
    116 
    117 /////////////////////////////////////////////////////////////////////////////
    118 // CMainFrame diagnostics
    119 
    120 #ifdef _DEBUG
    121 void CMainFrame::AssertValid() const
    122 {
    123     CFrameWnd::AssertValid();
    124 }
    125 
    126 void CMainFrame::Dump(CDumpContext& dc) const
    127 {
    128     CFrameWnd::Dump(dc);
    129 }
    130 
    131 #endif //_DEBUG
    132 
    133 /////////////////////////////////////////////////////////////////////////////
    134 // CMainFrame message handlers
    135 
    136 BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
    137 {
    138     // let the view have first crack at the command
    139     if (m_pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
    140         return TRUE;
    141 
    142     // otherwise, do default handling
    143     return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
    144 }
    145 
    146 
    147 
    148 //////////////////////////////////////////////////////////////////////////
    149 // 源代码中使用的框架销毁函数有bug,这里暂时不使用正常销毁-AWEN
    150 void CMainFrame::OnClose()
    151 {
    152     // TODO: 在此添加消息处理程序代码和/或调用默认值
    153 
    154     //CFrameWnd::OnClose();
    155     CFrameWnd::DestroyWindow();
    156 }

    WrapperView.cpp

      1 // WrapperView.cpp : implementation file
      2 //
      3 
      4 #include "stdafx.h"
      5 #include "WrapperView.h"
      6 #include "MyPreviewView.h"
      7 #include "MainFrm.h"
      8 
      9 #ifdef _DEBUG
     10 #define new DEBUG_NEW
     11 #undef THIS_FILE
     12 static char THIS_FILE[] = __FILE__;
     13 #endif
     14 
     15 BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd);
     16 
     17 /////////////////////////////////////////////////////////////////////////////
     18 // CWrapperView
     19 
     20 IMPLEMENT_DYNCREATE(CWrapperView, CScrollView)
     21 
     22 CWrapperView::CWrapperView()
     23 {
     24     m_pFrameWnd = NULL;
     25     m_nMapMode = MM_TEXT; //default
     26 }
     27 
     28 CWrapperView::~CWrapperView()
     29 {
     30 }
     31 
     32 
     33 BEGIN_MESSAGE_MAP(CWrapperView, CScrollView)
     34     //{{AFX_MSG_MAP(CWrapperView)
     35         // NOTE - the ClassWizard will add and remove mapping macros here.    
     36     ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
     37     //}}AFX_MSG_MAP
     38 END_MESSAGE_MAP()
     39 
     40 void CWrapperView::OnDraw(CDC *pDC)
     41 {
     42     
     43 }
     44 
     45 void CWrapperView::OnPrint(CDC* pDC,CPrintInfo* pInfo)
     46 {
     47     if(pDC)
     48     {
     49         CMainFrame* pf=(CMainFrame*)::AfxGetMainWnd();        
     50         (*pf->Draw)(pDC,pInfo,(void*)pf->pCallWnd);        
     51     }
     52 }
     53 
     54 
     55 /////////////////////////////////////////////////////////////////////////////
     56 // CWrapperView diagnostics
     57 
     58 #ifdef _DEBUG
     59 void CWrapperView::AssertValid() const
     60 {
     61     CView::AssertValid();
     62 }
     63 
     64 void CWrapperView::Dump(CDumpContext& dc) const
     65 {
     66     CView::Dump(dc);
     67 }
     68 #endif //_DEBUG
     69 
     70 /////////////////////////////////////////////////////////////////////////////
     71 // CWrapperView message handlers
     72 
     73 BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd)
     74 {
     75     ASSERT_VALID(pFrameWnd);
     76 
     77     CMyPreviewView* pView = (CMyPreviewView*) pFrameWnd->GetDlgItem(AFX_IDW_PANE_FIRST);
     78     ASSERT_KINDOF(CPreviewView, pView);
     79 
     80     pView->OnPreviewClose();
     81     return FALSE;
     82 }
     83 
     84 BOOL CWrapperView::OnPreparePrinting(CPrintInfo* pInfo) 
     85 {
     86     // default preparation
     87     return DoPreparePrinting(pInfo);
     88 }
     89 
     90 void CWrapperView::OnFilePrint()
     91 {
     92     CMainFrame* pf=(CMainFrame*)::AfxGetMainWnd();
     93     if(pf->bDirectPrint)
     94         pf->ShowWindow(SW_HIDE);
     95     CView::OnFilePrint();    
     96     if(pf->bDirectPrint)
     97     {
     98         CWinApp *pApp=AfxGetApp();        
     99         pApp->m_pMainWnd=pf->pOldWnd;
    100         pf->DestroyWindow();    
    101     }
    102 }
    103 
    104 
    105 void CWrapperView::OnFilePrintPreview(CFrameWnd *pFrame)
    106 {    
    107     m_pFrameWnd = pFrame;
    108 
    109     CPrintPreviewState* pState = new CPrintPreviewState;
    110     pState->lpfnCloseProc = _AfxMyPreviewCloseProc;
    111 
    112     if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,RUNTIME_CLASS(CMyPreviewView), pState))
    113     {        
    114 
    115         TRACE0("Error: DoPrintPreview failed.\n");
    116         AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
    117         delete pState;
    118     }
    119 }
    120 
    121 BOOL CWrapperView::DoPrintPreview(UINT nIDResource, CView* pPrintView,CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState)
    122 {
    123     ASSERT_VALID_IDR(nIDResource);
    124     ASSERT_VALID(pPrintView);
    125     ASSERT(pPreviewViewClass != NULL);
    126     ASSERT(pPreviewViewClass->IsDerivedFrom(RUNTIME_CLASS(CPreviewView)));
    127     ASSERT(pState != NULL);
    128 
    129     ASSERT(m_pFrameWnd != NULL);
    130     CFrameWnd* pParent = m_pFrameWnd; //STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd()); //
    131     ASSERT_VALID(pParent);
    132 
    133     CCreateContext context;
    134     context.m_pCurrentFrame = pParent;
    135     context.m_pCurrentDoc = GetDocument();
    136     context.m_pLastView = this;
    137 
    138     // Create the preview view object
    139     CMyPreviewView* pView = (CMyPreviewView*)pPreviewViewClass->CreateObject();
    140     if (pView == NULL)
    141     {
    142         TRACE0("Error: Failed to create preview view.\n");
    143         return FALSE;
    144     }
    145     ASSERT_KINDOF(CPreviewView, pView);
    146     pView->m_pPreviewState = pState;        // save pointer
    147 
    148     pParent->OnSetPreviewMode(TRUE, pState);    // Take over Frame Window
    149 
    150      //Create the toolbar from the dialog resource
    151     pView->m_pToolBar = new CDialogBar;
    152     if (!pView->m_pToolBar->Create(pParent, MAKEINTRESOURCE(nIDResource),
    153         CBRS_TOP, AFX_IDW_PREVIEW_BAR))
    154     {
    155         TRACE0("Error: Preview could not create toolbar dialog.\n");
    156         pParent->OnSetPreviewMode(FALSE, pState);   // restore Frame Window
    157         delete pView->m_pToolBar;       // not autodestruct yet
    158         pView->m_pToolBar = NULL;
    159         pView->m_pPreviewState = NULL;  // do not delete state structure
    160         delete pView;
    161         return FALSE;
    162     }
    163     pView->m_pToolBar->m_bAutoDelete = TRUE;    // automatic cleanup
    164 
    165     if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    166         CRect(0,0,0,0), pParent, AFX_IDW_PANE_FIRST, &context))
    167     {
    168         TRACE0("Error: couldn't create preview view for frame.\n");
    169         pParent->OnSetPreviewMode(FALSE, pState);   // restore Frame Window
    170         pView->m_pPreviewState = NULL;  // do not delete state structure
    171         delete pView;
    172         return FALSE;
    173     }
    174 
    175     pState->pViewActiveOld = pParent->GetActiveView();
    176     CWrapperView* pActiveView = (CWrapperView*)pParent->GetActiveFrame()->GetActiveView();
    177     if (pActiveView != NULL)
    178         pActiveView->OnActivateView(FALSE, pActiveView, pActiveView);
    179 
    180     if (!pView->SetPrintView(pPrintView))
    181     {
    182         pView->OnPreviewClose();
    183         return TRUE;            // signal that OnEndPrintPreview was called
    184     }
    185 
    186     pParent->SetActiveView(pView);  // set active view - even for MDI
    187 
    188     // update toolbar and redraw everything
    189     pView->m_pToolBar->SendMessage(WM_IDLEUPDATECMDUI, (WPARAM)TRUE);
    190     pParent->RecalcLayout();            // position and size everything
    191     pParent->UpdateWindow();
    192 
    193     return TRUE;
    194 }
    195 
    196 // OnEndPrintPreview is here for swap tuning reasons
    197 //  (see viewprev.cpp for complete preview mode implementation)
    198 void CWrapperView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo,POINT, CMyPreviewView* pView)
    199 {
    200     ASSERT_VALID(pDC);
    201     ASSERT_VALID(pView);
    202     ASSERT(m_pFrameWnd != NULL);
    203 
    204     if (pView->m_pPrintView != NULL)
    205         OnEndPrinting(pDC, pInfo);
    206 
    207     CFrameWnd* pParent = m_pFrameWnd;
    208     ASSERT_VALID(pParent);
    209 
    210     // restore the old main window
    211     pParent->OnSetPreviewMode(FALSE, pView->m_pPreviewState);
    212 
    213     // Force active view back to old one
    214     pParent->SetActiveView(pView->m_pPreviewState->pViewActiveOld);
    215     if (pParent != GetParentFrame())
    216         OnActivateView(TRUE, this, this);   // re-activate view in real frame
    217     pView->DestroyWindow();     // destroy preview view
    218             // C++ object will be deleted in PostNcDestroy
    219 
    220     // restore main frame layout and idle message
    221     pParent->RecalcLayout();
    222     pParent->SendMessage(WM_SETMESSAGESTRING, (WPARAM)AFX_IDS_IDLEMESSAGE, 0L);
    223     pParent->UpdateWindow();
    224 }
    225 
    226 void CWrapperView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
    227 {
    228     CView::OnEndPrinting(pDC, pInfo);
    229 }
    230 
    231 /////////////////////////////////////////////////////////////////////
    232 
    233 IMPLEMENT_DYNCREATE(CMyPreviewView, CPreviewView)
    234 
    235 CMyPreviewView::CMyPreviewView()
    236 {
    237 }
    238 
    239 CMyPreviewView::~CMyPreviewView()
    240 {
    241 }
    242 
    243 BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
    244     //{{AFX_MSG_MAP(CMyPreviewView)
    245     ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)
    246     ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
    247         // NOTE - the ClassWizard will add and remove mapping macros here.
    248     //}}AFX_MSG_MAP
    249 END_MESSAGE_MAP()
    250 
    251 /////////////////////////////////////////////////////////////////////////////
    252 // CMyPreviewView drawing
    253 
    254 void CMyPreviewView::OnDraw(CDC* pDC)
    255 {    
    256     CPreviewView::OnDraw(pDC);    
    257 }
    258 
    259 /////////////////////////////////////////////////////////////////////////////
    260 // CMyPreviewView diagnostics
    261 
    262 #ifdef _DEBUG
    263 void CMyPreviewView::AssertValid() const
    264 {
    265     
    266 }
    267 
    268 void CMyPreviewView::Dump(CDumpContext& dc) const
    269 {
    270 }
    271 #endif //_DEBUG
    272 
    273 /////////////////////////////////////////////////////////////////////////////
    274 // CMyPreviewView message handlers
    275 
    276 void CMyPreviewView::OnActivateView(BOOL bActivate, CView* pView1, CView *pView2)
    277 {
    278     CPreviewView::OnActivateView(bActivate, pView1, pView2);
    279 }
    280 
    281 void CMyPreviewView::OnPreviewClose()
    282 {
    283     CMainFrame* pf=(CMainFrame*)::AfxGetMainWnd();
    284     CWinApp *pApp=AfxGetApp();        
    285     pApp->m_pMainWnd=pf->pOldWnd;
    286     pf->DestroyWindow();
    287 }
    288 
    289 void CMyPreviewView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView) 
    290 {    
    291     
    292     CPreviewView::OnEndPrintPreview(pDC, pInfo, point, pView);
    293 }
    294 
    295 void CMyPreviewView::OnPreviewPrint()
    296 {
    297     m_pPrintView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
    298     OnPreviewClose();               // force close of Preview
    299 }

    3.在stdafx.h中添加函数指针定义

    typedef void(*DRAWFUN)(CDC* pDC,CPrintInfo* pInfo,void* pVoid);

    4.在对话框类中添加上述类的头文件。

    5.在对话框中添加按钮消息响应函数

    1 void C我的打印预览Dlg::OnBnClickedOk()
    2 {
    3     m_PreFrame = new CMainFrame(DRWAPRINTER,this,this,FALSE);
    4     if (!m_PreFrame) return;
    5     m_PreFrame->ShowWindow(SW_SHOW);
    6     m_PreFrame->UpdateWindow();
    7 }

    6.添加打印函数DRWAPRINTER定义,同时将其声明。

    1 void DRWAPRINTER(CDC* pDC,CPrintInfo* pInfo,void* pVoid)
    2 {    
    3     //((CImageboxUI*)pVoid)->OnPrint(pDC,pInfo);    
    4 }

     7.将要绘制的内容写进DRWAPRINTER函数即可。

    1 void C我的打印预览Dlg::OnPrint(CDC * pDC, CPrintInfo* pInfo)
    2 {
    3     CBrush ColorBrush(RGB(100, 100, 0));//设置颜色
    4     pDC->SelectObject(&ColorBrush);//将色彩赋与刷子
    5     pDC->Ellipse(0,0,1000,1000);
    6 }

    8.至此即可完成MFC对话框中打印预览的实现。

    9.自己写了一个打印字符串的函数:

     1 //打印字符串
     2 void C我的打印预览Dlg::PrintString(CString str, CString mfont, int x, int y, int sz, CDC & dc)
     3 {
     4     CFont font;
     5 
     6     VERIFY(font.CreatePointFont(sz, 
     7 
     8         mfont, 
     9 
    10         &dc));//为DC创建字体
    11 
    12     CFont* def_font = dc.SelectObject(&font);//保存现在的字体
    13 
    14     dc.SetTextAlign(TA_TOP|TA_LEFT);
    15 
    16     CString s = str; //要打印的字符串(注意有长度的限制)
    17 
    18 
    19     s += "\n"; //必须增加一个换行符号(因为后面while循环的要求)
    20 
    21     CString ss;
    22 
    23     int index;
    24 
    25     CSize size;
    26 
    27     size = dc.GetTextExtent("00", 2);//计算使用当前字体输出时文本所占大小、宽 度
    28 
    29     while((index = s.Find("\n")) != -1)
    30 
    31     {
    32 
    33         ss = s.Left(index);
    34 
    35         if(ss.Find("\r") != -1)//输入的字符串有回车符
    36 
    37             ss = s.Left(index-1);
    38 
    39         s = s.Mid(index+1);//取剩余的字符串 // AfxMessageBox("A"+ss+"A");
    40 
    41         dc.TextOut(x, y, ss);//打印到缓冲区
    42 
    43         y -= size.cy;
    44 
    45     }
    46 
    47 
    48     dc.SelectObject(def_font); //恢复原来的字体
    49 
    50     font.DeleteObject(); 
    51 }

    这样就可以随心所欲地设置打印内容了。

    感谢AWEN提供的巨大帮助!

    另外,设置打印间距的函数为:

    SetTextCharacterExtra(pDC->m_hDC, 5);

  • 相关阅读:
    如何动态调用WebServices
    Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
    SQL创建索引(转)
    TSQL用法四:OpenDataSource, OpenRowSet
    AppDomain动态加载程序集
    hdu 2544 最短路
    hdu 1151 Air Raid
    hdu3790 最短路径问题
    hdu 1548 A strange lift
    对于 前K短路径问题 和 A*算法 的一些小小总结
  • 原文地址:https://www.cnblogs.com/johnpher/p/2728936.html
Copyright © 2020-2023  润新知