📄 mydrawview.cpp
字号:
// MyDrawView.cpp : implementation of the CMyDrawView class
//
#include "stdafx.h"
#include "MyDraw.h"
#include "MyDrawDoc.h"
#include "MyDrawView.h"
#include "Ellipse.h"
#include "Line.h"
#include "LineProperties.h"
#include "EllipseProperties.h"
#include "Rectangle.h"
#include "Text.h"
#include "TextProperties.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView
IMPLEMENT_DYNCREATE(CMyDrawView, CView)
BEGIN_MESSAGE_MAP(CMyDrawView, CView)
//{{AFX_MSG_MAP(CMyDrawView)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_BUTTON_LINE, OnButtonLine)
ON_COMMAND(ID_BUTTON_ELLIPSE, OnButtonEllipse)
ON_COMMAND(ID_BUTTON_RECTANGLE, OnButtonRectangle)
ON_COMMAND(ID_BUTTON_ARC, OnButtonArc)
ON_WM_KEYUP()
ON_COMMAND(ID_BUTTON_TEXT, OnButtonText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView construction/destruction
CMyDrawView::CMyDrawView()
{
// TODO: add construction code here
m_tracker.m_nStyle=CRectTracker::resizeInside|CRectTracker::dottedLine;
m_tracker.m_rect.SetRect(0,0,0,0);
m_tracker.m_nHandleSize=8;
}
CMyDrawView::~CMyDrawView()
{
}
BOOL CMyDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView drawing
void CMyDrawView::OnDraw(CDC* pDC)
{
CMyDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(m_ObjectList.GetCount() <= 0)
return;
POSITION pos = m_ObjectList.GetHeadPosition();
CDrawElement* pObject=NULL;
while(pos != NULL)
{
pObject=(CDrawElement*)m_ObjectList.GetNext(pos);
if(pObject != NULL)
{
if (pObject->bIsSelected)
{
pObject->startX=m_tracker.m_rect.left;
pObject->startY=m_tracker.m_rect.top;
pObject->endX=m_tracker.m_rect.right;
pObject->endY=m_tracker.m_rect.bottom;
}
pObject->Draw(pDC);
if (pObject->bIsSelected)
{
m_tracker.Draw(pDC);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView diagnostics
#ifdef _DEBUG
void CMyDrawView::AssertValid() const
{
CView::AssertValid();
}
void CMyDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDrawDoc* CMyDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDrawDoc)));
return (CMyDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyDrawView message handlers
void CMyDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
POSITION pos = m_ObjectList.GetHeadPosition();
CDrawElement* pHitItem;
while(pos != NULL)
{
pHitItem=(CDrawElement*)m_ObjectList.GetNext(pos);
if(pHitItem != NULL)
{
CRect rc;
rc.left=pHitItem->startX;
rc.top=pHitItem->startY;
rc.right=pHitItem->endX;
rc.bottom=pHitItem->endY;
if (rc.PtInRect(point))
{
ClearSelection();
pHitItem->bIsSelected=true;
m_tracker.m_rect.SetRect(pHitItem->startX,pHitItem->startY,
pHitItem->endX,pHitItem->endY);
}
else
{
pHitItem->bIsSelected=false;
}
}
}
m_tracker.Track(this,point);
Invalidate();
}
BOOL CMyDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(m_tracker.SetCursor(pWnd,nHitTest))
return true;
return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CMyDrawView::ClearSelection()
{
CDrawElement* pHitItem=NULL;
POSITION pos = m_ObjectList.GetHeadPosition();
while(pos != NULL)
{
pHitItem=(CDrawElement*)m_ObjectList.GetNext(pos);
if(pHitItem != NULL)
{
pHitItem->bIsSelected=false;
}
}
return;
}
void CMyDrawView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
POSITION pos = m_ObjectList.GetHeadPosition();
CDrawElement* pHitItem;
while(pos != NULL)
{
pHitItem=(CDrawElement*)m_ObjectList.GetNext(pos);
if(pHitItem != NULL)
{
if (pHitItem->bIsSelected)
{
pHitItem->ShowProperties();
Invalidate();
break;
}
}
}
CView::OnLButtonDblClk(nFlags, point);
}
void CMyDrawView::OnButtonLine()
{
// TODO: Add your command handler code here
CLine* pLine=new CLine;
m_ObjectList.AddTail(pLine);
Invalidate();
}
void CMyDrawView::OnButtonEllipse()
{
// TODO: Add your command handler code here
CEllipse* pEllipse=new CEllipse;
m_ObjectList.AddTail(pEllipse);
Invalidate();
}
void CMyDrawView::OnButtonRectangle()
{
// TODO: Add your command handler code here
CRectangle* pRectangle=new CRectangle;
m_ObjectList.AddTail(pRectangle);
Invalidate();
}
void CMyDrawView::OnButtonArc()
{
// TODO: Add your command handler code here
CArc* pArc=new CArc;
srand(::GetTickCount());
if(rand()%10+1>5)//产生一个1到10的随机数
pArc->Direction=0;
else
pArc->Direction=1;
m_ObjectList.AddTail(pArc);
Invalidate();
}
void CMyDrawView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if (46==nChar)
{
POSITION pos = m_ObjectList.GetTailPosition();
CDrawElement* pHitItem;
while(pos != NULL)
{
POSITION curpos=pos;
pHitItem=(CDrawElement*)m_ObjectList.GetPrev(pos);
if(pHitItem != NULL)
{
if (pHitItem->bIsSelected)
{
m_ObjectList.RemoveAt(curpos);
Invalidate();
break;
}
}
}
}
CView::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CMyDrawView::OnButtonText()
{
// TODO: Add your command handler code here
CText* pText=new CText;
m_ObjectList.AddTail(pText);
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -