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

📄 pcaview.cpp

📁 face recognition test source code
💻 CPP
字号:
// pcaview.cpp : implementation of the CPcaView class
//

#include "stdafx.h"
#include "pca.h"

#include "pcadoc.h"
#include "pcaview.h"

#include "str.h"
#include "utilcpp.h"
#include "error.h"
#include "utilcpp.h"
#include "str.h"
#include "def.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>

#define MEM_SIZESTART		10000
#define MEM_SIZEEXPAND		5000
#define MAX_CHAR_LINE		255

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPcaView

IMPLEMENT_DYNCREATE(CPcaView, CView)

BEGIN_MESSAGE_MAP(CPcaView, CView)
	//{{AFX_MSG_MAP(CPcaView)
	ON_WM_VSCROLL()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_HSCROLL()
	ON_WM_DESTROY()
	ON_MESSAGE(WM_PUTINFO, OnPutInfo)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPcaView construction/destruction

BOOL CPcaView::PreCreateWindow(CREATESTRUCT &cs)
	{
	CView::PreCreateWindow(cs);
	
	cs.style |= WS_HSCROLL | WS_VSCROLL;
	return TRUE;
	}

CPcaView::CPcaView()
	{
	int nHeight;
	int nWidth;
	
	pMem = new CMemory();
	pMem->Allocate(MEM_SIZESTART);
	lReportLen = 0;
	lLine = 0;
	pText = NULL;
	pLineArray = new CDWordArray();
	pLineArray->Add(0);
	
	/*The number of the first line that has to be displayed at the beginning is one*/
	nVPos = 1;
	nHPos = 1;
	
	nWidth = GetCharWidth();
	nHeight = GetCharHeight();
	}

CPcaView::~CPcaView()
	{
	delete pMem;
	
	pLineArray->RemoveAt(0, lLine);
		  
	delete pLineArray;
	}

/////////////////////////////////////////////////////////////////////////////
// CPcaView drawing

void CPcaView::OnDraw(CDC *pDC)
	{
	int nHeight, nWidth;
	long l, lstart, lend;
	CPcaDoc* pDoc = GetDocument();
	RECT area;
	
	nHeight = GetCharHeight();
	nWidth = GetCharWidth();
	
	GetClientRect(&area);
	
	lstart = area.top / GetCharHeight();
	lend = area.bottom / GetCharHeight() + 1L;
	
	lstart += nVPos;
	lend += nVPos;
	
	if (lstart < 1)
		lstart = 1;
	if (lend > lLine)
		lend = lLine;
		
	for (l = lstart; l <= lend; l++)
		{
		DrawLine(l, pDC);
		}
	}

/////////////////////////////////////////////////////////////////////////////
// CPcaView printing

/////////////////////////////////////////////////////////////////////////////
// CPcaView diagnostics

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

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

CPcaDoc* CPcaView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPcaDoc)));
	return (CPcaDoc*) m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPcaView message handlers

void CPcaView::AppendMsg(char *str)
	{
	int nLinePage;
	RECT lr, rect;
	int nWidth, nHeight;
	
	while (lReportLen + strlen(str) >= pMem->GetSize())
		{															/*Need to reallocate memory*/
		pMem->Reallocate(lReportLen + MEM_SIZEEXPAND);
		}
	
	pText = (char *)pMem->GetPtr();
	
	strcpy(pText + lReportLen, str);
	
	pMem->ReleasePtr();
	lLine++;
	pLineArray->Add(lReportLen + (long)strlen(str));
	
	nWidth = GetCharWidth();
	nHeight = GetCharHeight();
	lr.left = 0;												/*lr, now, contains the rectangle that bounds*/
	lr.top = 0;                                     /*the whole text contained in the window.*/
	lr.bottom = lLine * nHeight + 10L;
	lr.right = MAX_CHAR_LINE * nWidth;
	
	GetClientRect(&rect);
	nLinePage = floor((float)(rect.bottom - rect.top) / (float)GetCharHeight());
	if (lLine > nLinePage)
		{
		nVPos = lLine - nLinePage + 1;
		SetScrollRange(SB_VERT, 1, nVPos, FALSE);

		ScrollWindow(0, -nHeight, NULL, NULL);
		SetScrollPos(SB_VERT, nVPos);
				
		/*Invalidate the area that must be drawn*/
		GetClientRect(&lr);									/*lr, now, contains the bounding rectangle of the client window*/
		lr.top = lr.bottom - 2*nHeight;
		InvalidateRect(&lr);
		}
	else
		{
		/*Invalidate the area that must be drawn*/
		lr.top = (lLine-1) * nHeight;
		lr.bottom = lLine * nHeight;
		InvalidateRect(&lr);
		}
	
	lReportLen += strlen(str);
	}

int CPcaView::GetCharHeight()
	{
	return 13;
	}

int CPcaView::GetCharWidth()
	{
	return 8;
	}

void CPcaView::DrawCharLine(long lPos)
	{
	long lCurLine;
	
	lCurLine = GetLineNumber(lPos);
	
	DrawLine(lCurLine);
	}

void CPcaView::DrawLine(long lCurLine, CDC *pDC)	
	{
	char str_line[MAX_CHAR_LINE], *str_end;
	long length, lPos;
	BOOL bProvidedDC = TRUE;
	RECT lr;
	
	/* Check the line number requested; It must be > 1 and < lLine */
	if (lCurLine < 1)
		return;
	if (lCurLine > lLine)
		return;
	lCurLine--;
	
	/* Get the pointer to the whole data */
	pText = (char*)pMem->GetPtr();
	              
	/* Get the windows' device context */
	if (pDC == NULL)
		{
		bProvidedDC = FALSE;
		pDC = GetDC();
		if (pDC == NULL)
			return;
		}	
	
	/* Get the position int the data (pText) of the text at the line lCurLine */
	lPos = pLineArray->GetAt(lCurLine);
	
	str_end = strchr(pText + lPos, chr_RT2);
	if (str_end != NULL)
		{
		length = (long)str_end - (long)pText - (long)lPos;
		if (length >= MAX_CHAR_LINE)
			length = MAX_CHAR_LINE - 1L;
		}
	else
		length = strlen(pText) - lPos;
	
	/*Adjust the horizontal alignment*/
	if (length > nHPos)
		{
		length -= (long)nHPos-1L;
		strncpy(str_line, pText + lPos + (long)(nHPos-1), length);
		str_line[length] = 0;
		}
	else
		str_line[0] = 0; 
	
	pMem->ReleasePtr();

	/*Select the font*/
	hfontOld = (HFONT)::SelectObject(pDC->m_hDC, (HGDIOBJ)hfont);

	SetRect(&lr, 0, (lCurLine+1 - nVPos)*GetCharHeight(), strlen(str_line)*GetCharWidth(), 
		(lCurLine+2 - nVPos)*GetCharHeight());
	pDC->DrawText(str_line, strlen(str_line), &lr, DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE);
	
	SelectObject(pDC->m_hDC, hfontOld);					/*deselect the font*/

	if (bProvidedDC == FALSE)
		ReleaseDC(pDC);
	}

long CPcaView::GetLineNumber(long lPos)
	{
	long l, Item;
	
	l = 0L;
	do
		{
		Item = pLineArray->GetAt(l);
		l++;
		}
	while (l <= lLine && Item > lPos);
	
	return l;
	}

int CPcaView::Save(char *filename)
	{
	FILE* nf;
  	long lwritten;
   	
	nf = fopen(filename, "w");								/*Create the file in text mode, overwites it if it doesn't exist*/
	if (nf == NULL)
		{
		Msg(-1, str_ERR_FILE_OPEN, strerror(errno));
		return ERR_FILE_OPEN;
		}
	
	pText = (char *)pMem->GetPtr();
	lwritten = fwrite(pText, sizeof(char), lReportLen, nf);
	pMem->ReleasePtr();
	if (lwritten != lReportLen)
		{
		Msg(0, str_ERR_FILE_WRITE, lwritten, lReportLen);
		}
	if (fclose(nf) == -1)
		{
		Msg(0, str_ERR_FILE_CLOSE);
		}
	
	Msg(0, str_SAVE_NUM, lwritten); 
	
	return 0;
	}

void CPcaView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
	{
	RECT rect;
	int nLinePage, nPrevVPos;
	
	GetClientRect(&rect);
	nLinePage = floor((float)(rect.bottom - rect.top) / (float)GetCharHeight());
   
	nPrevVPos = nVPos;
	switch (nSBCode)
		{
		case SB_LINEDOWN:
			nVPos++;
			break;
		
		case SB_LINEUP:
			nVPos--;
			break;
		
		case SB_PAGEDOWN:
			nVPos += nLinePage;
			break;
		
		case SB_PAGEUP:
			nVPos -= nLinePage;
			break;
		
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			nVPos = nPos;
			break;
		}
	if (nVPos < 1)
		nVPos = 1;
   if (nVPos > lLine - nLinePage + 1)
   	nVPos = lLine - nLinePage + 1;
   
	SetScrollPos(SB_VERT, nVPos);
   ScrollWindow(0, -(nVPos - nPrevVPos)*GetCharHeight());
   
	CView::OnVScroll(nSBCode, nPos, pScrollBar);
	}

void CPcaView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
	{  
	RECT rect;
	int nPrevHPos;
	
	GetClientRect(&rect);

	nPrevHPos = nHPos;
	switch (nSBCode)
		{
		case SB_LINERIGHT:
			nHPos++;
			break;
		
		case SB_LINELEFT:
			nHPos--;
			break;
		
		case SB_PAGERIGHT:
			nHPos += (float)rect.right/(float)GetCharWidth();
			break;
		
		case SB_PAGEUP:
			nHPos -= (float)rect.right/(float)GetCharWidth();
			break;
		
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			nHPos = nPos;
			break;
		}
	if (nHPos < 1)
		nHPos = 1;
   if (nHPos > MAX_CHAR_LINE - (float)rect.right/(float)GetCharWidth() + 1)
   	nHPos = MAX_CHAR_LINE - (float)rect.right/(float)GetCharWidth() + 1;
   
	SetScrollPos(SB_HORZ, nHPos);
   //ScrollWindow(-(nHPos - nPrevHPos)*GetCharWidth(), 0);
	
	CView::OnHScroll(nSBCode, nPos, pScrollBar);
	
	Invalidate();
	}

int CPcaView::OnCreate(LPCREATESTRUCT lpCreateStruct)
	{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	RECT rect;
	
	GetClientRect(&rect);

	SetScrollRange(SB_VERT, 1, 1);						/* Initialise the vertical scroll range*/
	SetScrollRange(SB_HORZ, 1, MAX_CHAR_LINE - (float)rect.right/(float)GetCharWidth() + 1);		
																	/* Initialise the horizontal scroll range*/
	/*Create the font*/
	LPSTR pszFace = "MS Sans Serif";
	HDC hdc = ::GetDC(GetSafeHwnd());
	
	MapModePrevious = SetMapMode(hdc, MM_TWIPS);
	hfont = CreateFont(-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Courier"/*"MS Sans Serif"*/);
	SetMapMode(hdc, MapModePrevious);
	
	::ReleaseDC(GetSafeHwnd(), hdc);
	
	return 0;
	}

void CPcaView::OnDestroy()
	{
	CView::OnDestroy();
	
	/*Destroy the font*/
	DeleteObject(hfont);
	}

void CPcaView::OnSize(UINT nType, int cx, int cy)
	{
	CView::OnSize(nType, cx, cy);
	
	/*Reset the vertical scroll range*/
	int nLinePage = floor((float)cy / (float)GetCharHeight());
	if (lLine - nLinePage + 1 > 1)
		SetScrollRange(SB_VERT, 1, lLine - nLinePage + 1);

	/*Reset the horizontal scroll range*/
	SetScrollRange(SB_HORZ, 1, MAX_CHAR_LINE - (float)cx/(float)GetCharWidth() + 1);		
	}

LRESULT CPcaView::OnPutInfo(WPARAM wParam, LPARAM str_Info)
	{
	AppendMsg((char*)str_Info);
	return NULL;
	}

⌨️ 快捷键说明

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