previewview.cpp
来自「Visual C++ 的学习资料一」· C++ 代码 · 共 157 行
CPP
157 行
// previewView.cpp : implementation of the CMyPreviewView class
//
#include "stdafx.h"
#include "preview.h"
#include "previewDoc.h"
#include "previewView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView
IMPLEMENT_DYNCREATE(CMyPreviewView, CView)
BEGIN_MESSAGE_MAP(CMyPreviewView, CView)
//{{AFX_MSG_MAP(CMyPreviewView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView construction/destruction
CMyPreviewView::CMyPreviewView()
{
// TODO: add construction code here
}
CMyPreviewView::~CMyPreviewView()
{
}
BOOL CMyPreviewView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView drawing
void CMyPreviewView::OnDraw(CDC* pDC)
{
CPreviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
titlefont.CreatePointFont(200,"宋体",pDC);
bodyfont.CreatePointFont(120,"宋体",pDC);
screenx =pDC->GetDeviceCaps(LOGPIXELSX);
screeny =pDC->GetDeviceCaps(LOGPIXELSY);
CRect rect;
GetClientRect(rect);
rect.DeflateRect(0,50,0,0);
pDC->SelectObject(&titlefont);
pDC->DrawText("打印预览",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left,m_rect.top+100,m_rect.Width(),m_rect.bottom);
pDC->DrawText("在基于文档、视图的结构中自定义打印预览",temprect,DT_CENTER);
titlefont.DeleteObject();
bodyfont.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView printing
BOOL CMyPreviewView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyPreviewView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
printx =pDC->GetDeviceCaps(LOGPIXELSX);
printy =pDC->GetDeviceCaps(LOGPIXELSY);
ratex = (double)printx /screenx; //确定打印机与屏幕的比率
ratey = (double)printy /screeny;
pageheight =pDC->GetDeviceCaps(VERTRES);
pagewidth =pDC->GetDeviceCaps(HORZRES);
//获取打印机的左右边距
leftmargin = pDC->GetDeviceCaps(PHYSICALOFFSETX);//获取左边距
int phywidth= pDC->GetDeviceCaps(PHYSICALWIDTH);
rightmargin = phywidth-pagewidth-leftmargin;
}
void CMyPreviewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView diagnostics
#ifdef _DEBUG
void CMyPreviewView::AssertValid() const
{
CView::AssertValid();
}
void CMyPreviewView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPreviewDoc* CMyPreviewView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPreviewDoc)));
return (CPreviewDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyPreviewView message handlers
void CMyPreviewView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
titlefont.CreatePointFont(200,"宋体",pDC);
bodyfont.CreatePointFont(120,"宋体",pDC);
screenx =pDC->GetDeviceCaps(LOGPIXELSX);
screeny =pDC->GetDeviceCaps(LOGPIXELSY);
CRect rect(-leftmargin,0,pagewidth+rightmargin,pageheight);
rect.DeflateRect(0,(int)(ratey*50),0,0);
pDC->SelectObject(&titlefont);
pDC->StartDoc("printinformation");
pDC->DrawText("打印预览",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left,m_rect.top+(int)(ratey*100),m_rect.Width(),m_rect.bottom);
pDC->DrawText("在基于文档、视图的结构中自定义打印预览",temprect,DT_CENTER);
pDC->EndDoc();
titlefont.DeleteObject();
bodyfont.DeleteObject();
// CView::OnPrint(pDC, pInfo);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?