📄 curverdemoview.cpp
字号:
// CurverDemoView.cpp : implementation of the CCurverDemoView class
//
#include "stdafx.h"
#include "CurverDemo.h"
#include "CurverDemoDoc.h"
#include "CurverDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView
IMPLEMENT_DYNCREATE(CCurverDemoView, CView)
BEGIN_MESSAGE_MAP(CCurverDemoView, CView)
//{{AFX_MSG_MAP(CCurverDemoView)
ON_COMMAND(ID_ABOUT, OnHelptopic)
ON_WM_PAINT()
ON_COMMAND(ID_MAINBMP , OnMainCurver)
ON_COMMAND(ID_MSGSEL ,OnMsgSel)
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView construction/destruction
CCurverDemoView::CCurverDemoView()
{
// TODO: add construction code here
nPointSize = 0;
}
CCurverDemoView::~CCurverDemoView()
{
db.Close();
}
BOOL CCurverDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView drawing
void CCurverDemoView::OnDraw(CDC* pDC)
{
CCurverDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView printing
BOOL CCurverDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCurverDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCurverDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView diagnostics
#ifdef _DEBUG
void CCurverDemoView::AssertValid() const
{
CView::AssertValid();
}
void CCurverDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCurverDemoDoc* CCurverDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCurverDemoDoc)));
return (CCurverDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCurverDemoView message handlers
void CCurverDemoView::OnInitialUpdate()
{
CView::OnInitialUpdate();
db.Open("JHWY");
CreateDevice();
InitbufferVal();
this->m_DataWnd.Create(this);
// TODO: Add your specialized code here and/or call the base class
}
void CCurverDemoView::OnHelptopic()
{
// TODO: Add your command handler code here
this->m_SysType = ABOUT;
this->m_pAboutMsgDlg->ShowWindow(SW_SHOW);
this->m_pAboutMsgDlg->m_SysMsg.StartShowMsg();
}
void CCurverDemoView::OnMainCurver()
{
this->m_SysType = CURVE;
this->m_pAboutMsgDlg->ShowWindow(SW_HIDE);
this->m_pAboutMsgDlg->m_SysMsg.TerminateShowMsg();
}
void CCurverDemoView::OnMsgSel()
{
CMsgSelDlg dlg;
CString szMsg,szDate;
if(dlg.DoModal() == IDOK)
{
if(ReadHisMsgFromFile(dlg.szYear,dlg.szMonth,dlg.szDay))
{
szMsg = dlg.szName;
szDate = dlg.szYear+"年"+dlg.szMonth+"月"+dlg.szDay+"日";
this->m_LineMsgEdit.SetWindowText(szMsg);
this->m_LineDataEdit.SetWindowText(szDate);
for(int i = 0; i < 1000; i++)
{
if(m_ItemMsg[i].szId == dlg.szId)
{
this->nPointSize = i;
break;
}
}
Invalidate();
}
}
}
void CCurverDemoView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
GetClientRect(&rect);
HBITMAP hbitmap;
switch(this->m_SysType)
{
case CURVE:
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_ONTIME_CURVE));
break;
case ABOUT:
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BACKBMP));
break;
default:
hbitmap=::LoadBitmap(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_ONTIME_CURVE));
break;
}
HDC hMenDC=::CreateCompatibleDC(NULL);
SelectObject(hMenDC,hbitmap);
::StretchBlt(dc.m_hDC,0,0,1024,768,hMenDC,0,0,1024,768,SRCCOPY);
::DeleteDC(hMenDC);
::DeleteObject(hbitmap);
DrawRealCurve();
// Do not call CView::OnPaint() for painting messages
}
BOOL CCurverDemoView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CCurverDemoView::CreateDevice()
{
//Create AboutMsg Dialog
CRect rect = CRect(100,100,1000,701);
this->m_pAboutMsgDlg = new CAboutMsgDlg(this);
this->m_pAboutMsgDlg->Create(IDD_DLG_ABOUT,this);
this->m_pAboutMsgDlg->ShowWindow(SW_SHOW);
this->m_pAboutMsgDlg->MoveWindow(CRect(26,112,1001,724),TRUE);
this->m_pAboutMsgDlg->GetClientRect(&rect);
this->m_pAboutMsgDlg->m_SysMsg.MoveWindow(rect,TRUE);
this->m_pAboutMsgDlg->ShowWindow(SW_HIDE);
//
this->m_LineMsgRect.left = 131; this->m_LineMsgRect.top = 29;
this->m_LineMsgRect.right = 404; this->m_LineMsgRect.bottom = 61;
this->m_LineDataRect.left = 758; this->m_LineDataRect.top = 29;
this->m_LineDataRect.right = 966; this->m_LineDataRect.bottom = 61;
this->m_EditMsgRect.left = 524; this->m_EditMsgRect.top = 29;
this->m_EditMsgRect.right = 658; this->m_EditMsgRect.bottom = 61;
m_pFnt = new CAutoFont("隶书");
m_pFnt->SetHeight(28);
m_pFnt->SetWidth(12);
m_pFnt->SetBold(TRUE);
m_pFntNew = new CAutoFont("隶书");
m_pFntNew->SetHeight(25);
m_pFntNew->SetWidth(13);
m_pFntNew->SetBold(TRUE);
m_pFntNew->SetItalic(TRUE);
this->m_LineMsgEdit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_CENTER,this->m_LineMsgRect,
this,20100);
this->m_LineMsgEdit.SetWindowText("历史曲线模拟显示");
this->m_LineMsgEdit.SetFont(m_pFnt);
this->m_LineMsgEdit.SetReadOnly(TRUE);
this->m_LineMsgEdit.ShowWindow(SW_SHOW);
this->m_EditMsg.Create("",WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP,this->m_EditMsgRect,
this,20020);
this->m_EditMsg.SetBkColor(BLACK);
this->m_EditMsg.SetColor(DARKGREEN,LIGHTGREEN);
this->m_EditMsg.SetText("1233.58");
this->m_EditMsg.ShowWindow(SW_SHOW);
this->m_LineDataEdit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_CENTER,this->m_LineDataRect,
this,20101);
this->m_LineDataEdit.SetWindowText("2004年5月3日");
this->m_LineDataEdit.SetFont(m_pFnt);
this->m_LineDataEdit.SetReadOnly(TRUE);
this->m_LineDataEdit.ShowWindow(SW_SHOW);
}
void CCurverDemoView::InitbufferVal()
{
this->m_ItemMsg = new ItemMsg[1000];
CRecordset res(&db);
CString sql;
CDBVariant var;
var.Clear();
CString szId;
int nCount = 0;
sql.Format("Select * From Realanaquantity");
res.Open(CRecordset::forwardOnly,sql,CRecordset::readOnly);
while(!res.IsEOF())
{
CString szName;
res.GetFieldValue("name",var);
szName = *var.m_pstring;
if(szName != "undefine")
{
res.GetFieldValue("ID",var);
szId = *var.m_pstring;
m_ItemMsg[nCount].szId = szId;
m_ItemMsg[nCount].szName = szName;
nCount++;
}
res.MoveNext();
}
res.Close();
///
sql.Format("Select * From Realelecquantity");
res.Open(CRecordset::forwardOnly,sql,CRecordset::readOnly);
while(!res.IsEOF())
{
CString szName;
res.GetFieldValue("name",var);
szName = *var.m_pstring;
if(szName != "undefine")
{
res.GetFieldValue("ID",var);
szId = *var.m_pstring;
m_ItemMsg[nCount].szId = szId;
m_ItemMsg[nCount].szName = szName;
nCount++;
}
res.MoveNext();
}
res.Close();
nTolCount = nCount;
for(int i=0;i<this->nTolCount;i++)
for(int j=0;j<720;j++)
this->m_ItemMsg[i].fHisVal[j] = float(GetRandom(40000,50000))/100;
}
BOOL CCurverDemoView::ReadHisMsgFromFile(CString szYear, CString szMonth, CString szDay)
{
CString sPath,szFileName;
CDatabase db;
CString sql;
sPath = "d:\\历史数据文件夹\\"+szYear+"年"+szMonth+"月"+szDay+"日";
db.Open("JHWY_BY");
CFileFind fFind;
if(!fFind.FindFile(sPath))
{
::AfxMessageBox("没有找到历史记录!");
return FALSE;
}
//读模拟两信息
CFile file,Hisfile;
int nLen;
szFileName.Format("\\Device.qigao");
if(file.Open(sPath+szFileName,CFile::modeRead))
{
int t=file.GetLength();
nLen=file.GetLength()/(sizeof(float)*(nTolCount+1));
float *pfValue= new float[nTolCount+1];
for(int i=0;i<nLen;i++)
{
file.Read(pfValue,sizeof(float)*(nTolCount+1));
for(int j=1;j<nTolCount+1;j++)
m_ItemMsg[j-1].fHisVal[(int)pfValue[0]] = pfValue[j];
}
if(pfValue)
delete pfValue;
}
file.Close();
return TRUE;
}
void CCurverDemoView::DrawRealCurve()
{
CDC *pDC = GetDC();
CPen pNewPen,*pOldPen;
pNewPen.CreatePen(0,1,RGB(0,255,0));
pOldPen = pDC->SelectObject(&pNewPen);
int nXPoint[720],nYPoint[720];
float fMaxNum = 0.00f;
for(int i=0;i<720;i++)
{
if(m_ItemMsg[this->nPointSize].fHisVal[i] > fMaxNum)
fMaxNum = m_ItemMsg[this->nPointSize].fHisVal[i];
}
/////显示参数
CString szMaxNum;
CSize cSize;
fMaxNum = fMaxNum*3/2;
if(fMaxNum == 0) fMaxNum = 1;
CPen pNewPen1,*pOldPen1;
pNewPen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
pOldPen1 = (CPen *)pDC->SelectObject(&pNewPen1);
pDC->SetBkColor(RGB(0,0,0));
pDC->SetTextColor(RGB(255,0,0));
szMaxNum.Format("%.2f",fMaxNum);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN,szMaxNum);
szMaxNum.Format("%.2f",fMaxNum*4/5);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN+90,szMaxNum);
szMaxNum.Format("%.2f",fMaxNum*3/5);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN+180,szMaxNum);
szMaxNum.Format("%.2f",fMaxNum*2/5);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN+270,szMaxNum);
szMaxNum.Format("%.2f",fMaxNum/5);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN+360,szMaxNum);
/*szMaxNum.Format("%.2f",0);
cSize = pDC->GetTextExtent(szMaxNum);
pDC->TextOut(GRAPH_LEFT+GRAPH_LEFT_MARGIN-cSize.cx-10,GRAPH_TOP+GRAPH_TOP_MARGIN+410,szMaxNum);
*/ pDC->SelectObject(pOldPen1);
pNewPen1.DeleteObject();
////////////
float nMinNum = float(30*14)/fMaxNum;
for(i=0;i<720;i++)
{
nYPoint[i]=GRAPH_BOTTOM-GRAPH_BOTTOM_MARGIN-int((m_ItemMsg[this->nPointSize].fHisVal[i])*nMinNum);
if(int((m_ItemMsg[this->nPointSize].fHisVal[i])*nMinNum) > 420)
nYPoint[i]=GRAPH_TOP+GRAPH_TOP_MARGIN;
if(int((m_ItemMsg[this->nPointSize].fHisVal[i])*nMinNum) == 0)
nYPoint[i]=GRAPH_TOP+GRAPH_TOP_MARGIN+420;
nXPoint[i]=GRAPH_LEFT+GRAPH_LEFT_MARGIN+i;
if(i==0)
pDC->MoveTo(nXPoint[i],nYPoint[i]);
else
pDC->LineTo(nXPoint[i],nYPoint[i]);
}
pDC->SelectObject(&pOldPen);
pNewPen.DeleteObject();
ReleaseDC(pDC);
}
void CCurverDemoView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CView::OnTimer(nIDEvent);
}
void CCurverDemoView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int i = point.x - (GRAPH_LEFT+GRAPH_LEFT_MARGIN);
CString szVal;
if(i>=0 && i < 720)
{
szVal.Format("%.2f",this->m_ItemMsg[this->nPointSize].fHisVal[i]);
this->m_EditMsg.SetText(szVal);
}
CView::OnMouseMove(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -