general_classview.cpp

来自「掌握Visual C++编程,挺基础的」· C++ 代码 · 共 230 行

CPP
230
字号
// General_ClassView.cpp : implementation of the CGeneral_ClassView class
//

#include "stdafx.h"
#include "General_Class.h"

#include "General_ClassDoc.h"
#include "General_ClassView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGeneral_ClassView

IMPLEMENT_DYNCREATE(CGeneral_ClassView, CView)

BEGIN_MESSAGE_MAP(CGeneral_ClassView, CView)
	//{{AFX_MSG_MAP(CGeneral_ClassView)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_SET_DEFAULT_RECT, OnSetDefaultRect)
	ON_COMMAND(ID_RECORD_BEGIN, OnRecordBegin)
	ON_UPDATE_COMMAND_UI(ID_RECORD_BEGIN, OnUpdateRecordBegin)
	ON_COMMAND(ID_RECORD_END, OnRecordEnd)
	ON_UPDATE_COMMAND_UI(ID_RECORD_END, OnUpdateRecordEnd)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeneral_ClassView construction/destruction

CGeneral_ClassView::CGeneral_ClassView()
{
	m_bRecord = FALSE;
	m_bDrag = FALSE;
	m_Rect.SetRectEmpty();
}

CGeneral_ClassView::~CGeneral_ClassView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGeneral_ClassView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CGeneral_ClassView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGeneral_ClassView message handlers

void CGeneral_ClassView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_bRecord == TRUE)
	{
		CDC* pDC = GetDC();
		CRect rect;
		CString strIndex,strXY;
		CTime t;

		rect.bottom = point.y + 2;
		rect.top = point.y -2;
		rect.left = point.x - 2;
		rect.right = point.x + 2;
		CBrush Brush(RGB(0,255,0));
		pDC->Rectangle(&rect);
		pDC->FillRect(&rect,&Brush);

		m_PointArray.Add(point);
		t = CTime::GetCurrentTime();
		m_TimeList.AddTail(t);
		strIndex.Format("%d", m_nIndex);
		m_nIndex++;
		strXY.Format("X:%d,Y:%d", point.x, point.y);
		m_Map[strIndex] = strXY;
	}
	else if( m_Rect.PtInRect(point) )	
	{
		m_bDrag = TRUE;
		m_PrevPoint = point;
		SetCapture();
	}
	CView::OnLButtonDown(nFlags, point);
}

void CGeneral_ClassView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(m_bDrag == TRUE)
	{
		CDC* pDC = GetDC();
		CRect newRect;
		CBrush Brush(pDC->GetBkColor());

		newRect.CopyRect(&m_Rect);
		newRect.InflateRect(1,1,1,1);
		pDC->FillRect(&newRect, &Brush);
		m_Rect += point - m_PrevPoint;
		pDC->Rectangle(&m_Rect);
		m_PrevPoint = point;
	}

	CView::OnMouseMove(nFlags, point);
}

void CGeneral_ClassView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bDrag = FALSE;
	ReleaseCapture();

	CView::OnLButtonUp(nFlags, point);
}

void CGeneral_ClassView::OnSetDefaultRect() 
{
	CRect ClientRect;
	CDC* pDC = GetDC();

	GetClientRect(&ClientRect);
	m_Rect.bottom = (int)ClientRect.bottom * 0.75;
	m_Rect.top = (int)ClientRect.bottom * 0.5;
	m_Rect.left = (int)ClientRect.right * 0.5;
	m_Rect.right = (int)ClientRect.right * 0.75;
	pDC->Rectangle(&m_Rect);
}

void CGeneral_ClassView::OnRecordBegin() 
{
	m_nIndex = 0;
	m_bRecord = TRUE;
	m_PointArray.RemoveAll();
	m_TimeList.RemoveAll();
	m_Map.RemoveAll();
}

void CGeneral_ClassView::OnUpdateRecordBegin(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bRecord == FALSE);
}

void CGeneral_ClassView::OnRecordEnd() 
{
	CDC* pDC = GetDC();
	int nIndex,nCount;
	CString strTime;
	POSITION pos;
	CTime *pTime;
	CPoint point;

	m_bRecord = FALSE;
	nCount = m_PointArray.GetSize();
	pos = m_TimeList.GetHeadPosition();
	for(nIndex=0;nIndex<nCount;nIndex++)
	{
		point = m_PointArray.GetAt(nIndex);
		if(nIndex == 0)
		{
			pDC->MoveTo(point);
		}
		else
		{
			pDC->LineTo(point);
		}
		pTime = &( m_TimeList.GetNext(pos) );
		strTime.Format( "%d:%d:%d", pTime->GetHour(), pTime->GetMinute(), pTime->GetSecond() );
		pDC->TextOut(point.x, point.y, strTime);
	}
}

void CGeneral_ClassView::OnUpdateRecordEnd(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bRecord == TRUE);
}

void CGeneral_ClassView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CString strIndex, strXY;

	strIndex.Format("%c", nChar);
	m_Map.Lookup(strIndex, strXY);
	if(strXY.IsEmpty() == FALSE)
	{
		AfxMessageBox(strXY);
	}
	else
	{
		AfxMessageBox("No record found");
	}
	CView::OnChar(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

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