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

📄 paintview.cpp

📁 这是一个小型画板系统
💻 CPP
字号:
// paintView.cpp : implementation of the CPaintView class
//

#include "stdafx.h"
#include "paint.h"

#include "paintDoc.h"
#include "paintView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPaintView

IMPLEMENT_DYNCREATE(CPaintView, CView)

BEGIN_MESSAGE_MAP(CPaintView, CView)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_TYPE,OnUpdateIndicatorType)
	//{{AFX_MSG_MAP(CPaintView)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_COLOR, OnColor)
	ON_COMMAND(ID_LINE, OnLine)
	ON_COMMAND(ID_RECT, OnRect)
	ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
	ON_UPDATE_COMMAND_UI(ID_RECT, OnUpdateRect)
	ON_COMMAND(ID_COLOR_BLACK, OnColorBlack)
	ON_COMMAND(ID_COLOR_BLUE, OnColorBlue)
	ON_COMMAND(ID_COLOR_GREEN, OnColorGreen)
	ON_COMMAND(ID_COLOR_RED, OnColorRed)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, OnUpdateColorBlue)
	ON_UPDATE_COMMAND_UI(ID_COLOR_BLACK, OnUpdateColorBlack)
	ON_UPDATE_COMMAND_UI(ID_COLOR_GREEN, OnUpdateColorGreen)
	ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CPaintView construction/destruction

CPaintView::CPaintView()
{
	// TODO: add construction code here
	m_Type = 1;

}

CPaintView::~CPaintView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPaintView drawing

void CPaintView::OnDraw(CDC* pDC)
{

	CPaintDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	int count;
	CObArray *pObArray;
	
	pObArray =&pDoc->m_obLineArray;
	count = pObArray->GetSize();
	if(count)
	{
		for(int i=0;i<count;i++)
		{
			CLine *ptr=(CLine *)pObArray->GetAt(i);
			ptr->Draw(pDC);
		}
		
	}

	pObArray = &pDoc->m_obRectArray;
	count = pObArray->GetSize();
	if(count)
	{
		for(int i=0;i<count;i++)
		{
			CRectangle *ptr=(CRectangle *)pObArray->GetAt(i);
			ptr->Draw(pDC);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CPaintView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CPaintView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CPaintView message handlers

void CPaintView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	SetCapture();
	m_Start = point;
	m_End = point;
	
	CView::OnLButtonDown(nFlags, point);
}

void CPaintView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetCapture() == this)
	{
		CLine *pLine = new CLine(m_Start,m_End,color);
		CRectangle *pRect = new CRectangle(m_Start,m_End,color);
		CClientDC dc(this);
		switch(m_Type) 
		{
		case 1 :
			pLine->Draw(&dc);								//画掉旧线 
			
			pLine = new CLine(m_Start,point,color);
			pLine->Draw(&dc);								//画新线 
			m_End = point;			
			break;
		case 2 :
			pRect->Draw(&dc);
			pRect = new CRectangle(m_Start,point,color);
			pRect->Draw(&dc);
			m_End = point;
			break;
		}


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

void CPaintView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(GetCapture() == this)
	{	ReleaseCapture();
		m_End = point;
		CLine *pLine = new CLine(m_Start,m_End,color);
		CRectangle *pRect = new CRectangle(m_Start,m_End,color);
		switch(m_Type) 
		{
		case 1 :
			GetDocument()->AddLine(pLine);
			break;
		case 2 :
			GetDocument()->AddRect(pRect);
			break;
		}

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



void CPaintView::OnColor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	if(dlg.DoModal()==IDOK)
	{
		color = dlg.GetColor();
	}
	
}

void CPaintView::OnLine() 
{
	// TODO: Add your command handler code here
	m_Type = 1;
}

void CPaintView::OnRect() 
{
	// TODO: Add your command handler code here
	m_Type = 2;
}

void CPaintView::OnUpdateLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Type == 1 ? 1:0);
}

void CPaintView::OnUpdateRect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Type == 2 ? 1:0);
	
}

void CPaintView::OnColorBlack() 
{
	// TODO: Add your command handler code here
	color = RGB(0,0,0);
	m_Color = 1;
}

void CPaintView::OnColorBlue() 
{
	// TODO: Add your command handler code here
	color = RGB(0,0,255);
	m_Color = 2;
}

void CPaintView::OnColorGreen() 
{
	// TODO: Add your command handler code here
	color = RGB(0,255,0);
	m_Color = 3;
}

void CPaintView::OnColorRed() 
{
	// TODO: Add your command handler code here
	color = RGB(255,0,0);
	m_Color = 4;
}

void CPaintView::OnUpdateColorBlue(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color == 2 ? 1:0);
}

void CPaintView::OnUpdateColorBlack(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color == 1 ? 1:0);
}

void CPaintView::OnUpdateColorGreen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color == 3 ? 1:0);
}

void CPaintView::OnUpdateColorRed(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_Color == 4 ? 1:0);
}

void CPaintView::OnUpdateIndicatorType(CCmdUI *pCmdUI)
{
	CString strType;
	switch(m_Type) {
	case 1 :
		strType = "Line";
		break;
	case 2 :
		strType = "Rectangle";
		break;
	}

	pCmdUI->Enable(TRUE);
	pCmdUI->SetText(strType);
}

⌨️ 快捷键说明

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