lineview.cpp
来自「串口通讯演示程序」· C++ 代码 · 共 788 行 · 第 1/2 页
CPP
788 行
// LineView.cpp : implementation of the CLineView class
//
#include "stdafx.h"
#include "CeTools.h"
//#include "LineDoc.h"
#include "LineView.h"
#include "DaySel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLineView
IMPLEMENT_DYNCREATE(CLineView, CScrollView)
BEGIN_MESSAGE_MAP(CLineView, CScrollView)
//{{AFX_MSG_MAP(CLineView)
ON_COMMAND(IDM_ERRRATE_COLA, OnErrRateColA)
ON_COMMAND(IDM_ERRRATE_COLB, OnErrRateColB)
ON_COMMAND(IDM_DATA_COLA, OnDataColA)
ON_COMMAND(IDM_DATA_COLB, OnDataColB)
ON_COMMAND(IDM_NODATA_COL, OnNoDataCol)
ON_COMMAND(IDM_FLAG_COL, OnFlagCol)
ON_COMMAND(IDM_CURSOR_COL, OnCursorCol)
ON_COMMAND(IDM_SHOWA, OnShowA)
ON_COMMAND(IDM_SHOWB, OnShowB)
ON_COMMAND(IDM_CURRENTDAY, OnCurDay)
ON_COMMAND(IDM_OLDDAY, OnOldDay)
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLineView construction/destruction
CLineView::CLineView()
{
// TODO: add construction code here
IsInit=0;
ReadFileDataFlag=0;
OldSecond=0;
ShowSel=0;
ZeroBuf();
}
CLineView::~CLineView()
{
}
BOOL CLineView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLineView drawing
void CLineView::OnDraw(CDC* pDC)
{
// CLineDoc* pDoc = GetDocument();
// ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int BottomOffset=30;
CRect rect;
GetClientRect(&rect);
//画曲线
if(ShowA)
DrawDataLine(pDC,ErrCodeRate_str,rect,0);//A
else
DrawDataLine(pDC,ErrCodeRate_str,rect,1);//B
//X方向标尺
DrawXText(pDC,rect);
//设置原点
CPoint point=pDC->GetViewportOrg();
pDC->SetViewportOrg(point.x+OffsetX,point.y);
//画十字线 显示数值
if(ShowA)
DrawCrusorData(pDC,ErrCodeRate_str,rect,Cx,Cy,OffsetX,0);
else
DrawCrusorData(pDC,ErrCodeRate_str,rect,Cx,Cy,OffsetX,1);
//画Y方向坐标
if(ShowA)
DrawYLine(pDC,ErrCodeRate_str,rect,0);
else
DrawYLine(pDC,ErrCodeRate_str,rect,1);
//设置页尺寸
CSize docSize(1440+100,120);
CSize pageSize(rect.right,rect.bottom);
CSize lineSize(0,120);
SetScrollSizes(MM_TEXT,docSize,pageSize,lineSize);
}
void CLineView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
Cx=0;
Cy=0;
OffsetX=0;
ShowA=1;
OldScrollPos=0;
IsInit=1;
OldDay.Empty();
app = (CCeToolsApp *)AfxGetApp();
SetTimer(1,1000,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CLineView printing
BOOL CLineView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CLineView diagnostics
/*
#ifdef _DEBUG
void CLineView::AssertValid() const
{
CScrollView::AssertValid();
}
void CLineView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CLineDoc* CLineView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLineDoc)));
return (CLineDoc*)m_pDocument;
}
#endif //_DEBUG
*/
/////////////////////////////////////////////////////////////////////////////
// CLineView message handlers
void CLineView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Cx=point.x;
Cy=point.y;
Invalidate();
CRect rect;
GetClientRect(&rect);
if(point.x>=rect.left+60 && point.x<rect.right-10 && point.y<rect.bottom-30 && point.y>rect.top+10)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZE));
if(app->ShowMouseFlag==1)
{
// ShowCursor(FALSE);
app->ShowMouseFlag=0;
SetFocus();
}
}
else
{
if(app->ShowMouseFlag==0)
{
ShowCursor(TRUE);
app->ShowMouseFlag=1;
}
}
CScrollView::OnMouseMove(nFlags, point);
}
BOOL CLineView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
{
// TODO: Add your specialized code here and/or call the base class
CPoint point=GetScrollPosition();
OffsetX=point.x;
Invalidate();
return CScrollView::OnScrollBy(sizeScroll, bDoScroll);
}
void CLineView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(m_hWnd!=NULL && IsInit==1)
{
CPoint point=GetScrollPosition();
OffsetX=point.x;
}
Invalidate();
}
void CLineView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime time;
int sc;
CString str1,str;
time=CTime::GetCurrentTime();
sc=time.GetSecond();
if(ShowSel==0)//今日数据
{
if(ReadFileDataFlag==0 || sc<OldSecond)
{
ReadFileDataFlag=1;
str=time.Format("%Y%m%d");
str1=str + "A.txt";
ReadLogFile(&ErrCodeRate_str[0],str1);
str1=str + "B.txt";
ReadLogFile(&ErrCodeRate_str[1],str1);
Invalidate();
}
}
OldSecond=sc;
CScrollView::OnTimer(nIDEvent);
}
int CLineView::ReadLogFile(err_struct *ErrCodeRatestr, CString FileName)
{
CStdioFile cf;
int i=0;
CString str;
SaveData_Struct SaveDataStruct;
if(!cf.Open(FileName,CFile::modeRead))
return 0;
i=0;
cf.SeekToBegin();
while(TRUE)
{
if(!cf.ReadString(str))
break;
sscanf(str,"%u %u %u %f %f %u %u",
&SaveDataStruct.run,
&SaveDataStruct.hour,
&SaveDataStruct.minute,
&SaveDataStruct.m,
&SaveDataStruct.t,
// &SaveDataStruct.total,
// &SaveDataStruct.commtotal
&SaveDataStruct.commtotal,
&SaveDataStruct.total
);
i=SaveDataStruct.hour*60+SaveDataStruct.minute;
if(i>1440 || i<0)
continue;
ErrCodeRatestr->ErrCodeRate_t[i]=SaveDataStruct.m;
ErrCodeRatestr->run[i]=SaveDataStruct.run;
ErrCodeRatestr->total[i]=SaveDataStruct.total;
ErrCodeRatestr->commtotal[i]=SaveDataStruct.commtotal;
if(ErrCodeRatestr->MaxVal<SaveDataStruct.m)
ErrCodeRatestr->MaxVal=SaveDataStruct.m;
if(ErrCodeRatestr->MaxTotal<SaveDataStruct.total)
ErrCodeRatestr->MaxTotal=SaveDataStruct.total;
}
cf.Close();
return 1;
}
BOOL CLineView::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_RBUTTONDOWN)
{
CMenu Menu;
CPoint pt;
pt.x = LOWORD(pMsg->lParam);
pt.y = HIWORD(pMsg->lParam);
ClientToScreen(&pt);
if (Menu.LoadMenu(IDR_MENU1))
{
CMenu* pSubMenu = Menu.GetSubMenu(0);
if (pSubMenu!=NULL)
{
if(ShowSel==0)
{
pSubMenu->CheckMenuItem(IDM_CURRENTDAY,MF_CHECKED);
pSubMenu->CheckMenuItem(IDM_OLDDAY,MF_UNCHECKED);
}
else
{
pSubMenu->CheckMenuItem(IDM_OLDDAY,MF_CHECKED);
pSubMenu->CheckMenuItem(IDM_CURRENTDAY,MF_UNCHECKED);
}
if(ShowA)
{
pSubMenu->CheckMenuItem(IDM_SHOWA,MF_CHECKED);
pSubMenu->CheckMenuItem(IDM_SHOWB,MF_UNCHECKED);
}
else
{
pSubMenu->CheckMenuItem(IDM_SHOWB,MF_CHECKED);
pSubMenu->CheckMenuItem(IDM_SHOWA,MF_UNCHECKED);
}
CWnd* pWndPop=this;
pWndPop=pWndPop->GetParent();
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x,pt.y,pWndPop);
}
}
}
return CScrollView::PreTranslateMessage(pMsg);
}
void CLineView::OnShowA()
{
ShowA=1;
}
void CLineView::OnShowB()
{
ShowA=0;
}
void CLineView::OnCurDay()
{
ZeroBuf();
ReadFileDataFlag=0;
ShowSel=0;//今日数据
}
void CLineView::OnOldDay()
{
CDaySel dlg;
dlg.DoModal();
CString str,str1;
str=dlg.YMD;
if(!str.IsEmpty())
{
ZeroBuf();
ShowSel=1;//历史数据
str1=str + "A.txt";
if(!ReadLogFile(&ErrCodeRate_str[0],str1))
{
MessageBox("A文件打开失败");
return;
}
str1=str + "B.txt";
if(!ReadLogFile(&ErrCodeRate_str[1],str1))
{
MessageBox("B文件打开失败");
return;
}
Invalidate();
}
}
void CLineView::ZeroBuf()
{
ZeroMemory(ErrCodeRate_str,sizeof(ErrCodeRate_str));
}
void CLineView::DrawDataLine(CDC *pDC, err_struct *ErrCodeRatestr,CRect rect,int ColroFlag)
{
//画曲线
int temp=0,temp1=0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?