📄 report.cpp
字号:
// report.cpp : implementation of the CReport class
//
#include "stdafx.h"
#include "report.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
CReport::CReport()
{
int nHeight;
int nWidth;
RECT rect;
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;
nWidth = GetCharWidth();
nHeight = GetCharHeight();
//GetSteps((short*)&nWidth, (short*)&nHeight);
}
CReport::~CReport()
{
long l;
delete pMem;
pLineArray->RemoveAt(0, lLine);
delete pLineArray;
}
/////////////////////////////////////////////////////////////////////////////
// CReport drawing
void CReport::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);
}
}
/////////////////////////////////////////////////////////////////////////////
// CReport printing
/////////////////////////////////////////////////////////////////////////////
// CReport diagnostics
#ifdef _DEBUG
void CReport::AssertValid() const
{
CView::AssertValid();
}
void CReport::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPcaDoc* CReport::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPcaDoc)));
return (CPcaDoc*) m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CReport message handlers
void CReport::AppendMsg(char *str)
{
int nLinePage;
RECT lr, lr2, rect;
POINT lp;
int nWidth, nHeight;
CWnd *pWnd;
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());
int nMin, nMax;
//GetScrollRange(SB_VERT, &nMin, &nMax);
//SetScrollRange(SB_VERT, 1, nMax+1);
SetScrollRange(SB_VERT, 1, 10);
GetClientRect(&lr2); /*lr2, now, contains the bounding rectangle of the client window*/
if (lr.bottom > lr2.bottom)
{
//ScrollWindow(0, -nHeight, NULL, NULL);
//SetScrollPos(SB_VERT, nMax+1);
}
/* lr.top = (lLine-1L) * nHeight;
lr.bottom = lLine * nHeight;
InvalidateRect(&lr);
*/
lReportLen += strlen(str);
}
int CReport::GetCharHeight()
{
return 13;
}
int CReport::GetCharWidth()
{
return 8;
}
void CReport::DrawCharLine(long lPos)
{
long lCurLine;
lCurLine = GetLineNumber(lPos);
DrawLine(lCurLine);
}
void CReport::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 = str_end - pText - lPos;
if (length >= MAX_CHAR_LINE)
length = MAX_CHAR_LINE - 1;
}
else
length = strlen(pText) - lPos;
strncpy(str_line, pText + lPos, length);
str_line[length] = 0;
pMem->ReleasePtr();
SetRect(&lr, 0, (lCurLine-1L)*GetCharHeight(), strlen(str_line)*GetCharWidth(), lCurLine*GetCharHeight());
pDC->DrawText(str_line, strlen(str_line), &lr, DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE);
if (bProvidedDC == FALSE)
ReleaseDC(pDC);
}
long CReport::GetLineNumber(long lPos)
{
long l, Item;
l = 0L;
do
{
Item = pLineArray->GetAt(l);
l++;
}
while (l <= lLine && Item > lPos);
return l;
}
int CReport::Save(char *filename)
{
FILE* nf;
long lwritten;
nf = fopen(filename, "w"); /*Create the file in binary mode, overwites it if it doesn't exist*/
if (nf == NULL)
{
Msg(-1, str_ERR_FILE_OPEN, 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 CReport::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_ENDSCROLL:
case SB_BOTTOM:
nVPos = lLine;
break;
case SB_TOP:
nVPos = 1;
break;
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 > 10/*lLine - nLinePage*/)
nVPos = 10/*lLine - nLinePage*/;
int nMin, nMax;
GetScrollRange(SB_VERT, &nMin, &nMax);
int i=SetScrollPos(SB_VERT, nVPos);
ScrollWindow(0, -(nVPos - nPrevVPos)*GetCharHeight());
CView::OnVScroll(nSBCode, nPos, pScrollBar);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -