首先在这里感谢互联网的大哥们!同时我讨厌动不动就是要vip才能查看!所以我写的不需要vip就能看。只求点个赞。
直接上代码,新建6个文件CPrintFrame.cpp;CPrintFrame.h;CPrintPreviewView.cpp;CPrintPreviewView.h;CPrintView.cpp;CPrintView.h;
下面是代码
#pragma once
// CPrintFrame 框架
#include "CPrintView.h"
#include "CertEditPage.h"
#include "DemarManageDlg.h"
class CPrintFrame : public CFrameWnd
{
DECLARE_DYNCREATE(CPrintFrame)
protected:
CPrintFrame(); // 动态创建所使用的受保护的构造函数
virtual ~CPrintFrame();
public:
CPrintFrame(CCertEditPage* pCCertEditPage, CDemarManageDlg* pDemarManageDlg, BOOL bPreview);
CCertEditPage* m_pCCertEditPage;
CPrintView* m_pCPrintView;
CDemarManageDlg* m_pDemarManageDlg;
BOOL m_bPreview;
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnClose();
void SetOrientation(short orientation);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
};
// CPrintFrame.cpp: 实现文件
//
#include "stdafx.h"
#include "CPrintFrame.h"
// CPrintFrame
IMPLEMENT_DYNCREATE(CPrintFrame, CFrameWnd)
CPrintFrame::CPrintFrame()
:m_pCCertEditPage(NULL),m_pCPrintView(NULL), m_bPreview(TRUE)
{
m_pCCertEditPage = NULL;
}
CPrintFrame::CPrintFrame(CCertEditPage* pCCertEditPage, CDemarManageDlg* pDemarManageDlg,BOOL bPreview)
: m_pCCertEditPage(NULL), m_pCPrintView(NULL)
{
m_bPreview = bPreview;
m_pCCertEditPage = pCCertEditPage;
m_pDemarManageDlg = pDemarManageDlg;
CString title;
if (m_bPreview)
title = L"打印预览";
else
title = L"打印";
if (Create(NULL, title, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, CRect(200, 200, 500, 500)))
{
//创建失败
return;
}
SetOrientation(DMORIENT_LANDSCAPE);
}
CPrintFrame::~CPrintFrame()
{
//CWinApp* pApp = AfxGetApp();
//pApp->m_pMainWnd = m_pDemarManageDlg;
if (m_pCCertEditPage)
m_pCCertEditPage->ShowWindow(SW_SHOW);
}
BEGIN_MESSAGE_MAP(CPrintFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CPrintFrame 消息处理程序
int CPrintFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_pCCertEditPage)
return -1;
// TODO: 在此添加您专用的创建代码
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CPrintView);
context.m_pCurrentFrame = this;
context.m_pCurrentDoc = NULL;
context.m_pLastView = NULL;
m_pCPrintView = STATIC_DOWNCAST(CPrintView, CreateView(&context));
if (!m_pCPrintView)
return -1;
if (m_bPreview)
{
m_pCPrintView->ShowWindow(SW_SHOW);
}
SetActiveView(m_pCPrintView);
SetIcon(m_pCCertEditPage->GetIcon(FALSE), FALSE);
SetIcon(m_pCCertEditPage->GetIcon(TRUE), TRUE);
//ShowWindow(SW_MAXIMIZE);
CRect rect;
m_pCCertEditPage->GetWindowRect(rect);
MoveWindow(rect);
ShowWindow(SW_SHOW);
m_pCPrintView->m_pCPrintFrame = this;
CWinApp* pApp = AfxGetApp();
pApp->m_pMainWnd = this;
if (m_bPreview)
{
m_pCPrintView->OnFilePrintPreview();
m_pCCertEditPage->ShowWindow(SW_HIDE);
}
else
m_pCPrintView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
return 0;
}
void CPrintFrame::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CWinApp* pApp = AfxGetApp();
pApp->m_pMainWnd = m_pDemarManageDlg;
this->DestroyWindow();
//CFrameWnd::OnClose();
}
void CPrintFrame::SetOrientation(short orientation)
{
PRINTDLG pd;
pd.lStructSize = (DWORD)sizeof(PRINTDLG);
BOOL bRet = AfxGetApp()->GetPrinterDeviceDefaults(&pd);
if (bRet)
{
LPDEVMODE lpDevMode = (LPDEVMODE) ::GlobalLock(pd.hDevMode);
if (lpDevMode != NULL)
lpDevMode->dmOrientation = orientation;
::GlobalUnlock(pd.hDevMode);
}
}
BOOL CPrintFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此添加专用代码和/或调用基类
if (!CFrameWnd::PreCreateWindow(cs))
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
//return CFrameWnd::PreCreateWindow(cs);
}
BOOL CPrintFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// TODO: 在此添加专用代码和/或调用基类
if (m_pCPrintView && m_pCPrintView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
#pragma once
#include
// CPrintView 视图
class CPrintView : public CScrollView
{
DECLARE_DYNCREATE(CPrintView)
protected:
CPrintView(); // 动态创建所使用的受保护的构造函数
virtual ~CPrintView();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
virtual void OnInitialUpdate(); // 构造后的第一次
DECLARE_MESSAGE_MAP()
public:
void SetPaper(short dmOrientation, short dmPaperSize);
void OnFilePrintPreview();
afx_msg void OnFilePrint();
void DrawCertification(CDC* pDC, CPrintInfo* pInfo);
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
CFrameWnd* m_pCPrintFrame;
virtual void OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
void DrawCardData(CDC* pDC, CPrintInfo* pInfo);
afx_msg void OnClose();
BOOL DoPrintPreview(UINT nIDResource, CView* pPrintView, CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState);
};
// CPrintView.cpp: 实现文件
//
#include "stdafx.h"
#include "DemarManage.h"
#include "CPrintView.h"
#include "DemarManageDlg.h"
#include "CPrintFrame.h"
#include "CPrintPreviewView.h"
// CPrintView
IMPLEMENT_DYNCREATE(CPrintView, CScrollView)
CPrintView::CPrintView():m_pCPrintFrame(NULL)
{
m_nMapMode = MM_TEXT;
}
CPrintView::~CPrintView()
{
}
BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd)
{
ASSERT_VALID(pFrameWnd);
CPrintPreviewView* pView = (CPrintPreviewView*)pFrameWnd->GetDlgItem(AFX_IDW_PANE_FIRST);
ASSERT_KINDOF(CPrintPreviewView, pView);
pView->OnPreviewClose();
return FALSE;
}
BEGIN_MESSAGE_MAP(CPrintView, CScrollView)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CPrintView::OnFilePrintPreview)
ON_COMMAND(ID_FILE_PRINT, &CPrintView::OnFilePrint)
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CPrintView 诊断
#ifdef _DEBUG
void CPrintView::AssertValid() const
{
CScrollView::AssertValid();
}
#ifndef _WIN32_WCE
void CPrintView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
#endif
#endif //_DEBUG
// CPrintView 绘图
void CPrintView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: 计算此视图的合计大小
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
void CPrintView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: 在此添加绘制代码
}
/************************************************************************/
/* 设置纸张 */
/************************************************************************/
void CPrintView::SetPaper(short dmOrientation, short dmPaperSize)
{
PRINTDLG pd;
pd.lStructSize = (DWORD)sizeof(PRINTDLG);
BOOL bRet = AfxGetApp()->GetPrinterDeviceDefaults(&pd);
if (bRet)
{
LPDEVMODE lpDevMode = (LPDEVMODE) ::GlobalLock(pd.hDevMode);
if (lpDevMode != NULL)
{
lpDevMode->dmOrientation = dmOrientation;
lpDevMode->dmPaperSize = dmPaperSize;
}
::GlobalUnlock(pd.hDevMode);
}
}
void CPrintView::OnFilePrintPreview()
{
CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
if (!pCPrintFrame || !pCPrintFrame->m_pCCertEditPage)
return;
switch (pCPrintFrame->m_pCCertEditPage->m_nPrintMode)
{
case 1:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
case 2:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
case 4:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
}
//CView::OnFilePrintPreview();
CPrintPreviewState* pState = new CPrintPreviewState;
pState->lpfnCloseProc = _AfxMyPreviewCloseProc;
if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this, RUNTIME_CLASS(CPrintPreviewView), pState))
{
TRACE0("Error: DoPrintPreview failed.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
delete pState;
}
}
BOOL CPrintView::DoPrintPreview(UINT nIDResource, CView* pPrintView, CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState)
{
ASSERT_VALID_IDR(nIDResource);
if (!m_pCPrintFrame || !pPrintView || !pPreviewViewClass)
return FALSE;
ASSERT(pPreviewViewClass->IsDerivedFrom(RUNTIME_CLASS(CPreviewView)));
if(!pState)
return FALSE;
CCreateContext context;
context.m_pCurrentFrame = m_pCPrintFrame;
context.m_pCurrentDoc = GetDocument();
context.m_pLastView = this;
// Create the preview view object
CPrintPreviewView* pView = (CPrintPreviewView*)pPreviewViewClass->CreateObject();
if (!pView)
{
TRACE0("Error: Failed to create preview view.\n");
return FALSE;
}
ASSERT_KINDOF(CPreviewView, pView);
pView->m_pPreviewState = pState; // save pointer
m_pCPrintFrame->OnSetPreviewMode(TRUE, pState); // Take over Frame Window
//Create the toolbar from the dialog resource
pView->m_pToolBar = new CDialogBar;
if (!pView->m_pToolBar->Create(m_pCPrintFrame, MAKEINTRESOURCE(nIDResource),
CBRS_TOP, AFX_IDW_PREVIEW_BAR))
{
TRACE0("Error: Preview could not create toolbar dialog.\n");
m_pCPrintFrame->OnSetPreviewMode(FALSE, pState); // restore Frame Window
delete pView->m_pToolBar; // not autodestruct yet
pView->m_pToolBar = NULL;
pView->m_pPreviewState = NULL; // do not delete state structure
delete pView;
return FALSE;
}
pView->m_pToolBar->m_bAutoDelete = TRUE; // automatic cleanup
if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), m_pCPrintFrame, AFX_IDW_PANE_FIRST, &context))
{
TRACE0("Error: couldn't create preview view for frame.\n");
m_pCPrintFrame->OnSetPreviewMode(FALSE, pState); // restore Frame Window
pView->m_pPreviewState = NULL; // do not delete state structure
delete pView;
return FALSE;
}
pState->pViewActiveOld = m_pCPrintFrame->GetActiveView();
CPrintView* pActiveView = (CPrintView*)m_pCPrintFrame->GetActiveFrame()->GetActiveView();
if (pActiveView != NULL)
pActiveView->OnActivateView(FALSE, pActiveView, pActiveView);
if (!pView->SetPrintView(pPrintView))
{
pView->OnPreviewClose();
return TRUE; // signal that OnEndPrintPreview was called
}
m_pCPrintFrame->SetActiveView(pView); // set active view - even for MDI
// update toolbar and redraw everything
pView->m_pToolBar->SendMessage(WM_IDLEUPDATECMDUI, (WPARAM)TRUE);
m_pCPrintFrame->RecalcLayout(); // position and size everything
m_pCPrintFrame->UpdateWindow();
return TRUE;
}
void CPrintView::OnFilePrint()
{
// TODO: 在此添加命令处理程序代码
CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
if (!pCPrintFrame || !pCPrintFrame->m_pCCertEditPage)
return;
if (!pCPrintFrame->m_bPreview)
pCPrintFrame->ShowWindow(SW_HIDE);
switch (pCPrintFrame->m_pCCertEditPage->m_nPrintMode)
{
case 1:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
case 2:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
case 4:
SetPaper(DMORIENT_PORTRAIT, DMPAPER_A5);
break;
}
CView::OnFilePrint();
if (!pCPrintFrame->m_bPreview)
{
CWinApp* pApp = AfxGetApp();
pApp->m_pMainWnd = pCPrintFrame->m_pDemarManageDlg;
pCPrintFrame->DestroyWindow();
}
}
void CPrintView::DrawCertification(CDC* pDC, CPrintInfo* pInfo)
{
if (!pDC)
return;
CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
if(!pCPrintFrame || !pCPrintFrame->m_pDemarManageDlg)
return;
POINT point = { 0, 0 };
SIZE size = { pDC->GetDeviceCaps(HORZRES),
pDC->GetDeviceCaps(VERTRES) };
CRect clientRect(point, size);
double nLeftSpace(0.8), nTopSpace(0.8), nRightSpace(0.8), nBottomSpace(0.8);
int iLeftSpace(0), iTopSpace(0), iRightSpace(0), iBottomSpace(0);
iLeftSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSX),
nLeftSpace);
iTopSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSY),
nTopSpace);
iRightSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSX),
nRightSpace);
iBottomSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSY),
nBottomSpace);
//这里添加打印代码,打印自己想要的
}
BOOL CPrintView::OnPreparePrinting(CPrintInfo* pInfo)
{
CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
if (!pCPrintFrame || !pCPrintFrame->m_pCCertEditPage)
return FALSE;
// TODO: 调用 DoPreparePrinting 以调用“打印”对话框
switch (pCPrintFrame->m_pCCertEditPage->m_nPrintMode)
{
case 1:
pInfo->SetMaxPage(1);
pInfo->m_bDirect = FALSE;
break;
case 2:
pInfo->SetMaxPage(1);
pInfo->m_bDirect = FALSE;
break;
case 4:
pInfo->SetMaxPage(2);
pInfo->m_bDirect = FALSE;
break;
default:
break;
}
return DoPreparePrinting(pInfo);
}
void CPrintView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: 在此添加专用代码和/或调用基类
CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
if (!pCPrintFrame || !pCPrintFrame->m_pCCertEditPage)
return;
switch (pCPrintFrame->m_pCCertEditPage->m_nPrintMode)
{
case 1:
DrawCertification(pDC, pInfo);
break;
case 2:
DrawCardData(pDC, pInfo);
break;
case 4:
if (pInfo->m_nCurPage == 1)
DrawCertification(pDC, pInfo);
else if (pInfo->m_nCurPage == 2)
DrawCardData(pDC, pInfo);
break;
default:
break;
}
//CScrollView::OnPrint(pDC, pInfo);
}
void CPrintView::OnEndPrintPreview(CDC* pDC, CPrintInfo* pInfo, POINT point, CPreviewView* pView)
{
// TODO: 在此添加专用代码和/或调用基类
if (!m_pCPrintFrame || !pView || !pDC)
return;
//pInfo->m_nCurPage = 1;
//CScrollView::OnEndPrintPreview(pDC, pInfo, point, pView);
CPrintPreviewView* pCPrintPreviewView = (CPrintPreviewView*)pView;
if (!pCPrintPreviewView->m_pPrintView)
OnEndPrinting(pDC, pInfo);
// restore the old main window
m_pCPrintFrame->OnSetPreviewMode(FALSE, pCPrintPreviewView->m_pPreviewState);
// Force active view back to old one
m_pCPrintFrame->SetActiveView(pCPrintPreviewView->m_pPreviewState->pViewActiveOld);
if (m_pCPrintFrame != GetParentFrame())
OnActivateView(TRUE, this, this);
pView->DestroyWindow();
m_pCPrintFrame->RecalcLayout();
m_pCPrintFrame->SendMessage(WM_SETMESSAGESTRING, (WPARAM)AFX_IDS_IDLEMESSAGE, 0L);
m_pCPrintFrame->UpdateWindow();
}
void CPrintView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: 在此添加专用代码和/或调用基类
CScrollView::OnEndPrinting(pDC, pInfo);
//CPrintFrame* pCPrintFrame = (CPrintFrame*)m_pCPrintFrame;
//if (!pCPrintFrame || !pCPrintFrame->m_pDemarManageDlg)
// return;
//if(pInfo->m_bPreview == 0)
// pCPrintFrame->PostMessageW(WM_CLOSE);
}
void CPrintView::DrawCardData(CDC* pDC, CPrintInfo* pInfo)
{
if (!pDC || !pInfo) return;
CPrintFrame* pMainFrm = (CPrintFrame*)m_pCPrintFrame;
if (!pMainFrm) return;
POINT point = { 0, 0 };
SIZE size = { pDC->GetDeviceCaps(HORZRES),
pDC->GetDeviceCaps(VERTRES) };
CRect clientRect(point, size);
chartParam.m_showRect = clientRect;
double nLeftSpace(0.8), nTopSpace(0.8), nRightSpace(0.8), nBottomSpace(0.8);
int iLeftSpace(0), iTopSpace(0), iRightSpace(0), iBottomSpace(0);
iLeftSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSX),
nLeftSpace);
iTopSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSY),
nTopSpace);
iRightSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSX),
nRightSpace);
iBottomSpace = (int)Transfer::Cm2Unit(
Transfer::GetScale(pDC, LOGPIXELSY),
nBottomSpace);
//这里添加打印代码,打印自己想要的
}
void CPrintView::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CScrollView::OnClose();
}
#pragma once
// CPrintPreviewView 视图
class CPrintPreviewView : public CPreviewView
{
DECLARE_DYNCREATE(CPrintPreviewView)
protected:
CPrintPreviewView(); // 动态创建所使用的受保护的构造函数
virtual ~CPrintPreviewView();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
friend class CPrintView;
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnPreviewClose();
afx_msg void OnPreviewPrint();
friend BOOL CALLBACK _AfxMyPreviewCloseProc(CFrameWnd* pFrameWnd);
virtual void OnDraw(CDC* pDC);
};
// CPrintPreviewView.cpp: 实现文件
//
#include "stdafx.h"
#include "DemarManage.h"
#include "CPrintPreviewView.h"
// CPrintPreviewView
IMPLEMENT_DYNCREATE(CPrintPreviewView, CPreviewView)
CPrintPreviewView::CPrintPreviewView()
{
}
CPrintPreviewView::~CPrintPreviewView()
{
}
BEGIN_MESSAGE_MAP(CPrintPreviewView, CPreviewView)
ON_COMMAND(AFX_ID_PREVIEW_CLOSE, OnPreviewClose)
ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
END_MESSAGE_MAP()
// CPrintPreviewView 诊断
#ifdef _DEBUG
void CPrintPreviewView::AssertValid() const
{
CPreviewView::AssertValid();
}
#ifndef _WIN32_WCE
void CPrintPreviewView::Dump(CDumpContext& dc) const
{
CPreviewView::Dump(dc);
}
#endif
#endif //_DEBUG
// CPrintPreviewView 消息处理程序
#include "CPrintFrame.h"
void CPrintPreviewView::OnPreviewClose()
{
CPrintFrame* pf = (CPrintFrame*)::AfxGetMainWnd();
CWinApp * pApp = AfxGetApp();
pApp->m_pMainWnd = pf->m_pDemarManageDlg;
pf->DestroyWindow();
}
void CPrintPreviewView::OnPreviewPrint()
{
m_pPrintView->SendMessage(WM_COMMAND, ID_FILE_PRINT);
OnPreviewClose();
}
void CPrintPreviewView::OnDraw(CDC* pDC)
{
// TODO: 在此添加专用代码和/或调用基类
CPreviewView::OnDraw(pDC);
m_pToolBar->PostMessage(WM_IDLEUPDATECMDUI, (WPARAM)TRUE);// 控制条的命令状态更新
}