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

📄 exdrawview.cpp

📁 c++语言界面设计
💻 CPP
字号:
// exdrawView.cpp : implementation of the CExdrawView class
//

#include "stdafx.h"
#include "exdraw.h"
#include "math.h"
#include "exdrawDoc.h"
#include "exdrawView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExdrawView

IMPLEMENT_DYNCREATE(CExdrawView, CView)

BEGIN_MESSAGE_MAP(CExdrawView, CView)
	//{{AFX_MSG_MAP(CExdrawView)
	ON_COMMAND(IDR_DRAW_LINE, OnDrawLine)
	ON_COMMAND(IDR_DRAW_RECT, OnDrawRect)
	ON_COMMAND(IDR_DRAW_CIRCLE, OnDrawCircle)
	ON_COMMAND(IDR_DRAW_ELLIPSE, OnDrawEllipse)
	ON_COMMAND(IDR_DRAW_CURVE, OnDrawCurve)
	ON_COMMAND(IDR_COLOR_BLACK, OnColorBlack)
	ON_COMMAND(IDR_COLOR_BLUE, OnColorBlue)
	ON_COMMAND(IDR_COLOR_GREEN, OnColorGreen)
	ON_COMMAND(IDR_COLOR_RED, OnColorRed)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_UPDATE_COMMAND_UI(IDR_COLOR_GREEN, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(IDR_COLOR_RED, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(IDR_COLOR_BLUE, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(IDR_COLOR_BLACK, OnUpdateColor)
	//}}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()

////////////////////外部函数声明/////////////////////////////////////////////////////////
// CExdrawView construction/destruction
extern COLORREF Cfromn(int ncolor);
extern RECT Rfromp(CPoint oldpoint,CPoint newpoint);
extern RECT radius(CPoint oldpoint,CPoint newpoint);
extern RECT Efromp(CPoint oldpoint,CPoint newpoint);
COLORREF Cfromn(int ncolor)
{
	switch(ncolor)
	{
	case 0:
	default:
		return RGB(255,0,0);
		break;
	case 1:
		return RGB(0,0,0);
		break;
	case 2:
		return RGB(0,0,255);
		break;
    case 3:
		return RGB(0,255,0);
		break;
	}
}
///////////////////////////////////////

RECT Rfromp(CPoint oldpoint,CPoint newpoint)
{
	RECT rect1;
	if(oldpoint.x<newpoint.x)
	{
		rect1.left = oldpoint.x;
		rect1.right = newpoint.x;
	}
	else
	{
        rect1.left = newpoint.x;
		rect1.right = oldpoint.x;
	}
	if(oldpoint.y > newpoint.y)
	{
		rect1.bottom = oldpoint.y;
		rect1.top = newpoint.y;
	}
	else
	{
		rect1.bottom = newpoint.y;
		rect1.top = oldpoint.y;
	}
	return rect1;
}

///////////////////////////

RECT radius(CPoint oldpoint,CPoint newpoint)
{
	RECT rectcircle;
	int nradius = (int)sqrt(pow(oldpoint.x - newpoint.x,2)
		                 +pow(oldpoint.y - newpoint.y,2));
    rectcircle.bottom = oldpoint.y+nradius;
	rectcircle.top = oldpoint.y-nradius;
	rectcircle.left = oldpoint.x-nradius;
	rectcircle.right = oldpoint.x+nradius;
	return rectcircle;
}
//////////////////////////////////////////////

RECT Efromp(CPoint oldpoint,CPoint newpoint)
{
	RECT erect;
	if (oldpoint.x<newpoint.x)
	{
		erect.left = 2* oldpoint.x - newpoint.x;
		erect.right = newpoint.x;
	}
	else
	{
        erect.left = 2*newpoint.x - oldpoint.x ;
		erect.right = oldpoint.x;
	}

	if(oldpoint.y < newpoint.y)
	{

		erect.bottom = newpoint.y;
	}
	else
	{
		erect.bottom = oldpoint.y;
		erect.top = 2*newpoint.y- oldpoint.y;
	}
	return erect;
}
CExdrawView::CExdrawView()
{
	// TODO: add construction code here
    m_nchoice = 0;
	m_ncolor = 1;
}

CExdrawView::~CExdrawView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExdrawView drawing

void CExdrawView::OnDraw(CDC* pDC)
{
	CExdrawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CExdrawView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExdrawView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExdrawView message handlers

void CExdrawView::OnDrawLine() 
{
	// TODO: Add your command handler code here
	//nflag=0;
	m_nchoice = 0;
}

void CExdrawView::OnDrawRect() 
{
	// TODO: Add your command handler code here
	//nflag=0;
	m_nchoice = 1;
}

void CExdrawView::OnDrawCircle() 
{
	// TODO: Add your command handler code here
	//nflag=0;
	m_nchoice = 2;
}

void CExdrawView::OnDrawEllipse() 
{
	// TODO: Add your command handler code here
	//nflag=0;
	m_nchoice = 3;
}

void CExdrawView::OnDrawCurve() 
{
	// TODO: Add your command handler code here
	//nflag=0;
	m_nchoice = 4;
}

void CExdrawView::OnColorBlack() 
{
	// TODO: Add your command handler code here
	m_ncolor = 1;
}

void CExdrawView::OnColorBlue() 
{
	// TODO: Add your command handler code here
	m_ncolor = 2;	
}

void CExdrawView::OnColorGreen() 
{
	// TODO: Add your command handler code here
	m_ncolor = 3;
}

void CExdrawView::OnColorRed() 
{
	// TODO: Add your command handler code here
	m_ncolor = 0;
}

void CExdrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	switch (m_nchoice)
	{
	case 0:
		if(nflag == 0)
		{
			oldpoint = point;
			nflag = 1;
			dc.SetPixel(oldpoint,RGB(255,0,0));
		}
		else
		{
			CPen linepen(PS_SOLID,1,Cfromn(m_ncolor));
			dc.SelectObject(&linepen);
			dc.MoveTo(oldpoint);
			dc.LineTo(point);
			oldpoint = point;
			dc.SetPixel(oldpoint,RGB(255,0,0));
		//nflag=0;
		}
		break;
		//////添加 case////////////////////////////////////////////////////////
	case 1:
		oldpoint = point;
		nflag = 1;
		break;
	case 2:
		oldpoint = point;
		dc.SetPixel(oldpoint,RGB(255,0,0));
		nflag = 1;
		break;
	case 3:
		oldpoint = point;
		dc.SetPixel(oldpoint, RGB(255,0,0));
		nflag = 1;
		break;
	case 4:
        oldpoint = point;
		dc.SetPixel(oldpoint, RGB(255,0,0));
		nflag = 1;
		break;
	}
////////////////////////////////////////////////////////////////////////////////////////
	CView::OnLButtonDown(nFlags, point);
}

void CExdrawView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	nflag = 0;    //结束一次连续的绘图动作
	CView::OnRButtonDown(nFlags, point);
}

////////////////////////////////////////////////////////////////////////////
void CExdrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	CPen rectpen(PS_SOLID,1,Cfromn(m_ncolor));
	dc.SelectObject(&rectpen);
	switch(m_nchoice)
	{
	case 0:
		break;
	case 1:
		RECT rect1;
		rect1 = Rfromp(oldpoint,newpoint);
		dc.Rectangle(&rect1);
		break;
		///////添加 //////////////////////
	case 2:
		RECT rectcircle = radius(oldpoint,newpoint);
		dc.Ellipse(&rectcircle);
		break;
	case 3:
		{
			RECT erect = Efromp(oldpoint,newpoint);
			dc.Ellipse(&erect);
			break;
		}
	}
	CView::OnLButtonUp(nFlags, point);
}

void CExdrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	newpoint = point;
	CClientDC dc(this);
	switch(m_nchoice)
	{
    case 0:
         break;
	case 1:
		if(nflag != 0)
			if((nFlags&MK_LBUTTON))
			{
				RECT rectdraw;
				CPen rectpen(PS_DOT,1,Cfromn(m_ncolor));
				rectdraw = Rfromp(oldpoint,point);
                InvalidateRect(&rectdraw,TRUE);  //将指定区域设为无效
				dc.SelectObject(&rectpen);
				dc.Rectangle(&rectdraw);
			}
	      break;
		  ///////添加 case////////////////////////////////////
    case 2:
		if(nflag != 0)
            if((nFlags&MK_LBUTTON))
			{
				CPen rectpen(PS_DOT,1,Cfromn(m_ncolor));
				dc.SelectObject(&rectpen);
				dc.MoveTo(oldpoint);
				dc.LineTo(point);
				RECT rectcircle = radius(oldpoint,newpoint);
				//InvalidateRgn
					InvalidateRect(&rectcircle,TRUE);  //将指定区域设为无效
				dc.Ellipse(&rectcircle);
			}
			break;
    case 3:
		if (nflag != 0)
			if ((nFlags & MK_LBUTTON))
				{
                    CPen epen(PS_DOT,1,Cfromn(m_ncolor));
					dc.SelectObject(&epen);
					RECT rectellipse = Efromp(oldpoint,point);
					//InvalidateRgn
					InvalidateRect(&rectellipse,TRUE);  //将指定区域设为无效
					dc.Ellipse(&rectellipse);
				}
		break;
	case 4:
		if (nflag != 0)
		{
			CPen rectpen(PS_DOT,1,Cfromn(m_ncolor));
			dc.SelectObject(&rectpen);
			dc.MoveTo (oldpoint);
			dc.LineTo (point);
		    oldpoint = point;
		}
		break;
	}
	CView::OnMouseMove(nFlags, point);
}

void CExdrawView::OnUpdateColor(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
    pCmdUI->SetCheck (m_ncolor==pCmdUI->m_nID-IDR_COLOR_RED?1:0);
	
}

⌨️ 快捷键说明

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