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

📄 drawtwoview.cpp

📁 一种学习MFC后基于vc编程实现画直线、矩形与圆形等基本图形。简单实现了一种画图功能。
💻 CPP
字号:
// DrawTwoView.cpp : implementation of the CDrawTwoView class
//

#include "stdafx.h"
#include "DrawTwo.h"

#include "DrawTwoDoc.h"
#include "DrawTwoView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView

IMPLEMENT_DYNCREATE(CDrawTwoView, CView)

BEGIN_MESSAGE_MAP(CDrawTwoView, CView)
	//{{AFX_MSG_MAP(CDrawTwoView)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}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)
	ON_COMMAND_RANGE(ID_DRAWING_LINE,ID_DRAWING_ELLIPSE,OnSelectDrawingType)
    ON_UPDATE_COMMAND_UI_RANGE(ID_DRAWING_LINE,ID_DRAWING_ELLIPSE,OnUpdateSelectDrawingType)
	
 END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView construction/destruction

CDrawTwoView::CDrawTwoView()
{
	// TODO: add construction code here

}

CDrawTwoView::~CDrawTwoView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView printing

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

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

void CDrawTwoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}
void CDrawTwoView::OnSelectDrawingType(unsigned int nID)
{
	ASSERT(nID>=ID_DRAWING_LINE && nID<=ID_DRAWING_ELLIPSE);
	m_nDrawingType=nID-ID_DRAWING_LINE;
}
void CDrawTwoView::OnUpdateSelectDrawingType(CCmdUI* pCmdUI)
{
	int nID=pCmdUI->m_nID-ID_DRAWING_LINE;
	if(nID==m_nDrawingType)
	{
		pCmdUI->SetCheck(true);
	}
	else{
		pCmdUI->SetCheck(false);
	}
}
/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawTwoView message handlers

void CDrawTwoView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_ptStart=point;
	m_ptOld=point;
	if(m_bIsDrawing)
		return;
	SetCapture();     // capture mouse
	m_bIsDrawing=true;
	RECT rect;
	GetClientRect(&rect);  //captrue the position of client
	ClientToScreen(&rect);  //transpfrom position of client to screen position
	//CView::OnLButtonDown(nFlags, point);
}

void CDrawTwoView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_bIsDrawing)
	{
		CClientDC dc(this);   //create context object for client area
		dc.SetROP2(R2_NOT);   //set model of drawing
		CRect rectOld(m_ptStart,m_ptOld);
		CRect rectNew(m_ptStart,point);
		 
		switch(m_nDrawingType){
		case 0:
			dc.MoveTo(m_ptStart);
			dc.LineTo(m_ptOld);
			dc.MoveTo(m_ptStart);
			dc.LineTo(point);
			break;
        case 1:
			dc.Rectangle(rectOld);
			dc.Rectangle(rectNew);
			break;
        case 2:
			dc.Ellipse(rectOld);
			dc.Ellipse(rectNew);
		default:
			break;
		}
	}
	m_ptOld=point;
   // CView::OnMouseMove(nFlags, point);
}

void CDrawTwoView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_bIsDrawing)
	{
		m_bIsDrawing=FALSE;
		ReleaseCapture();
	}
   // CView::OnLButtonUp(nFlags, point);
}

⌨️ 快捷键说明

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