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

📄 mineview.cpp

📁 能进行画图 鼠标移动画圆 矩形 同时改变光标
💻 CPP
字号:
// mineView.cpp : implementation of the CMineView class
//

#include "stdafx.h"
#include "mine.h"

#include "mineDoc.h"
#include "mineView.h"

#include "robot.h"


#include "const.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMineView

IMPLEMENT_DYNCREATE(CMineView, CView)

BEGIN_MESSAGE_MAP(CMineView, CView)
	//{{AFX_MSG_MAP(CMineView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_ObstCircle, OnObstCircle)
	ON_COMMAND(ID_ObstRectangle, OnObstRectangle)
	ON_COMMAND(ID_CREATEROBOT, OnCreaterobot)
	ON_COMMAND(ID_Arrow, OnArrow)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMineView construction/destruction

CMineView::CMineView()
{
	// TODO: add construction code here
	m_ptOld=CPoint(0,0);	
	m_ptStart= CPoint(0,0);
	m_ptEnd= CPoint(0,0);
	m_CircleCenter =CPoint(0,0);

	m_nDrawType=0;
    m_nDrawType=0;
	m_Radius=0;


	m_bDrawing= FALSE;
	m_bErase = FALSE;
	m_CreateRobot= FALSE;


}

CMineView::~CMineView()
{
}

BOOL CMineView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMineView drawing

void CMineView::OnDraw(CDC* pDC)
{
	
	// TODO: add draw code for native data here

}

/////////////////////////////////////////////////////////////////////////////
// CMineView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMineView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMineView message handlers

void CMineView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	m_bDrawing= TRUE;
	//SetCapture();
	m_ptStart= point;

	if(m_nDrawType==CREATEROBOT)
	{CDC *pDC=GetDC();
//	void CRobot::Draw(CDC *pDC,CPoint point);	
	pDC->SelectObject(RED_PEN);
	pDC->SelectObject(GRAY);
	pDC->Ellipse(m_ptStart.x-10,m_ptStart.y-10,m_ptStart.x+10,m_ptStart.y+10);
	pDC->SetPixel(m_ptStart.x,m_ptStart.y,RGB(0,0,0));
	ReleaseDC(pDC);
	
	};
	
	CView::OnLButtonDown(nFlags, point);
}

void CMineView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	//ReleaseCapture();
	if(m_bDrawing==TRUE)
	{
		CDC* pDC = GetDC(); // 获取设备上下文
		pDC->SelectObject(&CYAN);
		pDC->SelectObject(RED_PEN);
        pDC->SetROP2(R2_COPYPEN); // 设置为覆盖绘图方式
		CPoint center; // 圆心
		CRect rect(m_ptStart,point);
		
		switch(m_nDrawType)
		{
		case RECTOBST:
              //	CRectTracker( rect, CRectTracker::dottedLine );
	         pDC->Rectangle(rect);
			 ReleaseDC(pDC); // 释放设备上下文
			break;
		case CIRCLEOBST:
			center.x=(int(m_ptStart.x+point.x))/2;
            center.y=(int(m_ptStart.y+point.y))/2;
	    	m_Radius=(int)sqrt(double(point.y-m_ptStart.y)*(point.y-m_ptStart.y)+(point.x-m_ptStart.x)*(point.x-m_ptStart.x))/2;
	        pDC->Ellipse(center.x-m_Radius, center.y-m_Radius, center.x+m_Radius, center.y+m_Radius);
			old_center=center;
             ReleaseDC(pDC); // 释放设备上下文
			break;
		}
		m_bDrawing = FALSE;
		m_bErase = FALSE;

	}
		
	CView::OnLButtonUp(nFlags, point);
}

void CMineView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMineDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);


	//选择光标
	if (m_nDrawType==ARROW)
		SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));

	else if (m_nDrawType==CREATEROBOT)
		SetCursor(AfxGetApp()->LoadCursor(IDC_ROBOT));
	else if (m_nDrawType==RECTOBST||m_nDrawType==CIRCLEOBST)	
	    SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));

	if(m_bDrawing==TRUE) 
	{
        CDC* pDC = GetDC(); // 获取设备上下文
		pDC->SelectStockObject(NULL_BRUSH);
		pDC->SelectObject(BLACK);
		pDC->SetROP2(R2_XORPEN);//根据nDrawMode设置的方式重新设定绘图的方式		 
		CPoint center;
		CRect rect(m_ptStart,point);
		
    	switch(m_nDrawType)
		{
		   case RECTOBST:
			   if (m_bErase)
			   {            
                 pDC->Rectangle(old_rect);
			   }
               else // 需要擦除为假
			    m_bErase = TRUE;//设需要擦除为真
			    pDC->Rectangle(rect);
		    	old_rect=rect;
		     	ReleaseDC(pDC); // 释放设备上下文
			break;

		  case CIRCLEOBST:
		     if (m_bErase)
				{ // need to erase            
                  pDC->Ellipse(old_center.x-m_Radius, old_center.y-m_Radius,old_center.x+m_Radius, old_center.y+m_Radius);
				}
              else // 需要擦除为假
                 m_bErase = TRUE; // 设需要擦除为真
		         center.x = (int(m_ptStart.x+point.x))/2;
                 center.y = (int(m_ptStart.y+point.y))/2;
                 m_Radius = (int)sqrt((double)(point.y-m_ptStart.y)*(point.y-m_ptStart.y)+
                     (point.x-m_ptStart.x)*(point.x-m_ptStart.x))/2;
	       	
			     pDC->Ellipse(center.x-m_Radius, center.y-m_Radius, center.x+m_Radius, center.y+m_Radius);
			     old_center=center;
			     ReleaseDC(pDC); // 释放设备上下文
			  break;
		
	}
         	
	
	CView::OnMouseMove(nFlags, point);
	}	
}
void CMineView::OnObstCircle() 
{
	// TODO: Add your command handler code here
	m_nDrawType= CIRCLEOBST;
}

void CMineView::OnObstRectangle() 
{
	// TODO: Add your command handler code here
	m_nDrawType= RECTOBST;
	
	
}

void CMineView::OnCreaterobot() 
{
	// TODO: Add your command handler code here
	m_CreateRobot = TRUE;
	m_nDrawType = CREATEROBOT;
	
}

void CMineView::OnArrow() 
{
	// TODO: Add your command handler code here
	m_nDrawType= ARROW;

	
}

⌨️ 快捷键说明

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