⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydrawview.cpp

📁 VC++绘图软件开发教程事例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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
	
	POSITION pos = m_ObjectList.GetHeadPosition();
	CEllipse* pEllipse = NULL;
	CLine* pLine=NULL;
	CRectangle* pRectangle=NULL;
	CArc* pArc=NULL;
	CText* pText=NULL;

	CObject* pObject=NULL;
	
	while(pos != NULL)
	{
		pObject=(CObject*)m_ObjectList.GetNext(pos);
		if (pObject->IsKindOf(RUNTIME_CLASS(CEllipse)))
		{
			pEllipse = (CEllipse*)pObject;
			if(pEllipse != NULL)
			{
				if (pEllipse->bIsSelected)
				{
					pEllipse->startX=m_tracker.m_rect.left;
					pEllipse->startY=m_tracker.m_rect.top;
					pEllipse->endX=m_tracker.m_rect.right;
					pEllipse->endY=m_tracker.m_rect.bottom;
				}
				DrawEllipse(pDC,pEllipse);
				if (pEllipse->bIsSelected)
				{
					m_tracker.Draw(pDC); 
				}
			}
		}
		else if (pObject->IsKindOf(RUNTIME_CLASS(CLine)))
		{
			pLine = (CLine*)pObject;
			if(pLine != NULL)
			{
				if (pLine->bIsSelected)
				{
					pLine->startX=m_tracker.m_rect.left;
					pLine->startY=m_tracker.m_rect.top;
					pLine->endX=m_tracker.m_rect.right;
					pLine->endY=m_tracker.m_rect.bottom;   
				}				
				DrawLine(pDC,pLine);
				if (pLine->bIsSelected)
				{
					m_tracker.Draw(pDC);
				}				
			}
		}
		else if (pObject->IsKindOf(RUNTIME_CLASS(CArc)))
		{
			pArc = (CArc*)pObject;
			if(pArc != NULL)
			{
				if (pArc->bIsSelected)
				{
					pArc->startX=m_tracker.m_rect.left;
					pArc->startY=m_tracker.m_rect.top;
					pArc->endX=m_tracker.m_rect.right;
					pArc->endY=m_tracker.m_rect.bottom;   
				}				
				DrawArc(pDC,pArc);
				if (pArc->bIsSelected)
				{
					m_tracker.Draw(pDC);
				}				
			}
		}
		else if (pObject->IsKindOf(RUNTIME_CLASS(CText)))
		{
			pText = (CText*)pObject;
			if(pText != NULL)
			{
				if (pText->bIsSelected)
				{
					pText->startX=m_tracker.m_rect.left;
					pText->startY=m_tracker.m_rect.top;
					pText->endX=m_tracker.m_rect.right;
					pText->endY=m_tracker.m_rect.bottom;   
				}				
				DrawMyText(pDC,pText);
				if (pText->bIsSelected)
				{
					m_tracker.Draw(pDC);
				}				
			}
		}
		else if (pObject->IsKindOf(RUNTIME_CLASS(CRectangle)))
		{
			pRectangle = (CRectangle*)pObject;
			if(pRectangle != NULL)
			{
				if (pRectangle->bIsSelected)
				{
					pRectangle->startX=m_tracker.m_rect.left;
					pRectangle->startY=m_tracker.m_rect.top;
					pRectangle->endX=m_tracker.m_rect.right;
					pRectangle->endY=m_tracker.m_rect.bottom;
				}
				DrawRectangle(pDC,pRectangle);
				if (pRectangle->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
	CEllipse* pEllipse = NULL;
	CLine* pLine=NULL;
	CRectangle* pRectangle=NULL;
	CArc* pArc=NULL;
	CText* pText=NULL;
	
	POSITION pos = m_ObjectList.GetHeadPosition();
	CObject* pHitItem;
	while(pos != NULL)
	{
		pHitItem=(CObject*)m_ObjectList.GetNext(pos);
		if (pHitItem->IsKindOf(RUNTIME_CLASS(CEllipse)))
		{
			pEllipse = (CEllipse*)pHitItem;
			if(pEllipse != NULL)
			{
				CRect rc;
				rc.left=pEllipse->startX;
				rc.top=pEllipse->startY;
				rc.right=pEllipse->endX;
				rc.bottom=pEllipse->endY;
				if (rc.PtInRect(point))
				{
					ClearSelection();
					pEllipse->bIsSelected=true; 
					m_tracker.m_rect.SetRect(pEllipse->startX,pEllipse->startY,
						pEllipse->endX,pEllipse->endY);  
				}
				else
				{
					pEllipse->bIsSelected=false; 
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CLine)))
		{
			pLine = (CLine*)pHitItem;
			if(pLine != NULL)
			{
				CRect rc(pLine->startX,pLine->startY,pLine->endX,pLine->endY);
				if (rc.PtInRect(point))
				{
					ClearSelection();
					pLine->bIsSelected=true; 
					m_tracker.m_rect.SetRect(pLine->startX,pLine->startY,
						pLine->endX,pLine->endY);  
				}
				else
				{
					pLine->bIsSelected=false; 
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CArc)))
		{
			pArc = (CArc*)pHitItem;
			if(pArc != NULL)
			{
				CRect rc(pArc->startX,pArc->startY,pArc->endX,pArc->endY);
				if (rc.PtInRect(point))
				{
					ClearSelection();
					pArc->bIsSelected=true; 
					m_tracker.m_rect.SetRect(pArc->startX,pArc->startY,
						pArc->endX,pArc->endY);  
				}
				else
				{
					pArc->bIsSelected=false; 
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CText)))
		{
			pText = (CText*)pHitItem;
			if(pText != NULL)
			{
				CRect rc(pText->startX,pText->startY,pText->endX,pText->endY);
				if (rc.PtInRect(point))
				{
					ClearSelection();
					pText->bIsSelected=true; 
					m_tracker.m_rect.SetRect(pText->startX,pText->startY,
						pText->endX,pText->endY);  
				}
				else
				{
					pText->bIsSelected=false; 
				}
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CRectangle)))
		{
			pRectangle = (CRectangle*)pHitItem;
			if(pRectangle != NULL)
			{
				CRect rc;
				rc.left=pRectangle->startX;
				rc.top=pRectangle->startY;
				rc.right=pRectangle->endX;
				rc.bottom=pRectangle->endY;
				if (rc.PtInRect(point))
				{
					ClearSelection();
					pRectangle->bIsSelected=true; 
					m_tracker.m_rect.SetRect(pRectangle->startX,
						pRectangle->startY,pRectangle->endX,pRectangle->endY);  
				}
				else
				{
					pRectangle->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()
{
	CObject* pHitItem=NULL;
	POSITION pos = m_ObjectList.GetHeadPosition();
	CEllipse* pEllipse = NULL;
	CLine* pLine=NULL;
	CRectangle* pRectangle=NULL;
	CArc* pArc=NULL;
	CText* pText=NULL;
	
	while(pos != NULL)
	{
		pHitItem=(CObject*)m_ObjectList.GetNext(pos);
		if (pHitItem->IsKindOf(RUNTIME_CLASS(CEllipse)))
		{
			pEllipse = (CEllipse*)pHitItem;
			if(pEllipse != NULL)
			{
				pEllipse->bIsSelected=false; 
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CLine)))
		{
			pLine = (CLine*)pHitItem;
			if(pLine != NULL)
			{
				pLine->bIsSelected=false; 
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CRectangle)))
		{
			pRectangle = (CRectangle*)pHitItem;
			if(pRectangle != NULL)
			{
				pRectangle->bIsSelected=false; 
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CArc)))
		{
			pArc = (CArc*)pHitItem;
			if(pArc != NULL)
			{
				pArc->bIsSelected=false; 
			}
		}
		else if (pHitItem->IsKindOf(RUNTIME_CLASS(CText)))
		{
			pText = (CText*)pHitItem;
			if(pText != NULL)
			{
				pText->bIsSelected=false; 
			}
		}
	}
	return;
}

void CMyDrawView::DrawLine(CDC *pDC, CLine *pLine)
{
	CPen cMyPen;
	CPen* pOldPen;
	cMyPen.CreatePenIndirect(&pLine->LinePen);
	
	pOldPen=pDC->SelectObject(&cMyPen);
	pDC->MoveTo(pLine->startX,pLine->startY); 
	pDC->LineTo(pLine->endX,pLine->endY); 	
	pDC->SelectObject(pOldPen);
}

void CMyDrawView::DrawArc(CDC *pDC, CArc *pArc)
{
	CPen cMyPen;
	CPen* pOldPen;
	cMyPen.CreatePenIndirect(&pArc->LinePen);
	
	pOldPen=pDC->SelectObject(&cMyPen);
	if(pArc->Direction)
	{
		pDC->Arc(pArc->startX,pArc->startY,pArc->endX,pArc->endY,
			pArc->startX,pArc->startY,pArc->endX,pArc->endY); 	
	}
	else
	{
		pDC->Arc(pArc->endX,pArc->endY,pArc->startX,pArc->startY,
			pArc->endX,pArc->endY,
			pArc->startX,pArc->startY); 	
	}
	pDC->SelectObject(pOldPen);
}

void CMyDrawView::DrawMyText(CDC *pDC, CText *pText)
{
	CFont cMyFont;
	CFont* pOldFont;
	cMyFont.CreateFontIndirect(&pText->MyFont);
	
	pOldFont=pDC->SelectObject(&cMyFont);
	pDC->SetBkMode(pText->BkMode);  
	pDC->SetTextColor(pText->MyColor);  
	pDC->DrawText(pText->MyText,CRect(pText->startX,pText->startY,
		pText->endX,pText->endY),DT_LEFT);
	pDC->SelectObject(pOldFont);
}

void CMyDrawView::DrawEllipse(CDC *pDC, CEllipse *pEllipse)
{
	CPen cMyPen;
	CPen* pOldPen;
	
	cMyPen.CreatePenIndirect(&pEllipse->LinePen);
	pOldPen=pDC->SelectObject(&cMyPen);
	
	CBrush cMyBrush;
	CBrush* pOldBrush;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -