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

📄 drawview.cpp

📁 画一些简单的图元
💻 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_WM_CREATE()
	ON_COMMAND(IDM_LINE, OnLine)
	ON_COMMAND(IDM_RECT, OnRect)
	ON_COMMAND(IDM_ELLIPSE, OnEllipse)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	ON_WM_MOUSEMOVE()
	ON_WM_ERASEBKGND()
	ON_COMMAND(IDM_SELECT, OnSelect)
	ON_COMMAND(IDM_CLEARALL, OnClearall)
	ON_COMMAND(IDM_SELECTRECT, OnSelectrect)
	ON_COMMAND(IDM_IMAGE, OnImage)
	ON_COMMAND(ID_BUTTON_ARROW, OnButtonArrow)
	//}}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 hereb
	m_nDrawItem = -1;
	m_ptStart = 0;
	m_ptEnd = 0;
	m_pBase = NULL;
	m_pImage = NULL;

	m_pSelectRect = NULL;
	m_bSelectRect = false;

	m_bMouseLBDown = false;
	m_pSelectBase = NULL;
	m_lpsczImageFileName = NULL;

	m_bArrowEnable = FALSE;
	iarrowIndex = 0;
}

CDrawView::~CDrawView()
{
	POSITION pos = m_obList.GetHeadPosition();
	while(pos != NULL)
	{
		CLine* pLine = (CLine*) m_obList.GetNext(pos);

		delete pLine;
	}

	m_obList.RemoveAll();
	delete m_pSelectRect;
}

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);

	CDC		MemDC;
	CBitmap	MemBitmap;
	
	CRect		rtClient;
	GetClientRect(&rtClient);
	
	MemDC.CreateCompatibleDC(pDC);
	MemBitmap.CreateCompatibleBitmap(pDC,rtClient.Width(),rtClient.Height());
	CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
	
	MemDC.FillSolidRect(rtClient,RGB(255,255,255));

	POSITION  pos = m_obList.GetHeadPosition();
	while(pos != NULL)
	{
		CBase* pBase = (CBase*) m_obList.GetNext(pos);
		pBase->Draw(&MemDC);
	}

	if (m_bArrowEnable&&iarrowIndex<m_ptPairsArray.GetSize())
	{

		PointPairs ptPairs = m_ptPairsArray.GetAt(iarrowIndex);
		CArrow arrow;
		arrow.DrawArrow(&MemDC,ptPairs);
		iarrowIndex++;
    }
	
	DrawSelectRect(&MemDC);

   	pDC->BitBlt(0,0,rtClient.Width(),rtClient.Height(),&MemDC,0,0,SRCCOPY);

	MemBitmap.DeleteObject();
	MemDC.DeleteDC();
}

/////////////////////////////////////////////////////////////////////////////
// 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

int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	ShowWindow(SW_HIDE);
	return 0;
}

void CDrawView::OnLine() 
{
	m_nDrawItem = 0;
}

void CDrawView::OnRect() 
{
	m_nDrawItem = 1;
}

void CDrawView::OnEllipse() 
{
	m_nDrawItem = 2;
}

void CDrawView::OnSelect() 
{
	m_nDrawItem = 3;
}

void CDrawView::OnSelectrect() 
{
	m_nDrawItem = 4;
}

void CDrawView::OnImage() 
{
	m_nDrawItem = 5;

	TCHAR str[3200] = {0};
    TCHAR szFile[MAX_PATH+1024];
    
    TCHAR Name[MAX_PATH];
    int i=1;
    memset(szFile,0, sizeof(szFile));
	
    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = _T("*.bmp\0*.bmp\0");
    ofn.nMaxFile = sizeof(szFile)/sizeof(szFile[0]);
    ofn.lpstrFile = szFile;
    ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |OFN_EXPLORER;

	if (GetOpenFileName(&ofn))
    {
        m_lpsczImageFileName = szFile;
    }

	m_pImage = new CImage;

	m_pImage->OpenImageFile(m_lpsczImageFileName);
	m_obList.AddTail(m_pImage);

	Invalidate();
}

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point,CDC* pDC) 
{
	m_bMouseLBDown = true;

	switch(m_nDrawItem) 
	{
	case 0:
		m_pBase = new CLine;
		break;
	case 1:
		m_pBase = new CDrawRect;
		break;
	case 2:
		m_pBase = new CEllipse;
		break;
	case 3:
		SelectGraghObject(point);
		break;
	case 4:
		if(m_pSelectRect == NULL)
		{	
			m_bSelectRect = true;

			m_pSelectRect = new CRect;
			m_pSelectRect->left   = point.x;
			m_pSelectRect->top    = point.y;
			m_pSelectRect->right  = point.x;
			m_pSelectRect->bottom = point.y;

		}
		else if(m_pSelectRect != NULL && !m_pSelectRect->PtInRect(point))
		{			
			m_bSelectRect = true;

			m_pSelectRect->left   = point.x;
			m_pSelectRect->top    = point.y;
			m_pSelectRect->right  = point.x;
			m_pSelectRect->bottom = point.y;
		}
		else if(m_pSelectRect != NULL && m_pSelectRect->PtInRect(point))
		{
			::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
		}
		break;
	default:
		break;
	}
	if(m_nDrawItem == 0 || m_nDrawItem == 1 || m_nDrawItem == 2)
	{
		m_pBase->m_ptStart = point;
		m_pBase->m_ptEnd   = point;

		m_obList.AddTail(m_pBase);
	}
	
	m_ptOldPoint = point;

	if (m_bMouseLBDown && m_bArrowEnable) 
	{		
		//PointPairs ptPairs;
		m_ptPairs.m_ptStart = point;
		m_ptPairs.m_ptEnd = point;
		
	}

	Invalidate();
	CView::OnLButtonDown(nFlags, point);
}


void CDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
	if(m_bMouseLBDown && m_pBase != NULL)
	{
		SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
		
		m_pBase->m_ptEnd = point;
		
		Invalidate();
		
	}
	else if(!m_bMouseLBDown && m_nDrawItem == 3)
	{
		POSITION  pos = m_obList.GetHeadPosition();
		while(pos != NULL)
		{
			CBase* pBase = (CBase*) m_obList.GetNext(pos);
			pBase->m_bStrech = false;
			if(pBase->IsPointIn(point))
			{
				SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			pBase->StrechObject(point);
		}
		
		Invalidate();
	}
	else if(m_bMouseLBDown && m_nDrawItem == 3)
	{
		POSITION  pos = m_obList.GetHeadPosition();
		while(pos != NULL)
		{
			CBase* pBase = (CBase*) m_obList.GetNext(pos);
			pBase->m_bStrech = true;
			if(pBase->IsPointIn(point))
			{
				SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			pBase->StrechObject(point);
		}
		if(m_bMouseLBDown && (m_pSelectBase != NULL))
		{
			CPoint Oldpoint;
			Oldpoint = m_ptOldPoint;
			int altx = point.x - Oldpoint.x;
			int alty = point.y - Oldpoint.y;
			m_pSelectBase->m_ptStart.x += altx;
			m_pSelectBase->m_ptStart.y += alty;
			m_pSelectBase->m_ptEnd.x   += altx;
			m_pSelectBase->m_ptEnd.y   += alty;
			
			m_ptOldPoint = point;
			
			Invalidate();
		}
		Invalidate();
	}

	else if(m_nDrawItem == 4)
	{
		if(m_bMouseLBDown)
		{
			if(m_bSelectRect)
			{
				SelectRect(point);
			}
			else 
			{
				::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
				
				MoveSelectedRect(point);
			}
					
			Invalidate();
		}
		else if(m_pSelectRect != NULL && m_pSelectRect->PtInRect(point))
		{
			::SetCursor(::LoadCursor(NULL,IDC_SIZEALL));
		}
	}

	else if (m_bMouseLBDown && m_bArrowEnable)
	{
		SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
		//m_pPolyline->AddPoint(point);
		
		//xz
		//isMouseMove = true;
			//取箭头的终点
			m_ptPairs.m_ptEnd = point;
			m_ptPairsArray.Add(m_ptPairs);
	
		Invalidate();
	}
	
	CView::OnMouseMove(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_bMouseLBDown && m_pBase != NULL)
	{
		m_pBase->m_ptEnd = point;
		m_pBase = NULL;

		m_bMouseLBDown = false;
		m_ptEnd = point;

		Invalidate();
	}
	else if(m_bMouseLBDown && (m_pSelectBase != NULL))
	{
		m_pSelectBase->m_bSelected = false;
		m_pSelectBase = NULL;

		m_bMouseLBDown = false;
		m_ptOldPoint = point; 		
	}
	else if(m_bMouseLBDown && (m_nDrawItem == 4))
	{
		if(point != m_pSelectRect->TopLeft() && point != m_pSelectRect->BottomRight() && 
			!(point.x == m_pSelectRect->right && point.y == m_pSelectRect->top) &&
			!(point.x == m_pSelectRect->left && point.y == m_pSelectRect->bottom) && 
			!m_pSelectRect->PtInRect(point)  || m_pSelectRect->IsRectEmpty())
		{
			POSITION pos = m_obList.GetHeadPosition();
			while(pos != NULL)
			{
				CBase* pBase = (CBase*) m_obList.GetNext(pos);
				pBase->m_bSelected = false;			
			}

			Invalidate();
		}
		
		m_bSelectRect = false;
		m_bMouseLBDown = false;
	}
	else if(m_bMouseLBDown && m_nDrawItem == 3)
	{		
		POSITION  pos = m_obList.GetHeadPosition();
		while(pos != NULL)
		{
			CBase* pBase = (CBase*) m_obList.GetNext(pos);
			pBase->m_bStrech = false;
			if(pBase->IsPointIn(point))
			{
				SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			pBase->StrechObject(point);
		}
		
		Invalidate();
	}

	::ReleaseCapture();
	m_bMouseLBDown=false;
	
	CView::OnLButtonUp(nFlags, point);
}

void CDrawView::SetSyles(CLine *pLine, CPoint point)
{
	pLine->m_dwColor = RGB(255, 0, 0);
	pLine->m_nStyle = PS_DASH; 
	pLine->m_nWidth = 5;
	pLine->m_ptStart = m_ptStart;
	pLine->m_ptEnd = point;
}


void CDrawView::OnFilePrint() 
{
	// TODO: Add your command handler code here
	
}

BOOL CDrawView::OnEraseBkgnd(CDC* pDC) 
{

	return false;
}



void CDrawView::OnClearall() 
{
	m_obList.RemoveAll();

	if(m_pSelectRect != NULL)
	{
		m_pSelectRect->TopLeft() = (0,0);
		m_pSelectRect->BottomRight() = (0,0);
	}

	Invalidate();
	
}

BOOL CDrawView::SelectGraghObject(CPoint point)
{
	POSITION pos = m_obList.GetTailPosition();
	while(pos != NULL)
	{
		CBase* pBase = (CBase*)m_obList.GetPrev(pos);
		
		if(pBase->IsPointIn(point))
		{
			pBase->m_bSelected = true;
			m_pSelectBase = pBase;
		}
		
		if(pBase->m_bSelected && pos != NULL)
		{
			m_obList.GetNext(pos);
			m_obList.RemoveAt(pos);
			m_obList.AddTail(pBase);
			break;
		}
		else if(pBase->m_bSelected && pos == NULL)
		{
			m_obList.RemoveHead();
			m_obList.AddTail(pBase);
			break;
		}
	}	
	
	return  true;
}


BOOL CDrawView::SelectRect(CPoint point)
{
	CPoint Oldpoint;
	Oldpoint = m_ptOldPoint;
	
	*m_pSelectRect = CRect(Oldpoint,point);
	LONG lTemp;
	if(m_pSelectRect->left > m_pSelectRect->right)
	{
		lTemp = m_pSelectRect->left;
		m_pSelectRect->left = m_pSelectRect->right;
		m_pSelectRect->right = lTemp;
	}
	if(m_pSelectRect->top > m_pSelectRect->bottom)
	{
		lTemp = m_pSelectRect->top;
		m_pSelectRect->top = m_pSelectRect->bottom;
		m_pSelectRect->bottom = lTemp;
	}

	POINT  points[4];
	points[0].x = m_pSelectRect->left;
	points[0].y = m_pSelectRect->top;
	points[1].x = m_pSelectRect->right;
	points[1].y = m_pSelectRect->top;
	points[2].x = m_pSelectRect->right;
	points[2].y = m_pSelectRect->bottom;
	points[3].x = m_pSelectRect->left;
	points[3].y = m_pSelectRect->bottom;
	
	CRgn  rgn;
	rgn.CreatePolygonRgn(points, 4, ALTERNATE);

	POSITION pos = m_obList.GetHeadPosition();
	while(pos != NULL)
	{
		CLine* pLine = (CLine*) m_obList.GetNext(pos);
		
		if(rgn.PtInRegion(pLine->m_ptStart) && rgn.PtInRegion(pLine->m_ptEnd))
		{
			pLine->m_bSelected = true;
		}
		else
		{
			pLine->m_bSelected = false;
		}
	}

	return true;
}

BOOL CDrawView::DrawSelectRect(CDC* pDC)
{
	if(m_pSelectRect != NULL)
	{
		CPen	pen(PS_DASH, 1, RGB(0,0,0));

		pDC->SelectObject(&pen);

		pDC->MoveTo(m_pSelectRect->left,m_pSelectRect->top);
		pDC->LineTo(m_pSelectRect->right,m_pSelectRect->top);
		pDC->MoveTo(m_pSelectRect->right,m_pSelectRect->top);
		pDC->LineTo(m_pSelectRect->right,m_pSelectRect->bottom);
		pDC->MoveTo(m_pSelectRect->right,m_pSelectRect->bottom);
		pDC->LineTo(m_pSelectRect->left,m_pSelectRect->bottom);
		pDC->MoveTo(m_pSelectRect->left,m_pSelectRect->bottom);
		pDC->LineTo(m_pSelectRect->left,m_pSelectRect->top);

		return true;
	}
	
	return false;

}

BOOL CDrawView::MoveSelectedRect(CPoint point)
{
	CPoint Oldpoint;
	Oldpoint = m_ptOldPoint;
	int altx = point.x - Oldpoint.x;
	int alty = point.y - Oldpoint.y;
	m_pSelectRect->left   += altx;
	m_pSelectRect->top    += alty;
	m_pSelectRect->right  += altx;
	m_pSelectRect->bottom += alty;

	CPtrList  TempList;
	POSITION  pos = m_obList.GetHeadPosition();
	while(pos != NULL)
	{
		CLine* pLine = (CLine*) m_obList.GetNext(pos);
		if(!pLine->m_bSelected)
		{
			TempList.AddTail(pLine);
		}
	}
	
	pos = m_obList.GetHeadPosition();
	while(pos != NULL)
	{
		CLine* pLine = (CLine*) m_obList.GetNext(pos);
		if(pLine->m_bSelected)
		{
			pLine->m_ptStart.x += altx;
			pLine->m_ptStart.y += alty;
			pLine->m_ptEnd.x   += altx;
	    	pLine->m_ptEnd.y   += alty;

			TempList.AddTail(pLine);

		}
	}

	m_obList.RemoveAll();

	pos = TempList.GetHeadPosition();
	while(pos != NULL)
	{
		CLine* pLine = (CLine*) TempList.GetNext(pos);

		m_obList.AddTail(pLine);
	}

	m_ptOldPoint = point;
	
	Invalidate();
	return true;
}





void CDrawView::OnButtonArrow() 
{
	// TODO: Add your command handler code here
	m_bArrowEnable = TRUE;
}

//void CDrawView::OnUpdateButtonArrow(CCmdUI* pCmdUI) 
//{
//	// TODO: Add your command update UI handler code here
//	pCmdUI->Enable(m_bArrowEnable);
//}

⌨️ 快捷键说明

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