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

📄 demo1view.cpp

📁 Viaual C++实战演练一书的源代码,对于C++程序员有实际的借鉴意义.
💻 CPP
字号:
// Demo1View.cpp : implementation of the CDemo1View class
//

#include "stdafx.h"
#include "Demo1.h"

#include "Demo1Doc.h"
#include "Demo1View.h"

#include "Draw.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDemo1View

IMPLEMENT_DYNCREATE(CDemo1View, CView)

BEGIN_MESSAGE_MAP(CDemo1View, CScrollView)
	//{{AFX_MSG_MAP(CDemo1View)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_UPDATE_COMMAND_UI(IDT_ARROW, OnUpdateArrow)
	ON_UPDATE_COMMAND_UI(IDT_LINE, OnUpdateLine)
	ON_UPDATE_COMMAND_UI(IDT_RECTANGLE, OnUpdateRectangle)
	ON_UPDATE_COMMAND_UI(IDT_ELLIPSE, OnUpdateEllipse)
	ON_UPDATE_COMMAND_UI(IDT_CIRCLE, OnUpdateCircle)
	ON_UPDATE_COMMAND_UI(IDT_PLINE, OnUpdatePline)
	ON_UPDATE_COMMAND_UI(IDT_FONT, OnUpdateFont)
	ON_COMMAND(IDT_ARROW, OnArrow)
	ON_COMMAND(IDT_LINE, OnLine)
	ON_COMMAND(IDT_RECTANGLE, OnRectangle)
	ON_COMMAND(IDT_ELLIPSE, OnEllipse)
	ON_COMMAND(IDT_CIRCLE, OnCircle)
	ON_COMMAND(IDT_PLINE, OnPline)
	ON_COMMAND(IDT_FONT, OnFont)
	ON_COMMAND(IDT_RED, OnRed)
	ON_COMMAND(IDT_GREEN, OnGreen)
	ON_COMMAND(IDT_BLUE, OnBlue)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_UPDATE_COMMAND_UI(IDT_DELETE, OnUpdateDelete)
	ON_COMMAND(IDT_DELETE, OnDelete)
	ON_UPDATE_COMMAND_UI(IDT_RED, OnUpdateRed)
	ON_UPDATE_COMMAND_UI(IDT_GREEN, OnUpdateGreen)
	ON_UPDATE_COMMAND_UI(IDT_BLUE, OnUpdateBlue)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDemo1View construction/destruction

CDemo1View::CDemo1View()
{
	// TODO: add construction code here
	m_bDrawing=false;
	m_nMouseMode = IDT_ARROW;
	m_usrCurrentObject = NULL;
	m_nCurrentColor = UD_RED;
}

CDemo1View::~CDemo1View()
{
}

BOOL CDemo1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//加载自定义光标资源
	m_hcurCross = (HCURSOR)::LoadImage(cs.hInstance,
		MAKEINTRESOURCE(IDC_USERDRAW), IMAGE_CURSOR,
		32, 32, LR_DEFAULTCOLOR);

	//加载光标的类方法(与上述方法功能类似)
	CWinApp* pApp = ::AfxGetApp();//获得当前CWinApp对象指针

	m_hcurMoving = pApp->LoadCursor(IDC_USERMOVING);
	m_hcurArrow = pApp->LoadCursor(IDC_USERARROW);

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDemo1View drawing

void CDemo1View::OnDraw(CDC* pDC)
{
	CDemo1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//以下重绘标绘图形
	pDoc->DrawObjects(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CDemo1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDemo1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDemo1View message handlers

void CDemo1View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC = GetDC();//获得DC
	OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
	pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
	CDemo1Doc* pDoc = GetDocument();

	CRect client;
	GetClientRect(&client);//获得客户区范围
	ClientToScreen(&client);//转化为屏幕坐标
	::ClipCursor(&client);//锁定鼠标移动范围

	if(m_usrCurrentObject)
	{//清除图元热点标记
		m_usrCurrentObject->Selected(pDC, false);
	}

	switch(m_nMouseMode)
	{
	case IDT_ARROW:
		if(m_usrCurrentObject && (m_nMoveMode = m_usrCurrentObject->SelectAt(point.x, point.y)))
		{//鼠标指针指向以选择的对象,不需再遍历全部对象
			m_usrCurrentObject->Selected(pDC, true);
			m_bDrawing = true;
			m_nCurrentColor = m_usrCurrentObject->GetPenColor();
			SetCursor(m_hcurMoving);//设置光标显示
			::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurMoving);
			break;//跳出switch结构
		}
		int i;
		for(i = pDoc->m_aObjects.GetSize()-1; i >= 0; i--)
		{
			m_usrCurrentObject = pDoc->m_aObjects[i];
			m_nMoveMode = m_usrCurrentObject->SelectAt(point.x, point.y);
			if(m_nMoveMode)//判断是否选择当前图元
			{//选择某图元
				m_usrCurrentObject->Selected(pDC, true);
				m_bDrawing = true;
				m_nCurrentColor = m_usrCurrentObject->GetPenColor();
				SetCursor(m_hcurMoving);//设置光标显示
				::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurMoving);
				break;
			}
			else
			{
				m_usrCurrentObject->Selected(pDC, false);
			}
			m_usrCurrentObject = NULL;
			m_bDrawing = false;
		}
		for(i--;i>=0; i--)
		{//清除其他图元的选择
			pDoc->m_aObjects[i]->Selected(pDC, false);
		}
		break;
	case IDT_LINE:
		m_bDrawing = true;
		m_usrCurrentObject = new CDrawLine(m_nCurrentColor);
		m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		break;
	case IDT_RECTANGLE:
		m_bDrawing = true;
		m_usrCurrentObject = new CDrawRect(m_nCurrentColor);
		m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		break;
	case IDT_ELLIPSE:
		m_bDrawing = true;
		m_usrCurrentObject = new CDrawEllipse(m_nCurrentColor);
		m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		break;
	case IDT_CIRCLE:
		m_bDrawing = true;
		m_usrCurrentObject = new CDrawCircle(m_nCurrentColor);
		m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		break;
	case IDT_PLINE:
		if(m_usrCurrentObject)
		{
			m_nMoveMode = m_usrCurrentObject->AddPoint(point.x, point.y);
			if(!m_nMoveMode)
			{//输入端点超过20个
				OnRButtonDown(nFlags, point);
			}
		}
		else
		{
			m_bDrawing = true;
			m_usrCurrentObject = new CDrawPLine(m_nCurrentColor);
			m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		}
		break;
	case IDT_FONT:
		m_bDrawing = true;
		m_usrCurrentObject = new CDrawFont(m_nCurrentColor);
		m_nMoveMode = m_usrCurrentObject->NewPoint(point.x, point.y);
		m_usrCurrentObject->Draw(pDC);
		break;
	}

	ReleaseDC(pDC);//释放DC资源

	CView::OnLButtonDown(nFlags, point);
}

void CDemo1View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC = GetDC();
	OnPrepareDC(pDC);
	pDC->DPtoLP(&point);

	if(m_bDrawing && m_usrCurrentObject)
	{
		m_usrCurrentObject->MoveAt(pDC, m_nMoveMode, point.x, point.y);	
	}
	
	ReleaseDC(pDC);//释放DC资源
	
	CView::OnMouseMove(nFlags, point);
}

void CDemo1View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC = GetDC();//获得DC
	OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
	pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
	CDemo1Doc* pDoc = GetDocument();

	switch(m_nMouseMode)
	{
	case IDT_ARROW:
		::ClipCursor(NULL);//释放鼠标锁定
		m_bDrawing = false;//停止绘图
		SetCursor(m_hcurArrow);//恢复光标显示
		::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurArrow);
		Invalidate();//刷新视图
		break;
	case IDT_LINE:
	case IDT_RECTANGLE:
	case IDT_ELLIPSE:
	case IDT_CIRCLE:
	case IDT_FONT:
		::ClipCursor(NULL);//释放鼠标锁定
		pDoc->m_aObjects.Add(m_usrCurrentObject);
		m_bDrawing = false;
		m_usrCurrentObject = NULL;
		Invalidate();//刷新视图
		break;
	}

	ReleaseDC(pDC);//释放DC资源

	CScrollView::OnLButtonUp(nFlags, point);
}

void CDemo1View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDemo1Doc* pDoc = GetDocument();
	CDC* pDC = GetDC();//获得DC

	if(m_nMouseMode == IDT_PLINE && m_usrCurrentObject)
	{
		::ClipCursor(NULL);//释放鼠标锁定
		if(((CDrawPLine*)m_usrCurrentObject)->m_nNumber >1)
		{
			pDoc->m_aObjects.Add(m_usrCurrentObject);
		}
		else
		{
			delete m_usrCurrentObject;
		}
		m_bDrawing = false;
		m_usrCurrentObject = NULL;
		Invalidate();//刷新视图
	}

	ReleaseDC(pDC);//释放DC资源

	CScrollView::OnRButtonDown(nFlags, point);
}

void CDemo1View::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	CSize size(1280, 1024);//设置绘图区范围为:1280*1024
	SetScrollSizes(MM_TEXT, size);
}

void CDemo1View::OnUpdateArrow(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_ARROW);
}

void CDemo1View::OnUpdateLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_LINE);
}

void CDemo1View::OnUpdateRectangle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_RECTANGLE);
}

void CDemo1View::OnUpdateEllipse(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_ELLIPSE);
}

void CDemo1View::OnUpdateCircle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_CIRCLE);
}

void CDemo1View::OnUpdatePline(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_PLINE);
}

void CDemo1View::OnUpdateFont(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nMouseMode == IDT_FONT);
}

void CDemo1View::OnArrow() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_ARROW;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurArrow);//修改缺省光标
}

void CDemo1View::OnLine() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_LINE;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标
}

void CDemo1View::OnRectangle() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_RECTANGLE;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标
}

void CDemo1View::OnEllipse() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_ELLIPSE;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标
}

void CDemo1View::OnCircle() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_CIRCLE;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标
}

void CDemo1View::OnPline() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_PLINE;
	if(m_usrCurrentObject)
	{
		m_usrCurrentObject->m_bSelected = false;
		m_usrCurrentObject = NULL;
	}
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标

	Invalidate();//更新试图
}

void CDemo1View::OnFont() 
{
	// TODO: Add your command handler code here
	m_nMouseMode = IDT_FONT;
	::SetClassLong(GetSafeHwnd(), GCL_HCURSOR, (LONG)m_hcurCross);//修改缺省光标
}

void CDemo1View::OnUpdateRed(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nCurrentColor == UD_RED);
}

void CDemo1View::OnUpdateGreen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nCurrentColor == UD_GREEN);
}

void CDemo1View::OnUpdateBlue(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nCurrentColor == UD_BLUE);
}

void CDemo1View::OnRed() 
{
	// TODO: Add your command handler code here
	m_nCurrentColor = UD_RED;
	if(m_usrCurrentObject && m_usrCurrentObject->m_bSelected)
	{//更新当前选择图元的显示
		m_usrCurrentObject->SetPenColor(m_nCurrentColor);
		Invalidate();
	}
}

void CDemo1View::OnGreen() 
{
	// TODO: Add your command handler code here
	m_nCurrentColor = UD_GREEN;
	if(m_usrCurrentObject && m_usrCurrentObject->m_bSelected)
	{//更新当前选择图元的显示
		m_usrCurrentObject->SetPenColor(m_nCurrentColor);
		Invalidate();
	}
}

void CDemo1View::OnBlue() 
{
	// TODO: Add your command handler code here
	m_nCurrentColor = UD_BLUE;
	if(m_usrCurrentObject && m_usrCurrentObject->m_bSelected)
	{//更新当前选择图元的显示
		m_usrCurrentObject->SetPenColor(m_nCurrentColor);
		Invalidate();
	}
}


void CDemo1View::OnFileNew() 
{
	// TODO: Add your command handler code here
	int i;
	CDemo1Doc* pDoc = GetDocument();

	for(i=pDoc->m_aObjects.GetSize()-1; i>=0; i--)
	{
		delete pDoc->m_aObjects[i];
	}
	m_usrCurrentObject = NULL;
	pDoc->m_aObjects.RemoveAll();//删除图元
	Invalidate();//刷新视图
	pDoc->OnNewDocument();
}

void CDemo1View::OnUpdateDelete(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_nMouseMode == IDT_ARROW && m_usrCurrentObject != NULL);
	//当选中图元时,才可使用删除功能
}

void CDemo1View::OnDelete() 
{
	// TODO: Add your command handler code here
	CDemo1Doc* pDoc = GetDocument();
	int i;

	if(m_usrCurrentObject)
	{//当前选中了图元
		for(i=pDoc->m_aObjects.GetSize()-1; i>=0; i--)
		{//遍历图元数据列表
			if(pDoc->m_aObjects[i] == m_usrCurrentObject)
			{//找到需删除的图元
				pDoc->m_aObjects.RemoveAt(i);
				delete m_usrCurrentObject;
				m_usrCurrentObject = NULL;
				break;
			}
		}
		Invalidate();
	}
}

⌨️ 快捷键说明

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