kmtview.cpp

来自「关于使用VC编程的代码」· C++ 代码 · 共 273 行

CPP
273
字号
// KMTView.cpp : implementation of the CKMTView class
//

#include "stdafx.h"
#include "KMT.h"

#include "KMTDoc.h"
#include "KMTView.h"
#include "CircleDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKMTView

IMPLEMENT_DYNCREATE(CKMTView, CView)

BEGIN_MESSAGE_MAP(CKMTView, CView)
	//{{AFX_MSG_MAP(CKMTView)
	ON_WM_CHAR()
	ON_COMMAND(ID_BEGIN_AUTO_OUTPUT, OnBeginAutoOutput)
	ON_UPDATE_COMMAND_UI(ID_BEGIN_AUTO_OUTPUT, OnUpdateBeginAutoOutput)
	ON_COMMAND(ID_END_AUTO_OUTPUT, OnEndAutoOutput)
	ON_UPDATE_COMMAND_UI(ID_END_AUTO_OUTPUT, OnUpdateEndAutoOutput)
	ON_WM_TIMER()
	ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
	ON_COMMAND(ID_DRAW_POLYGRON, OnDrawPolygron)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_CLEAR_ALL, OnClearAll)
	ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKMTView construction/destruction

CKMTView::CKMTView()
{
	m_nRowIndex = 0;
	m_nColIndex = 0;
	m_bAutoOutput = FALSE;
	m_bPoly = FALSE;
}

CKMTView::~CKMTView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CKMTView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CKMTView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CKMTView message handlers

void CKMTView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CharOut( (char)nChar );
	CView::OnChar(nChar, nRepCnt, nFlags);
}

void CKMTView::OnBeginAutoOutput() 
{
	m_bAutoOutput = TRUE;
	m_nAutoCount = 0;
	srand( (unsigned)time( NULL ) );
	SetTimer(1, 100, 0);
}

void CKMTView::OnUpdateBeginAutoOutput(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bAutoOutput == FALSE);	
}

void CKMTView::OnEndAutoOutput() 
{
	KillTimer(1);
	m_bAutoOutput = FALSE;	
}

void CKMTView::OnUpdateEndAutoOutput(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bAutoOutput == TRUE);	
}

void CKMTView::OnTimer(UINT nIDEvent) 
{
	int nRand,nBase;

	KillTimer(1);
	nBase = (int)'a';
	nRand = rand() % 26 + nBase;
	CharOut( (char)nRand );
	m_nAutoCount++;
	if(m_nAutoCount % 10 == 0)
	{
		CharOut((char) 13);
	}
	if(m_nAutoCount < 100)
	{
		SetTimer(1, 100, 0);
	}
	else
	{
		m_bAutoOutput = FALSE;
	}
	
	CView::OnTimer(nIDEvent);
}

void CKMTView::CharOut(char chVal)
{
	CDC* pDC = GetDC();
	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);

	if( chVal == 13 )
	{
		m_nRowIndex++;
		m_nColIndex = 0;
	}
	else if( (chVal >= 32) && (chVal < 127) )
	{
		CString str;
		str.Format("%c", chVal);
		pDC->TextOut( m_nColIndex * tm.tmMaxCharWidth, m_nRowIndex * (tm.tmExternalLeading + tm.tmHeight), str );
		m_nColIndex++;
	}
}

void CKMTView::OnDrawLine() 
{
	CPoint point[2];
	CRect ClientRect;
	CDC* pDC = GetDC();
	GetClientRect(&ClientRect);

	srand(time(NULL));
	point[0].x = rand() % ClientRect.right;
	point[0].y = rand() % ClientRect.bottom;
	point[1].x = rand() % ClientRect.right;
	point[1].y = rand() % ClientRect.bottom;
	
	pDC->MoveTo(point[0]);
	pDC->LineTo(point[1]);
}

void CKMTView::OnDrawPolygron() 
{
	m_bFirstPoint = TRUE;
}

void CKMTView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_bFirstPoint == TRUE)
	{
		m_bPoly = TRUE;
		m_bFirstPoint = FALSE;
		m_OrgPoint = point;
		m_PrevPoint = point;
		m_NextPoint = point;
		SetCapture();
	}
	
	CView::OnLButtonDown(nFlags, point);
}

void CKMTView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_bPoly == TRUE)
	{
		m_PrevPoint = point;
	}
	
	CView::OnLButtonUp(nFlags, point);
}

void CKMTView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(m_bPoly == TRUE)
	{
		CDC* pDC = GetDC();
		pDC->SetROP2(R2_NOT);
		pDC->MoveTo(m_PrevPoint);
		pDC->LineTo(m_NextPoint);
		pDC->MoveTo(m_PrevPoint);
		m_NextPoint = point;
		pDC->LineTo(m_NextPoint);
	}

	CView::OnMouseMove(nFlags, point);
}

void CKMTView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	if(m_bPoly == TRUE)
	{
		CDC* pDC = GetDC();
		pDC->MoveTo(point);
		pDC->LineTo(m_OrgPoint);
		m_bPoly = FALSE;
		ReleaseCapture();
	}
	CView::OnRButtonDown(nFlags, point);
}

void CKMTView::OnClearAll() 
{
	CRect ClientRect;
	GetClientRect(&ClientRect);
	CDC* pDC = GetDC();
	CBrush bkBrush(pDC->GetBkColor());
	pDC->FillRect(&ClientRect, &bkBrush);
}

void CKMTView::OnDrawCircle() 
{
	CCircleDlg dlg;
	if(dlg.DoModal() == IDOK)
	{
		CDC* pDC = GetDC();
		CRect rect;
		rect.top = dlg.m_nY - dlg.m_nDiam/2;
		rect.bottom = dlg.m_nY + dlg.m_nDiam/2;
		rect.left = dlg.m_nX - dlg.m_nDiam/2;
		rect.right = dlg.m_nX + dlg.m_nDiam/2;
		pDC->Ellipse(&rect);
	}
}

⌨️ 快捷键说明

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