从代码仓库下载利用MFC类打印及预览CPrintFrame,CPrintView类http://www.vckbase.com/document/viewdoc/?id=1618
代码如下,在其他窗口里使用该类,但有两个不完善的地方1.直接打印时总出现选择打印机类型,2.在预览窗口里按打印
按钮而不能直接打印,哪位高手能够完善该类,
多谢了..
 /************************CPrintFrame 类***************************************************/
#define WM_MY_PRINT (WM_USER+1003)
#define WM_BEGIN_PRINTING (WM_USER+1004)
#define WM_END_PRINTING (WM_USER+1005)
#define PRINTMARGIN 2#include "PrintView.h"
class CPrintFrame : public CFrameWnd
{
DECLARE_DYNCREATE(CPrintFrame)
public:
CPrintFrame();           // protected constructor used by dynamic creation
public:
CPrintView *m_pView;
CDialog *m_pCallerDlg;
CWnd *m_pWnd;
public:protected:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
public:
virtual ~CPrintFrame();
afx_msg void OnDestroy();
DECLARE_MESSAGE_MAP()
};#endif#include "stdafx.h"
#include "PrintFrame.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifIMPLEMENT_DYNCREATE(CPrintFrame, CFrameWnd)CPrintFrame::CPrintFrame()
{
m_pCallerDlg = NULL;
m_pWnd = AfxGetApp()->m_pMainWnd;
AfxGetApp()->m_pMainWnd = this;
}CPrintFrame::~CPrintFrame()
{
}
BEGIN_MESSAGE_MAP(CPrintFrame, CFrameWnd)
//{{AFX_MSG_MAP(CPrintFrame)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()BOOL CPrintFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
// TODO: Add your specialized code here and/or call the base class
m_pView = new CPrintView();
m_pView->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,AFX_IDW_PANE_FIRST,pContext);
return TRUE;
}void CPrintFrame::OnDestroy() 
{
if(m_pView!=NULL)
{
m_pView->DestroyWindow();
}
CFrameWnd::OnDestroy();
AfxGetApp()->m_pMainWnd = m_pWnd;
}
/*************************CPrintView 类**************************************************/
class CPrintView : public CView
{
public:
CPrintView();           
DECLARE_DYNCREATE(CPrintView)
public:
BOOL m_bPrint;
public:
void OnMyPrint();
void OnMyPrintPreview();
virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView); protected:
virtual void OnDraw(CDC* pDC);      
public:
virtual ~CPrintView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
public:
DECLARE_MESSAGE_MAP()
};
#include "stdafx.h"#include "PrintView.h"
#include "PrintFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifIMPLEMENT_DYNCREATE(CPrintView, CView)CPrintView::CPrintView()
{
m_bPrint = TRUE;
}CPrintView::~CPrintView()
{
}
BEGIN_MESSAGE_MAP(CPrintView, CView)
//{{AFX_MSG_MAP(CPrintView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CPrintView drawingvoid CPrintView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}/////////////////////////////////////////////////////////////////////////////
// CPrintView diagnostics#ifdef _DEBUG
void CPrintView::AssertValid() const
{
CView::AssertValid();
}void CPrintView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CPrintView message handlers
void CPrintView::OnMyPrint()
{
GetParent()->ShowWindow(SW_SHOWMINIMIZED);
m_bPrint = TRUE;
CView::OnFilePrint();}
void CPrintView::OnMyPrintPreview()
{
GetParent()->ShowWindow(SW_SHOWMAXIMIZED);
m_bPrint=FALSE;
CView::OnFilePrintPreview();
}
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo) 
{
if(DoPreparePrinting(pInfo))
return TRUE;
else
{
GetParent()->DestroyWindow();
return FALSE;
}
}
void CPrintView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) 
{
CView::OnBeginPrinting(pDC, pInfo);
CPrintFrame *pFrame =(CPrintFrame *)GetParent();
pFrame->m_pCallerDlg->SendMessage(WM_BEGIN_PRINTING,(WPARAM) pDC, (LPARAM) pInfo);
}
void CPrintView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) 
{
CView::OnEndPrinting(pDC, pInfo);
CPrintFrame *pFrame =(CPrintFrame *)GetParent();
pFrame->m_pCallerDlg->SendMessage(WM_END_PRINTING,(WPARAM) pDC, (LPARAM) pInfo);
if(m_bPrint) //直接打印,不是预览
GetParent()->DestroyWindow();
}void CPrintView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
CPrintFrame *pFrame =(CPrintFrame *)GetParent();
pFrame->m_pCallerDlg->SendMessage(WM_MY_PRINT,(WPARAM) pDC, (LPARAM) pInfo) ;
}
void CPrintView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView) 
{
// TODO: Add your specialized code here and/or call the base class
CView::OnEndPrintPreview(pDC, pInfo, point, pView);
GetParent()->DestroyWindow();
}