menuview.cpp

来自「偶作的毕业设计程序」· C++ 代码 · 共 353 行

CPP
353
字号
// MenuView.cpp : implementation file
//

#include "stdafx.h"
#include "LogDisplayDoc.h"
#include "MenuView.h"
#include "DcuPara.h"
#include "ParaSetDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMenuView

IMPLEMENT_DYNCREATE(CMenuView, CScrollView)

CMenuView::CMenuView()
{
	m_nCurPageCount = m_nScreenHighPerLine = m_nPrintHightPerLine = 0;
	m_bPrintContinue = m_bMenu = FALSE;
	m_strMenuLogTempFileName = "";

	m_pNewFont = m_pOldFont = NULL;
}

CMenuView::~CMenuView()
{
}


BEGIN_MESSAGE_MAP(CMenuView, CScrollView)
	//{{AFX_MSG_MAP(CMenuView)
	ON_WM_LBUTTONDOWN()
	ON_WM_CONTEXTMENU()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMenuView drawing

void CMenuView::OnDraw(CDC* pDC)
{
	CLogDisplayDoc *pDoc = (CLogDisplayDoc *)GetDocument();
	ASSERT(pDoc != NULL);
	
	INT i = 0;
	CRect rectClient, rectClip;
	GetClientRect(rectClient);		//获得滚动视范围
	pDC->GetClipBox(rectClip);		//获得无效矩形区域

	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	m_nScreenHighPerLine = tm.tmHeight + tm.tmExternalLeading;
	INT nStartLine	= rectClip.top / ((m_nScreenHighPerLine == 0) + m_nScreenHighPerLine);	//重绘无效区域起始行
	INT nEndLine	= (rectClip.bottom + m_nScreenHighPerLine - 1) / ((m_nScreenHighPerLine == 0) + m_nScreenHighPerLine);	//重绘无效区域结束行
	////////////////////////////////////////////////
	//用CStdioFile时出现乱码,改为CFile
	//CFile MenuFile;
	CStdioFile MenuFile;
	CString strCurLine;
	CHAR chReadBuf[1000], chCurChar(0);
	INT  nCharFlag(0);
	memset(chReadBuf, 0, sizeof(chReadBuf));
	if (!MenuFile.Open(g_strLogFile, CFile::modeReadWrite | CFile::shareDenyNone))
		return;
	INT nCurPage = nStartLine / 100;
	//MenuFile.Seek(g_dwBeginOffset, CFile::begin);
	if (nCurPage > sizeof(g_dwFileLinePos))
		nCurPage = sizeof(g_dwFileLinePos);
	MenuFile.Seek(g_dwFileLinePos[nCurPage], CFile::begin);
	i = nCurPage * 100;
	while(MenuFile.ReadString(strCurLine)){
		if (MenuFile.GetPosition() > g_dwEndOffset)
			break;
		//只重画无效矩形区域
		if (i >= nStartLine && i < nEndLine){
			pDC->TextOut(0, i * m_nScreenHighPerLine, strCurLine);
		}
		else if (i >= nEndLine){
			break;
		}
		i++;
	}
	////////////////////////////////////////////////
	//设置滚动视滚动范围 - i * 15: SizeTotal.cy, rectClient.Height() / 15: 每屏行数, 15: 每行高(Unit: pixel)
	//Add by sunyue 2003/03/09
	//SetRange(2000, i * m_nScreenHighPerLine, rectClient.Height(), m_nScreenHighPerLine);				//设置滚动范围
	SetRange(2000, g_dwLineCountOfFile * m_nScreenHighPerLine, rectClient.Height(), m_nScreenHighPerLine);				//设置滚动范围
	MenuFile.Close();
	if ((i == 0) && (strlen(g_strLogFile) == 0)){
		NoCmdLogProcess(pDC, 1);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMenuView diagnostics

#ifdef _DEBUG
void CMenuView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CMenuView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMenuView message handlers
void CMenuView::OnInitialUpdate() 
{
	CScrollView::OnInitialUpdate();
	
	CSize sizeTotal(300, 1000);
	SetScrollSizes(MM_TEXT, sizeTotal);
}

void CMenuView::OnLButtonDown(UINT nFlags, CPoint point) 
{	
	//ShowParaSetDlg(); //弹出参数设置对话框
}

void CMenuView::SetRange(INT nsizeTotalCx, INT nsizeTotalCy, INT nsizePageCy, INT nsizeLineCy)
{
	CSize sizeTotal(nsizeTotalCx, nsizeTotalCy);
	CSize sizePage(0, nsizePageCy);
	CSize sizeLine(0, nsizeLineCy);
	SetScrollSizes(MM_TEXT, sizeTotal, sizePage, sizeLine);
}

void CMenuView::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	CMenu Menu, *pSubMenu;
	Menu.LoadMenu(IDM_TRACK_MENU1);
	pSubMenu = Menu.GetSubMenu(0);

	pSubMenu->TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, this);
}

void CMenuView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) 
{
	m_nXLogPixPerInchPrn = pDC->GetDeviceCaps(LOGPIXELSX);
    m_nYLogPixPerInchPrn = pDC->GetDeviceCaps(LOGPIXELSY);
	
	m_PageWidth = pDC->GetDeviceCaps(HORZRES);
	m_PageHeight = pDC->GetDeviceCaps(VERTRES);

	m_pNewFont = new CFont;
	ASSERT(m_pNewFont != NULL);
	m_pNewFont->CreateFont(10, 10, 0, 0, 400, FALSE, FALSE, 0,
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
	m_pOldFont = pDC->SelectObject(m_pNewFont);
	memset(&m_tm, 0, sizeof(m_tm));
	pDC->GetTextMetrics(&m_tm);
	m_nPrintHightPerLine = m_tm.tmHeight + m_tm.tmExternalLeading;
	m_bPrintContinue = TRUE; //置打印允许标志为TRUE

	CScrollView::OnBeginPrinting(pDC, pInfo);
}

void CMenuView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) 
{
	if (NULL != m_pOldFont){
		pDC->SelectObject(m_pOldFont);
		m_pOldFont = NULL;
	}
	if (NULL != m_pNewFont){
		delete m_pNewFont;
		m_pNewFont = NULL;
	}
	
	CScrollView::OnEndPrinting(pDC, pInfo);
}

BOOL CMenuView::OnPreparePrinting(CPrintInfo* pInfo) 
{
	// TODO: call DoPreparePrinting to invoke the Print dialog box
	
	return CScrollView::DoPreparePrinting(pInfo);
}
//打印
void CMenuView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	CLogDisplayDoc *pDoc = (CLogDisplayDoc *)GetDocument();
	ASSERT(pDoc != NULL);

	//CStdioFile txtFile;
	CFile File;
	CString strCurLine("");
	INT i = 0, nCurPageStartLine = 0, nCurPageEndLine = 0, nCmdTmpFileLineCount = 0;
	CRect rectPrint = pInfo->m_rectDraw;
	CPoint pointOffset(50, 50);
	
	nCurPageStartLine = (m_nCurPageCount - 1) * CLogDisplayDoc::nLinesPerPage;
	nCurPageEndLine = nCurPageStartLine + CLogDisplayDoc::nLinesPerPage;
	//pDoc->GetMenuLogTempFileName(m_strMenuLogTempFileName); //获得操作日志临时文件名
	BOOL bFileEnd = FALSE;

	CHAR chReadBuf[1000], chCurChar;
	INT  nCharFlag(0);
	memset(chReadBuf, 0, sizeof(chReadBuf));
	//if (File.Open(g_strLogFile, CFile::modeReadWrite | CFile::shareDenyNone))
	if (File.Open("LogFile.txt", CFile::modeReadWrite | CFile::shareDenyNone))
	{
		File.Seek(g_dwBeginOffset, CFile::begin);
		////////////////
		while(File.Read(&chCurChar, 1)){
			//Add by sunyue 2003/03/10 因为打印的是临时文件,不判断文件结束
			//if (File.GetPosition() >= g_dwEndOffset){
			//	bFileEnd = TRUE;
			//	break;
			//}
			//提取当前日志行时间
			if ('\n' != chCurChar){ //如果不是回车换行符,继续读
				chReadBuf[nCharFlag] = chCurChar;
				nCharFlag++;
				if (nCharFlag >= sizeof(chReadBuf))
					nCharFlag = 0;
				continue;
			}
			chReadBuf[nCharFlag] = '\0';
			strCurLine = chReadBuf;
			nCharFlag = 0; //将字符计数标记清零
			strCurLine.TrimRight("\r\n");

			//只重画无效矩形区域
			if (i >= nCurPageStartLine && i < nCurPageEndLine){
				pDC->TextOut(0 + pointOffset.x, (i - nCurPageStartLine) * m_nPrintHightPerLine + pointOffset.y, strCurLine);
			}
			if (i >= nCurPageEndLine)
				break;
			i++;
		}
		////////////////
		if (File.GetPosition() >= File.GetLength()){
			bFileEnd = TRUE;
		}
		File.Close();
	}
	//if (nCurPageEndLine >= i){ //如果打印到文件尾,置打印继续标记为FALSE
	if (bFileEnd){ //如果打印到文件尾,置打印继续标记为FALSE
		m_bPrintContinue = FALSE;
	}
}

void CMenuView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	CScrollView::OnPrepareDC(pDC, pInfo);

	if (NULL == pInfo){
		m_nXLogPixPerInchScr = pDC->GetDeviceCaps(LOGPIXELSX);
		m_nYLogPixPerInchScr = pDC->GetDeviceCaps(LOGPIXELSY);
		
		return;
	}
	else{
		///// 转换坐标映射方式
		pDC->SetMapMode(MM_ANISOTROPIC);
		
		LONG nWndWidth	= (m_PageWidth * m_nXLogPixPerInchScr) / m_nXLogPixPerInchPrn;
		LONG nWndHeight = (m_PageHeight * m_nYLogPixPerInchScr) / m_nYLogPixPerInchPrn;
		
		CSize csizeWnd(nWndWidth, nWndHeight);
		///// 确定窗口大小
		pDC->SetWindowExt(csizeWnd);
		pDC->SetWindowOrg(0, 0);
		///// 确定视口大小
		pDC->SetViewportExt(m_PageWidth, m_PageHeight);
		pDC->SetViewportOrg(0,0);
		//动态分页
		m_nCurPageCount = pInfo->m_nCurPage;	//获得当前页号
		pInfo->m_bContinuePrinting = m_bPrintContinue;
	}
}
//弹出参数设置对话框
void CMenuView::ShowParaSetDlg()
{
	CParaSetDlg Dlg(NULL);
	Dlg.m_pDoc = (CLogDisplayDoc *)GetDocument();
	INT nRet = Dlg.DoModal();
	if (nRet == IDOK){
		g_bParaSet = TRUE;
		CDocument* pDoc = GetDocument();
		pDoc->UpdateAllViews(NULL);
	}
}
//函数功能:当找不到日志文件时,给出提示信息
//参    数:pDC		设备描述表
//			nType	类型 0:没进行参数设置	1:在当前参数设置下找不到日志信息
void CMenuView::NoCmdLogProcess(CDC *pDC, INT nType)
{
	CRect rectClient, rectDraw;
	CString strWarning = "";
	switch(nType){
	case 0:
		strWarning = "请点击鼠标左键进行参数设置";
		break;
	case 1:
		strWarning = "该时间段没有命令日志记录";
		break;
	default: break;
	}
	GetClientRect(rectClient);
	rectDraw = rectClient;
	rectDraw.top = rectClient.top + rectClient.Height() / 3;
	rectDraw.bottom = rectClient.top + 2 * rectClient.Height() / 3;
	pDC->DrawText(strWarning, rectDraw, DT_CENTER | DT_SINGLELINE);
}

void CMenuView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	//CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
	switch(nChar){
	case VK_HOME:
		OnVScroll(SB_TOP, 0, NULL);
		OnHScroll(SB_LEFT, 0, NULL);
		break;
	case VK_END:
		OnVScroll(SB_BOTTOM, 0, NULL);
		//OnHScroll(SB_RIGHT, 0, NULL);
		break;
	case VK_UP:
		OnVScroll(SB_LINEUP, 0, NULL);
		break;
	case VK_DOWN:
		OnVScroll(SB_LINEDOWN, 0, NULL);
		break;
	case VK_PRIOR:
		OnVScroll(SB_PAGEUP, 0, NULL);
		break;
	case VK_NEXT:
		OnVScroll(SB_PAGEDOWN, 0, NULL);
		break;
	case VK_LEFT:
		OnHScroll(SB_LINELEFT, 0, NULL);
		break;
	case VK_RIGHT:
		OnHScroll(SB_LINERIGHT, 0, NULL);
		break;
	default:
		break;
	}
}

⌨️ 快捷键说明

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