📄 drawview.cpp
字号:
// DrawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "PrintMono.h"
#include "DrawDoc.h"
#include "DrawView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CView)
BEGIN_MESSAGE_MAP(CDrawView, CView)
//{{AFX_MSG_MAP(CDrawView)
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()
/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
for( int i = 0; i < pDoc->m_Shapes.GetSize(); ++i )
{
CShape* pShape = pDoc->m_Shapes.GetAt( i );
ASSERT_VALID( pShape );
ASSERT_KINDOF( CShape, pShape );
pShape->Draw( pDC );
}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// Convert point to logical coordinates
CClientDC dc( this );
OnPrepareDC( &dc );
dc.DPtoLP( &point );
// Add new shape to document
CDrawDoc* pDoc = GetDocument();
pDoc->AddShape( point );
CView::OnLButtonDown(nFlags, point);
}
/////////////////////////////////////////////////////////////////////////////
void CDrawView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CView::OnPrepareDC(pDC, pInfo);
pDC->SetMapMode( MM_LOMETRIC );
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// Compute document page length
CDrawDoc* pDoc = GetDocument();
int nPages = pDoc->m_Shapes.GetSize() / ELEMENTS_PER_PAGE;
if( ( pDoc->m_Shapes.GetSize() % ELEMENTS_PER_PAGE ) > 0 )
{
++nPages;
}
pInfo->SetMaxPage( nPages );
// default preparation (shows the "Print" dialog box)
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// Create special font for printing
// ...prepare LOGFONT structure
LOGFONT lf; // logical font structure
::ZeroMemory( &lf, sizeof( lf ) );
// ...12 point Times bold
lf.lfHeight = - MulDiv( 12, pDC->GetDeviceCaps( LOGPIXELSX ), 72 );
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = FW_BOLD;
lf.lfItalic = FALSE;
lf.lfUnderline = FALSE;
lf.lfStrikeOut = FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
// ...create printing font
m_PrintFont.CreateFontIndirect( &lf );
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// Release the printing font
m_PrintFont.DeleteObject();
}
void CDrawView::PrintPageHeader( CDC* pDC, CPrintInfo* pInfo )
{
CRect& rcPage = pInfo->m_rectDraw;
// 1 - Prepare page header
CString strHeader = _T( "Document: " );
strHeader += GetDocument()->GetPathName();
CSize sizeHeader = pDC->GetTextExtent( strHeader );
// 2 - Draw header
int nCurrentY = rcPage.top - TOP_MARGIN;
pDC->TextOut( 0, nCurrentY, strHeader );
nCurrentY -= sizeHeader.cy * 2;
// 3 - Draw line
pDC->MoveTo( 0, nCurrentY );
pDC->LineTo( rcPage.right, nCurrentY );
// 4 - Adjust remaining printable area
rcPage.top = nCurrentY;
}
void CDrawView::PrintPageFooter( CDC* pDC, CPrintInfo* pInfo )
{
CRect& rcPage = pInfo->m_rectDraw;
// 1 - Prepare page footer
CString strFooter;
strFooter.Format( _T( "Page %d of %d" ), pInfo->m_nCurPage, pInfo->GetMaxPage() );
CSize sizeFooter = pDC->GetTextExtent( strFooter );
// 2 - Draw line
int nBottomY = rcPage.bottom + BOTTOM_MARGIN + sizeFooter.cy * 2;
int nCurrentY = nBottomY;
pDC->MoveTo( 0, nCurrentY );
pDC->LineTo( rcPage.right, nCurrentY );
nCurrentY -= sizeFooter.cy;
// 3 - Draw footer
pDC->TextOut( rcPage.left + ( rcPage.Width() - sizeFooter.cx ) / 2,
nCurrentY,
strFooter );
// 4 - Adjust remaining printable area
rcPage.bottom = nBottomY;
}
void CDrawView::PrintPageBody( CDC* pDC, CPrintInfo* pInfo )
{
CFont* pOldFont = pDC->SelectObject( &m_PrintFont );
// 1 - Compute which elements we will print on this page
CDrawDoc* pDoc = GetDocument();
int nStart = ( pInfo->m_nCurPage - 1 ) * ELEMENTS_PER_PAGE;
int nEnd = min( nStart + ELEMENTS_PER_PAGE, pDoc->m_Shapes.GetSize() );
// 2 - Print each element
int nCurrentY = pInfo->m_rectDraw.top - 200;
for( int i = nStart; i < nEnd; ++i )
{
// 3 - Draw shape
CShape* pShape = pDoc->m_Shapes.GetAt( i );
pShape->DrawAt( pDC, LEFT_MARGIN, nCurrentY - CShape::HALF_SIZE );
// 4 - Draw shape name
CString strName = pShape->GetShapeName();
pDC->TextOut( LEFT_MARGIN + 200, nCurrentY, strName );
nCurrentY -= pDC->GetTextExtent( strName ).cy;
// 5 - Draw coordinates
CString strCoords;
strCoords.Format( _T( "X = %d, Y = %d" ),
pShape->m_ptCenter.x, pShape->m_ptCenter.y );
pDC->TextOut( LEFT_MARGIN + 200, nCurrentY, strCoords );
nCurrentY -= pDC->GetTextExtent( strCoords ).cy;
// 6 - Draw color
CString strColor;
strColor.Format( _T( "( R, G, B ) = ( %d, %d, %d )" ),
GetRValue( pShape->m_crColor ),
GetGValue( pShape->m_crColor ),
GetBValue( pShape->m_crColor ) );
pDC->TextOut( LEFT_MARGIN + 200, nCurrentY, strColor );
nCurrentY -= pDC->GetTextExtent( strColor ).cy;
// 7 - Draw separator line (if needed)
if( i < nEnd-1 )
{
pDC->MoveTo( LEFT_MARGIN-CShape::HALF_SIZE, nCurrentY - 100 );
pDC->LineTo( 1000, nCurrentY - 100 );
nCurrentY -= 200;
}
}
// 8 - Cleanup
pDC->SelectObject( pOldFont );
}
void CDrawView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// The PrintPageHeader() and PrintPageFooter() functions
// *must* be called before PrintPageBody(), because they
// adjust pInfo->m_rectDraw to take into account the
// space occupied by the header and footer
PrintPageHeader( pDC, pInfo );
PrintPageFooter( pDC, pInfo );
PrintPageBody( pDC, pInfo );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -