myview.cpp
来自「由吕军等人著的《Visual C++ 与面向对象程序设计教程》课后习题的源代码」· C++ 代码 · 共 201 行
CPP
201 行
// MyView.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "My.h"
#include "MyDoc.h"
#include "MyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
m_nLinePerPage=40;
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
pDC->Rectangle(rect);
for(int i=0; i<pDoc->m_nBubbleCount; i++)
pDC->Ellipse(pDoc->m_rectBubble[i]);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
CMyDoc *pDoc = GetDocument();
int nPageCount = pDoc->m_nBubbleCount/m_nLinePerPage;
if(pDoc->m_nBubbleCount % m_nLinePerPage)
nPageCount ++;
pInfo->SetMaxPage(nPageCount);
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this); // 设置设备环境
OnPrepareDC(&dc);
if(pDoc->m_nBubbleCount < MAX_BUBBLE)
{
int r = rand()%50+10;
CRect rect(point.x-r, point.y-r, point.x+r, point.y+r);
InvalidateRect(rect, FALSE);
dc.DPtoLP(rect); // 转换物理坐标为逻辑坐标
pDoc->m_rectBubble[pDoc->m_nBubbleCount] = rect;// 修改文档数据
pDoc->m_nBubbleCount++;
pDoc->SetModifiedFlag(); // 设置修改标志
}
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
pDC->SetMapMode(MM_TWIPS);
CView::OnPrepareDC(pDC, pInfo);
}
void CMyView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
int nPage = pInfo->m_nCurPage; // 当前页号
int nStart = (nPage-1)*m_nLinePerPage; // 本页第一行
int nEnd = nStart+m_nLinePerPage; // 本页最后一行
CFont font; // 设置字体
font.CreateFont(-280, 0, 0, 0, 400, FALSE, FALSE,
0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH|FF_MODERN, "Courier New");
CFont *pOldFont = (CFont *)(pDC->SelectObject(&font));
CRect rectPaper = pInfo->m_rectDraw; // 取页面打印矩形
// 页眉: 页面顶端中央打印文档名称
CMyDoc *pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString str;
str.Format("Bubble Report: %s", (LPCSTR)pDoc->GetTitle());
CSize sizeText = pDC->GetTextExtent(str);
CPoint point((rectPaper.Width()-sizeText.cx)/2, 0);
pDC->TextOut(point.x, point.y, str);
point.x = rectPaper.left; // 打印页眉下划线
point.y = rectPaper.top-sizeText.cy;
pDC->MoveTo(point);
point.x = rectPaper.right;
pDC->LineTo(point);
// 打印表头
str.Format("%6.6s %6.6s %6.6s %6.6s %6.6s",
"Index", "Left", "Top", "Right", "Bottom");
point.x = 720;
point.y -= 720;
pDC->TextOut(point.x, point.y, str);
TEXTMETRIC tm; // 取当前字体有关信息
pDC->GetTextMetrics(&tm);
int nHeight = tm.tmHeight+tm.tmExternalLeading;
point.y -= 360; // 下移 1/4 英寸
for(int i=nStart; i<nEnd; i++) // 打印表体
{
if(i >= pDoc->m_nBubbleCount)
break;
str.Format("%6d %6d %6d %6d %6d", i+1,
pDoc->m_rectBubble[i].left,
pDoc->m_rectBubble[i].top,
pDoc->m_rectBubble[i].right,
pDoc->m_rectBubble[i].bottom);
point.y -= nHeight;
pDC->TextOut(point.x, point.y, str);
}
// 在页面底部中央打印页号
str.Format("- %d -", nPage);
sizeText = pDC->GetTextExtent(str);
point.x = (rectPaper.Width()-sizeText.cx)/2;
point.y = rectPaper.Height()+sizeText.cy;
pDC->TextOut(point.x, point.y, str);
// 释放字体对象
pDC->SelectObject(pOldFont);
//CView::OnPrint(pDC, pInfo);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?