最近用到了VC开发中基于对话框程序的打印及其打印预览的功能实现,在此做一下简要总结。
在微软的MFC单/多文档程序中,打印机编程由类库自动封装并提供,程序员只需根据实际需要添加少量打印机打印代码就可以完成需求。但是基于MFC对话框的程序,我们就不能够直接使用文档的那一套打印框架代码了。为了提高对框框打印机编程的效率,大家一致的想到了将微软打印框架从文档中移植到基于对话框的程序当中,通过一个虚拟的Frame和View来实现,以下代码来源于网络。
<移植框架头文件>
Frame:
// MainFrm.h : interface of the CMainFrame class // ///////////////////////////////////////////////////////////////////////////// #if !defined(AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_) #define AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "wrapperview.h" class CMainFrame : public CFrameWnd { public: CMainFrame(DRAWFUN pDraw,CWnd*pOldW,CWnd* pCallW,BOOL bDirect,LPCTSTR stTitle=NULL); CWnd* pOldWnd; CWnd* pCallWnd; DRAWFUN Draw; BOOL bDirectPrint; protected: DECLARE_DYNAMIC(CMainFrame) public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMainFrame) virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo); //}}AFX_VIRTUAL // Implementation public: virtual ~CMainFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; CWrapperView *m_pView; // Generated message map functions protected: //{{AFX_MSG(CMainFrame) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: afx_msg void OnClose(); }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_MAINFRM_H__5C3F89A6_932E_11D3_8420_FC56F1C91E86__INCLUDED_)
View:
#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 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: virtual ~CMyPreviewView(); #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_)
#if !defined(AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_) #define AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // WrapperView.h : header file // #include "MyPreviewView.h" ///////////////////////////////////////////////////////////////////////////// // CWrapperView view class CWrapperView : public CScrollView { protected: DECLARE_DYNCREATE(CWrapperView) CFrameWnd *m_pFrameWnd; public: CWrapperView(); virtual ~CWrapperView(); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); void OnFilePrintPreview(CFrameWnd *pFrame); BOOL DoPrintPreview(UINT nIDResource, CView* pPrintView, CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState); void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT, CMyPreviewView* pView); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CWrapperView) protected: virtual void OnPrint(CDC* pDC,CPrintInfo* pInfo); // overridden to draw this view virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); //}}AFX_VIRTUAL // Implementation protected: #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif // Generated message map functions protected: //{{AFX_MSG(CWrapperView) // NOTE - the ClassWizard will add and remove member functions here. afx_msg void OnFilePrint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_WRAPPERVIEW_H__AC579D20_8E3D_11D3_8410_E1CD9A2C01DC__INCLUDED_)
<使用方法>
1、在对话框程序中添加文件:MainFrm.h、MainFrm.cpp、WrapperView.h、WrapperView.cpp、MyPreviewView.h;2、实现打印机绘制函数体void DRWAPRINTER(CDC* pDC,CPrintInfo* pInfo,void* pVoid);3、打印预览:
void CImageboxUI::OnBnClickedBtnPirntview() { // TODO: 在此添加控件通知处理程序代码 m_PreFrame = new CMainFrame(DRWAPRINTER,this,this,FALSE); m_PreFrame->ShowWindow(SW_SHOW); m_PreFrame->UpdateWindow(); }打印:void CImageboxUI::OnBnClickedBtnPrint() { // TODO: 在此添加控件通知处理程序代码 m_PreFrame = new CMainFrame(DRWAPRINTER,this,this,TRUE); }
<使用效果>