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

📄 intersectionview.cpp

📁 实现画点画线功能以及二维空间的分析
💻 CPP
字号:
// IntersectionView.cpp : implementation of the CIntersectionView class
//

#include "stdafx.h"
#include "Intersection.h"

#include "IntersectionDoc.h"
#include "IntersectionView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <cmath>                    //求距离中用到开平方运算

static long startx = 0;              //起点:x
static long starty = 0;              //起点:y
static long endx = 0;                //终点:x
static long endy = 0;                //终点:y
static short flagCap = 0;            //标志:画线时记录鼠标捕获状态
static short flagMovePnt = 0;        //标志:点移动


// CIntersectionView

IMPLEMENT_DYNCREATE(CIntersectionView, CView)

BEGIN_MESSAGE_MAP(CIntersectionView, CView)
	// 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)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_DRAWPOINT, &CIntersectionView::OnDrawpoint)
	ON_COMMAND(ID_DRAWLINE, &CIntersectionView::OnDrawline)
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// CIntersectionView construction/destruction

CIntersectionView::CIntersectionView()
: m_iDrawMode(0)
, m_pointNum(0)
, m_lineNum(0)
, m_linepntNum(0)
, m_iNo(-1)

{
	// TODO: add construction code here

}

CIntersectionView::~CIntersectionView()
{
}

BOOL CIntersectionView::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.lpszClass=AfxRegisterWndClass(CS_DBLCLKS,AfxGetApp()->LoadStandardCursor(IDC_CROSS),
   (HBRUSH)(COLOR_WINDOW+1),
   AfxGetApp()->LoadIcon(IDR_MAINFRAME)); 
	return CView::PreCreateWindow(cs);
}

// CIntersectionView drawing

void CIntersectionView::OnDraw(CDC* pDC)
{
	CIntersectionDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//if (!pDoc)
	//	return;

	int i=0,j=0;
	CPen pen;
	pDC->SelectObject(&pen);
	pDC->SetROP2(R2_NOTXORPEN );

	switch(m_iDrawMode)                       //绘图模式
	{
	case 1: 
	case 3:                                  //画点和移动点
		for(i=0;i<m_pointNum;i++)
	pDC->Rectangle((int)m_point[i].x-1,(int)m_point[i].y-1,(int)m_point[i].x+1,(int)m_point[i].y+1);
		break;
	case 2:                                  //画线     
		for(i=0;i<m_lineNum;i++)
		{
			for(j=0;j<m_line[i].num-1;j++)
			{
				pDC->MoveTo((int)m_line[i].pnt[j].x,(int)m_line[i].pnt[j].y);
				pDC->LineTo((int)m_line[i].pnt[j+1].x,(int)m_line[i].pnt[j+1].y);
			}
		}
		break;
	default:
		break;
	}

	// TODO: add draw code for native data here
}


// CIntersectionView printing

BOOL CIntersectionView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CIntersectionView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CIntersectionView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CIntersectionView diagnostics

#ifdef _DEBUG
void CIntersectionView::AssertValid() const
{
	CView::AssertValid();
}

void CIntersectionView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CIntersectionDoc* CIntersectionView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CIntersectionDoc)));
	return (CIntersectionDoc*)m_pDocument;
}
#endif //_DEBUG


// CIntersectionView message handlers

void CIntersectionView::OnLButtonDown(UINT nFlags, CPoint point)
{
	CDC* pDC = GetDC();                           //获取设备环境   
	CPen pen, *pOldPen=NULL;                      //定义画笔
	pDC->SetROP2(R2_NOTXORPEN     );              //设置绘图模式(异或)
	pOldPen = pDC->SelectObject(&pen);           //将新画笔选入设备环境,并保存旧画笔
	
	if(m_iDrawMode == 1)                         //如果画点
	{
		m_point[m_pointNum].x = (float)point.x;   //将点的x坐标放入点数组
		m_point[m_pointNum].y = (float)point.y;   //将点的y坐标放入点数组
	
	pDC->Rectangle((int)m_point[m_pointNum].x-1,(int)m_point[m_pointNum].y-1,(int)m_point[m_pointNum].x+1,(int)m_point[m_pointNum].y+1);          //在该点画小矩形
		m_pointNum++;                             //点数增1
	}

	if(m_iDrawMode == 2)                          //如果画线
	{
		startx = point.x;                       //将鼠标点分别赋给起点和终点
		starty = point.y;
		endx = point.x;
		endy = point.y;

		pDC->MoveTo(startx,starty);              //抬笔到起点
		pDC->LineTo(endx,endy);                  //落笔到终点

		
		m_linepnt[m_linepntNum].x  = (float)point.x;  //将鼠标点放入线数组
		m_linepnt[m_linepntNum].y = (float)point.y;
		m_linepntNum++;                               //线数组中的点数增1
		
		SetCapture();                                 //设置有效区域的捕获方式
		flagCap = 1;                               //抓线标志置1(0:第一个点; 1:非第一个点)
	}

	//if(m_iDrawMode == 3)                           //如果是移动点
	//{
	//	if(flagMovePnt==0)
	//	{
	//		flagMovePnt = 1;                 //移动点标志置1

	//		PNT_INFO pnt;
	//		pnt.x = (float)point.x;          //将鼠标点放入点结构体变量  
	//		pnt.y = (float)point.y;
	//		//需要加入求距离最短点的函数,返回点数组下标;
	//		//m_iNo = CalcMinDist(m_point,m_pointNum,pnt);   //调函数求出离鼠标点最近点的下标

	//		SetCapture();                   //设置有效区域的捕获方式
	//	}
	//	else
	//	{
	//		flagMovePnt = 0;
	//		::ReleaseCapture();            //释放捕获
	//	}	
	//}

	pDC->SelectObject(pOldPen);              //选回旧画笔    
	ReleaseDC(pDC);                          //释放设备环境

	CView::OnLButtonDown(nFlags, point);
}

void CIntersectionView::OnRButtonDown(UINT nFlags, CPoint point)
{
	CDC* pDC = GetDC();                       //获取设备环境
	CPen pen, *pOldPen=NULL;                  //定义画笔
	pDC->SetROP2(R2_NOTXORPEN );              //设置绘图模式
	pOldPen = pDC->SelectObject(&pen);        //将画笔选入设备环境,并保存旧画笔
	
	if(m_iDrawMode == 2)                     //如果画线 
	{
		pDC->MoveTo(startx,starty);          //抬笔到起点
		pDC->LineTo(endx,endy);              //落笔到终点 

		endx = point.x;                      
		endy = point.y;

		flagCap = 0;

		for(int i=0;i<m_linepntNum;i++)        //将线上的所有点放入线点数组
		{
			m_line[m_lineNum].pnt[i].x = m_linepnt[i].x;
			m_line[m_lineNum].pnt[i].y = m_linepnt[i].y;
		}
		m_line[m_lineNum].num = m_linepntNum;    //记下线上的点数
		m_lineNum++;                             //线条数增1            

		m_linepntNum = 0;                         //线上的点数置0,为画下一条线作准备
	}

	::ReleaseCapture();

	pDC->SelectObject(pOldPen);              //将旧画笔选入设备环境,为退出绘图作准备
	ReleaseDC(pDC);                          //释放设备环境

	CView::OnRButtonDown(nFlags, point);
}

void CIntersectionView::OnDrawpoint()
{
	m_iDrawMode = 1; 
}

void CIntersectionView::OnDrawline()
{
	m_iDrawMode = 2; 
}

void CIntersectionView::OnMouseMove(UINT nFlags, CPoint point)
{
   CDC* pDC = GetDC();                    //获取设备环境
	CPen pen, *pOldPen=NULL;               //定义画笔
	pDC->SetROP2(R2_NOTXORPEN);            //设置绘图模式
	pOldPen = pDC->SelectObject(&pen);     //将画笔选入设备环境,并保存旧画笔
	
	if(m_iDrawMode == 2)                 //如果画线,进行橡皮筋方式画线
	{
		if(flagCap==1)
		{
			pDC->MoveTo(startx,starty);    //擦除线(异或方式画)
			pDC->LineTo(endx,endy);

			endx = point.x;                //记下新的点(鼠标点)
			endy = point.y;

			pDC->MoveTo(startx,starty);    //画新线(从原始起点到鼠标点间画橡皮筋线)
			pDC->LineTo(endx,endy);
		}
	}

	if(m_iDrawMode == 3)                   //如果移动点
	{
		if(m_iNo<0)                       //点的下标异常处理
			goto lab;                    //跳转到标号lab处
		if(flagMovePnt==1)                
		{
		                                  //擦除离鼠标最近点的小矩形
pDC->Rectangle(m_point[m_iNo].x-5,m_point[m_iNo].y-5,m_point[m_iNo].x+5,m_point[m_iNo].y+5);
			m_point[m_iNo].x = point.x;   //将该点坐标写回到点数组中
			m_point[m_iNo].y = point.y;
		                                    //在移动鼠标点画小矩形
	pDC->Rectangle(m_point[m_iNo].x-5,m_point[m_iNo].y-5,m_point[m_iNo].x+5,m_point[m_iNo].y+5);		}
	}
	
lab:
	pDC->SelectObject(pOldPen);           //将旧画笔选入设备环境,为退出绘图作准备
	ReleaseDC(pDC);                       //释放设备环境

	CView::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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