📄 drawview.cpp
字号:
// DrawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "Draw.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_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
ON_COMMAND(ID_DRAW_SKETCH, OnDrawSketch)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, OnUpdateDrawRectangle)
ON_UPDATE_COMMAND_UI(ID_DRAW_SKETCH, OnUpdateDrawSketch)
//}}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);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// 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::OnDrawEllipse()
{
ResetAllFlags();
m_bEllipse=true;
// TODO: Add your command handler code here
}
void CDrawView::OnDrawRectangle()
{
ResetAllFlags();
m_bRectangle=true;
// TODO: Add your command handler code here
}
void CDrawView::OnDrawSketch()
{
ResetAllFlags();
m_bSketch=true;
// TODO: Add your command handler code here
}
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_bRectangle||m_bEllipse)
{
m_startPoint=point;
m_LastEndPoint=point;
SetCapture();
}
if(m_bSketch)
{
m_startPoint=point;
}
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
if(m_bRectangle&&(GetCapture()==this))
{
ReleaseCapture();
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
}
if(m_bEllipse&&(GetCapture()==this))
{
ReleaseCapture();
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
dc.Ellipse(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
}
// TODO: Add your message handler code here and/or call default
CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
CClientDC dc(this);
if(m_bSketch&&(nFlags&&MK_LBUTTON))
{
dc.MoveTo(m_startPoint);
dc.LineTo(point);
m_startPoint=point;
}
if(m_bRectangle&&(nFlags&&MK_LBUTTON))
{
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
int nDrawmdoe=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Rectangle(m_LastEndPoint.x,m_LastEndPoint.y,m_startPoint.x,m_startPoint.y);
dc.SetROP2(nDrawmdoe);
dc.Rectangle(m_startPoint.x,m_startPoint.y,point.x,point.y);
dc.SelectObject(pOldObject);
m_LastEndPoint=point;
}
if(m_bEllipse&&(nFlags&&MK_LBUTTON))
{
CGdiObject*pOldObject=dc.SelectStockObject(NULL_BRUSH);
int nDrawmode=dc.GetROP2();
dc.SetROP2(R2_NOTCOPYPEN);
dc.Ellipse(m_LastEndPoint.x,m_LastEndPoint.y,m_startPoint.x,m_startPoint.y);
dc.SelectObject(pOldObject);
m_LastEndPoint=point;
}
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
void CDrawView::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bEllipse);
// TODO: Add your command update UI handler code here
}
void CDrawView::OnUpdateDrawRectangle(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bRectangle);
// TODO: Add your command update UI handler code here
}
void CDrawView::OnUpdateDrawSketch(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bSketch);
// TODO: Add your command update UI handler code here
}
void CDrawView::ResetAllFlags()
{
m_bRectangle=false;
m_bSketch=false;
m_bEllipse=false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -