📄 drawview.cpp
字号:
// DrawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "HighlightView.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()
ON_WM_ERASEBKGND()
//}}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()
{
m_bActive = FALSE;
}
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);
CBrush brush( RGB( 255, 0, 255 ) );
CBrush* pOldBrush = pDC->SelectObject( &brush );
CRect rect( pDoc->m_point.x-20, pDoc->m_point.y-20,
pDoc->m_point.x+20, pDoc->m_point.y+20 );
switch( pDoc->m_shape )
{
default :
ASSERT( FALSE ); // We should never get here !
break;
case CDrawDoc::SQUARE :
pDC->Rectangle( rect );
break;
case CDrawDoc::CIRCLE :
pDC->Ellipse( rect );
break;
}
CPen pen( PS_SOLID, 2, RGB( 0, 0, 0 ) );
CPen* pOldPen = pDC->SelectObject( &pen );
pDC->MoveTo( pDoc->m_point.x-3, pDoc->m_point.y-3 );
pDC->LineTo( pDoc->m_point.x+3, pDoc->m_point.y+3 );
pDC->MoveTo( pDoc->m_point.x-3, pDoc->m_point.y+3 );
pDC->LineTo( pDoc->m_point.x+3, pDoc->m_point.y-3 );
pDC->SelectObject( pOldPen );
pDC->SelectObject( pOldBrush );
// Draw "view highlight bar" when active
if( m_bActive )
{
CRect rcClient;
GetClientRect( rcClient );
const int iBarWidth = 8;
CPen penHighlight( PS_SOLID, iBarWidth, RGB( 0, 0, 255 ) ); // Blue
CPen* pOldPen = pDC->SelectObject( &penHighlight );
pDC->MoveTo( rcClient.left, rcClient.top + iBarWidth/2 );
pDC->LineTo( rcClient.right, rcClient.top + iBarWidth/2 );
pDC->SelectObject( pOldPen );
}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// 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)
{
CDrawDoc* pDoc = GetDocument();
pDoc->m_point = point;
pDoc->UpdateAllViews( NULL );
pDoc->SetModifiedFlag( TRUE );
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
// 1 - Find out if the state of the view has changed
BOOL bStateChanged = ( m_bActive != bActivate );
m_bActive = bActivate;
// 2 - If changed, trigger repaint
if( bStateChanged )
{
RedrawWindow();
}
CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
BOOL CDrawView::OnEraseBkgnd(CDC* pDC)
{
// If we don't have the focus, call the default implementation
if( !m_bActive )
{
return CView::OnEraseBkgnd(pDC);
}
// If we have the focus, draw our own background
// 1 - Find out area to erase
CRect rect;
pDC->GetClipBox( &rect );
// 2 - Create the correct brush and select into DC
CBrush brush( RGB( 0, 255, 255 ) ); // Cyan
CBrush* pOldBrush = pDC->SelectObject( &brush );
// 3 - Erase the background
pDC->PatBlt( rect.left, rect.top,
rect.Width(), rect.Height(),
PATCOPY );
pDC->SelectObject( pOldBrush );
// 4 - Tell Windows that we erased the background
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -