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

📄 exam2view.cpp

📁 Visual C++_ 600 编程学习捷径
💻 CPP
字号:
// Exam2View.cpp : implementation of the CExam2View class
//

#include "stdafx.h"
#include "Exam2.h"

#include "Exam2Doc.h"
#include "Exam2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExam2View

IMPLEMENT_DYNCREATE(CExam2View, CView)

BEGIN_MESSAGE_MAP(CExam2View, CView)
	//{{AFX_MSG_MAP(CExam2View)
	ON_COMMAND(ID_NOMODE_DIALOG, OnNomodeDialog)
	ON_UPDATE_COMMAND_UI(ID_NOMODE_DIALOG, OnUpdateNomodeDialog)
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_DELETE_POINT, OnDeletePoint)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CExam2View construction/destruction

CExam2View::CExam2View()
{
	m_pPointInfo = NULL;
}

CExam2View::~CExam2View()
{
	if(m_pPointInfo != NULL)
	{
		//销毁窗口
		m_pPointInfo->DestroyWindow();

		//删除对象
		delete m_pPointInfo;
	}
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExam2View drawing

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

	CPen pen, *pOldPen;
	
	//创建一个单线宽的红色画笔
	pen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
	pOldPen = pDC->SelectObject(&pen); //把画笔选进DC
	pDC->SetBkMode(TRANSPARENT);       //设置字体背景透明
	pDC->SetTextColor(RGB(255, 0, 0)); //设置字体颜色为红色

	for(int i = 0; i < m_arArray.GetSize(); i++)
	{
		//从数组中取一个点
		CPoint point = m_arArray.GetAt(i);

		//在选中的点上画一个十字标记
		pDC->MoveTo(point.x - 5, point.y);
		pDC->LineTo(point.x + 5, point.y);
		pDC->MoveTo(point.x, point.y - 5);
		pDC->LineTo(point.x, point.y + 5);
		
		//输出点在数组中的序号
		CString szText;
		szText.Format("%d", i + 1);
		pDC->TextOut(point.x + 3, point.y + 3, szText);
	}

	pDC->SelectObject(pOldPen);        //恢复DC画笔
}

/////////////////////////////////////////////////////////////////////////////
// CExam2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExam2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExam2View message handlers

void CExam2View::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	m_pPointInfo = new CPointInfo(this); //创建CPointInfo对象
	if(m_pPointInfo != NULL)             //创建对象成功
	{
		//创建对话框
		m_pPointInfo->Create(IDD_MOUSE_POINT_DIALOG, this);

		//显示对话框
		m_pPointInfo->ShowWindow(SW_SHOW);
	}
	else//如果创建CPointInfo对象失败,则显示一个消息框
		MessageBox("创建CPointInfo对象失败!", "错误", MB_OK);
}

void CExam2View::OnNomodeDialog() 
{
	if(m_pPointInfo != NULL) //窗口对象是否存在
	{
		//窗口对象可见,则隐藏
		if(m_pPointInfo->IsWindowVisible())
			m_pPointInfo->ShowWindow(SW_HIDE);
		else  //窗口对象隐藏,则显示
			m_pPointInfo->ShowWindow(SW_SHOW);
	}
}

void CExam2View::OnUpdateNomodeDialog(CCmdUI* pCmdUI) 
{
	//当对话框窗口可见时,在菜单项上作上一个标记
	pCmdUI->SetCheck(m_pPointInfo != NULL && 
					 m_pPointInfo->IsWindowVisible());
}

void CExam2View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	//把点添加到m_arArray数组中保存下来
	m_arArray.Add(point);
	
	CClientDC dc(this); //声明一个DC对象
	CPen pen, *pOldPen;
	
	//创建一个单线宽的红色画笔
	pen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
	pOldPen = dc.SelectObject(&pen); //把画笔选进DC

	//在选中的点上画一个十字标记
	dc.MoveTo(point.x - 5, point.y);
	dc.LineTo(point.x + 5, point.y);
	dc.MoveTo(point.x, point.y - 5);
	dc.LineTo(point.x, point.y + 5);
	
	dc.SelectObject(pOldPen);        //恢复DC画笔

	dc.SetBkMode(TRANSPARENT);       //设置字体背景透明
	dc.SetTextColor(RGB(255, 0, 0)); //设置字体颜色为红色

	//输出点在数组中的序号
	CString szText;
	szText.Format("%d", m_arArray.GetUpperBound() + 1);
	dc.TextOut(point.x + 3, point.y + 3, szText);

	//向非模式对话框发送新加点的坐标
	if(m_pPointInfo != NULL)
	{
		m_pPointInfo->SendMessage(WM_ADD_POINT, 0, 
						MAKELPARAM((WORD)point.x, (WORD)point.y));
	}

	CView::OnLButtonUp(nFlags, point);
}

void CExam2View::OnMouseMove(UINT nFlags, CPoint point) 
{
	//向非模式对话框发送鼠标位置改变消息
	if(m_pPointInfo != NULL)
	{
		m_pPointInfo->SendMessage(WM_MOUSE_CHANGED, 0, 
				MAKELPARAM((WORD)point.x, (WORD)point.y));
	}
		
	CView::OnMouseMove(nFlags, point);
}

void CExam2View::OnDeletePoint(WPARAM wParam, LPARAM lParam)
{
	//得到删除点的索引
	int index = (int)lParam;

	//检查索引是否合法
	if(index >= 0 && index < m_arArray.GetSize())
	{
		//丛数组中删除该点
		m_arArray.RemoveAt(index);

		//刷新视图显示
		Invalidate();
	}
}

⌨️ 快捷键说明

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